"""Canonical Polars schemas for every published config.""" from __future__ import annotations from collections.abc import Mapping import polars as pl UTC_DATETIME = pl.Datetime(time_unit="us", time_zone="UTC") BUILD_VERSION = "1.0.0" PARSER_VERSION = "1.0.0" # One row per public information arrival. These are the kinds. EVENT_TYPES = ( "STATEMENT", "IMPLEMENTATION_NOTE", "SEP", "MINUTES", "PRESS_CONFERENCE", "LONGER_RUN_STRATEGY", "OTHER_POLICY_DOCUMENT", ) DOCUMENT_TYPES = ( "statement", "implementation_note", "minutes", "sep_text", "press_conference_transcript", "longer_run_strategy", "other", ) MEETING_TYPES = ("scheduled", "unscheduled", "notation_vote", "conference_call", "other") # How precisely the moment of publication is known. `exact` means the document # stated it; `date` means only the day is known and the instant is the end of # that day in Washington, which is late and never early. KNOWLEDGE_PRECISIONS = ("exact", "minute", "hour", "date", "modeled_conservative", "unknown") STATUSES = ( "scheduled", "discovered", "fetched", "parsed", "validated", "published", "quarantined", "revised", "cancelled", ) DECISIONS = ("raise", "cut", "hold", "other") VOTES = ("for", "against", "not_voting", "unknown") PROVENANCE: dict[str, pl.DataType] = { "source": pl.String, "source_url": pl.String, "source_document_id": pl.String, "source_type": pl.String, "raw_sha256": pl.String, "parser_version": pl.String, "first_seen_at": UTC_DATETIME, "ingested_at": UTC_DATETIME, } MEETING_SCHEMA: dict[str, pl.DataType] = { "meeting_id": pl.String, "meeting_start_date": pl.Date, "meeting_end_date": pl.Date, "meeting_type": pl.String, "scheduled": pl.Boolean, "has_statement": pl.Boolean, "has_implementation_note": pl.Boolean, "has_minutes": pl.Boolean, "has_sep": pl.Boolean, "has_press_conference": pl.Boolean, "chair": pl.String, # The instant the first thing about this meeting became public. A meetings # table without one cannot be mounted, and indexing it on the meeting date # would be exactly the look-ahead this dataset exists to remove. "knowledge_date": UTC_DATETIME, "source_url": pl.String, "ingested_at": UTC_DATETIME, } EVENT_SCHEMA: dict[str, pl.DataType] = { "event_id": pl.String, "meeting_id": pl.String, "event_type": pl.String, "scheduled_at": UTC_DATETIME, "release_at": UTC_DATETIME, "knowledge_at": UTC_DATETIME, # The same instant under the name ziplime indexes on. "knowledge_date": UTC_DATETIME, "release_date_et": pl.Date, "release_time_et": pl.Time, "timezone": pl.String, "target_rate_lower": pl.Float64, "target_rate_upper": pl.Float64, "target_rate_mid": pl.Float64, "rate_change_bp": pl.Float64, # The administered rates, from the implementation note published with the # statement. They are levels rather than a range, and the front end trades # against them directly. "iorb_rate": pl.Float64, "on_rrp_rate": pl.Float64, "primary_credit_rate": pl.Float64, "decision": pl.String, "vote_for": pl.Int32, "vote_against": pl.Int32, "knowledge_time_precision": pl.String, "status": pl.String, **PROVENANCE, } DOCUMENT_SCHEMA: dict[str, pl.DataType] = { "document_id": pl.String, "meeting_id": pl.String, "event_id": pl.String, "document_type": pl.String, "release_at": UTC_DATETIME, "knowledge_at": UTC_DATETIME, "knowledge_date": UTC_DATETIME, "knowledge_time_precision": pl.String, "title": pl.String, "text": pl.String, "word_count": pl.Int32, "html_url": pl.String, "pdf_url": pl.String, "language": pl.String, "content_hash": pl.String, **PROVENANCE, } SEP_SCHEMA: dict[str, pl.DataType] = { "sep_id": pl.String, "meeting_id": pl.String, "release_at": UTC_DATETIME, "knowledge_at": UTC_DATETIME, "knowledge_date": UTC_DATETIME, "variable": pl.String, "projection_year": pl.String, "central_tendency_low": pl.Float64, "central_tendency_high": pl.Float64, "range_low": pl.Float64, "range_high": pl.Float64, "median": pl.Float64, "unit": pl.String, "source_url": pl.String, # Which of the accessible version's two layouts the numbers came from. "extraction_method": pl.String, "parser_version": pl.String, "ingested_at": UTC_DATETIME, } DOT_PLOT_SCHEMA: dict[str, pl.DataType] = { "dot_id": pl.String, "meeting_id": pl.String, "release_at": UTC_DATETIME, "knowledge_at": UTC_DATETIME, "knowledge_date": UTC_DATETIME, "projection_year": pl.String, "rate_level": pl.Float64, "dot_count": pl.Int32, "source_url": pl.String, # Where the dots came from. The Federal Reserve publishes the chart's # underlying counts as a table in the accessible version of the # projections, so nothing here is read off an image. "extraction_method": pl.String, "parser_version": pl.String, "ingested_at": UTC_DATETIME, } VOTE_SCHEMA: dict[str, pl.DataType] = { "vote_id": pl.String, "meeting_id": pl.String, "event_id": pl.String, "member_name": pl.String, "vote": pl.String, "dissent": pl.Boolean, "preferred_action_text": pl.String, "source_url": pl.String, "knowledge_at": UTC_DATETIME, "knowledge_date": UTC_DATETIME, "parser_version": pl.String, "ingested_at": UTC_DATETIME, } CALENDAR_SCHEMA: dict[str, pl.DataType] = { "calendar_id": pl.String, "meeting_id": pl.String, "meeting_start_date": pl.Date, "meeting_end_date": pl.Date, "scheduled_at": UTC_DATETIME, "status": pl.String, "has_sep_scheduled": pl.Boolean, "timezone": pl.String, "knowledge_time_precision": pl.String, # When the schedule entry itself became knowable: a calendar is a forecast # of a publication, never the date it points at. "knowledge_date": UTC_DATETIME, "source_url": pl.String, "ingested_at": UTC_DATETIME, } # One row per public number the Committee produced, in the shape ziplime mounts # a point-in-time bundle from: an entity, the period it is about, and the # instant it became knowable. Everything else in this dataset is context for # these rows. PIT_SCHEMA: dict[str, pl.DataType] = { "entity_id": pl.String, "event_date": pl.Date, "knowledge_date": UTC_DATETIME, "meeting_id": pl.String, "event_id": pl.String, "event_type": pl.String, "actual": pl.Float64, "previous": pl.Float64, "change": pl.Float64, # Always null. Consensus forecasts are licensed data and this dataset # carries none; the column exists so a bundle can be mounted without one. "forecast": pl.Float64, "unit": pl.String, "decision": pl.String, "vote_for": pl.Int32, "vote_against": pl.Int32, # `reported` where the Committee printed the number, `derived` where this # recipe computed it from numbers the Committee printed. "value_method": pl.String, "knowledge_time_precision": pl.String, "source_url": pl.String, "parser_version": pl.String, "ingested_at": UTC_DATETIME, } CONFIG_SCHEMAS: dict[str, dict[str, pl.DataType]] = { "events": EVENT_SCHEMA, "meetings": MEETING_SCHEMA, "documents": DOCUMENT_SCHEMA, "sep": SEP_SCHEMA, "dot_plot": DOT_PLOT_SCHEMA, "votes": VOTE_SCHEMA, "calendar": CALENDAR_SCHEMA, "pit": PIT_SCHEMA, } def empty_frame(schema: Mapping[str, pl.DataType]) -> pl.DataFrame: return pl.DataFrame(schema=dict(schema)) def align(frame: pl.DataFrame, schema: Mapping[str, pl.DataType]) -> pl.DataFrame: missing = [ pl.lit(None, dtype=dtype).alias(name) for name, dtype in schema.items() if name not in frame.columns ] if missing: frame = frame.with_columns(missing) return frame.select([pl.col(n).cast(d, strict=False) for n, d in schema.items()]) __all__ = [ "BUILD_VERSION", "CALENDAR_SCHEMA", "CONFIG_SCHEMAS", "DECISIONS", "DOCUMENT_SCHEMA", "DOCUMENT_TYPES", "DOT_PLOT_SCHEMA", "EVENT_SCHEMA", "EVENT_TYPES", "KNOWLEDGE_PRECISIONS", "MEETING_SCHEMA", "MEETING_TYPES", "PARSER_VERSION", "PIT_SCHEMA", "PROVENANCE", "SEP_SCHEMA", "STATUSES", "UTC_DATETIME", "VOTES", "VOTE_SCHEMA", "align", "empty_frame", ]