ZipLime's picture
Replace placeholder with real SEC EDGAR fundamentals PIT bundle (93 issuers, 23.5k as-reported facts incl. restatements)
0561fec verified
Raw
History Blame Contribute Delete
1.46 kB
"""Append-only ingest harness for the SEC fundamentals PIT bundle."""
from __future__ import annotations
import argparse
from datetime import datetime, timezone
from pathlib import Path
import polars as pl
from deltalake import DeltaTable, write_deltalake
import recipe
TABLE_URI = str(Path(__file__).parent / "data" / "data_bundle" /
"sec_fundamentals_pit" / "1784818614" / "data.delta")
def _row_hash(df: pl.DataFrame) -> pl.Series:
cols = [c for c in df.columns if c != "ingested_at"]
return df.select(cols).hash_rows().cast(pl.Utf8)
async def main(since: datetime) -> None:
fresh = await recipe.fetch(since)
if fresh.is_empty():
print("no rows"); return
fresh = fresh.with_columns(pl.lit(datetime.now(timezone.utc)).alias("ingested_at"))
try:
existing = pl.from_arrow(DeltaTable(TABLE_URI).to_pyarrow_table())
seen = set(_row_hash(existing).to_list())
fresh = fresh.filter(~_row_hash(fresh).is_in(seen))
except Exception:
pass
if fresh.is_empty():
print("no new/revised rows"); return
write_deltalake(TABLE_URI, fresh.to_arrow(), mode="append", partition_by=["knowledge_year"])
print(f"appended {len(fresh)} rows")
if __name__ == "__main__":
ap = argparse.ArgumentParser(); ap.add_argument("--since", default="2020-01-01")
import asyncio
asyncio.run(main(datetime.fromisoformat(ap.parse_args().since).replace(tzinfo=timezone.utc)))