File size: 1,637 Bytes
de2ca63 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | """
Macro Indicators — Vintage / PIT (FRED-style) — collection recipe.
Contract (ziplime PIT recipe, design doc §9):
async def fetch(since: datetime) -> pl.DataFrame
Returns rows in the PIT schema for `macro-indicators`:
system: entity_id, event_date, knowledge_date, knowledge_estimated
values: series_value, unit, native_frequency, release_kind
The shared harness (ingest.py) stamps `ingested_at`, dedups against existing rows and
appends to the Delta bundle. This recipe only fetches and shapes.
"""
from __future__ import annotations
from datetime import datetime, timezone
async def fetch(since: datetime):
import polars as pl # noqa: F401
import httpx
import polars as pl
rows = []
for series in SERIES_CODES:
# ALFRED returns every vintage (real-time period) for the series
obs = httpx.get(ALFRED_URL, params={"series_id": series, "since": since.date().isoformat()},
timeout=60).json()["observations"]
for o in obs:
rows.append({
"entity_id": series,
"event_date": o["period"],
"knowledge_date": o["realtime_start"], # when this vintage was released
"knowledge_estimated": False,
"series_value": o["value"],
"unit": o["unit"],
"native_frequency": o["frequency"],
"release_kind": o["release_kind"],
})
return pl.DataFrame(rows)
if __name__ == "__main__":
import asyncio, polars as pl
df = asyncio.run(fetch(datetime(2025, 1, 1, tzinfo=timezone.utc)))
print(df.head())
|