"""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" # The Treasury's own labels, normalised. `cmb` is a cash management bill, which # is a bill with an irregular term announced at short notice; keeping it apart # from the regular bill calendar is the point of having the value. SECURITY_TYPES = ("bill", "note", "bond", "tips", "frn", "cmb", "other") CURVE_TYPES = ("nominal", "real", "bill", "long_term_composite", "long_term_real") # How precisely the moment of publication is known. `minute` means the artifact # stated it; `modeled_conservative` means a documented rule that is later than # the truth and never earlier. KNOWLEDGE_PRECISIONS = ("exact", "minute", "hour", "date", "modeled_conservative", "unknown") STATUSES = ( "scheduled", "discovered", "fetched", "parsed", "validated", "published", "quarantined", "revised", "cancelled", ) PROVENANCE: dict[str, pl.DataType] = { "source": pl.String, "source_url": pl.String, "raw_sha256": pl.String, "parser_version": pl.String, "first_seen_at": UTC_DATETIME, "ingested_at": UTC_DATETIME, } AUCTION_CALENDAR_SCHEMA: dict[str, pl.DataType] = { "auction_id": pl.String, "cusip": pl.String, "security_type": pl.String, "security_term": pl.String, "original_security_term": pl.String, "reopening": pl.Boolean, "announcement_date": pl.Date, "announcement_at": UTC_DATETIME, "auction_date": pl.Date, "auction_at": UTC_DATETIME, "issue_date": pl.Date, "maturity_date": pl.Date, "offering_amount": pl.Float64, "status": pl.String, "knowledge_at": UTC_DATETIME, "knowledge_date": UTC_DATETIME, "knowledge_time_precision": pl.String, **PROVENANCE, } ANNOUNCEMENT_SCHEMA: dict[str, pl.DataType] = { "announcement_id": pl.String, "auction_id": pl.String, "release_at": UTC_DATETIME, "knowledge_at": UTC_DATETIME, "knowledge_date": UTC_DATETIME, "knowledge_time_precision": pl.String, "offering_amount": pl.Float64, "security_type": pl.String, "security_term": pl.String, "cusip": pl.String, "announcement_date": pl.Date, "auction_date": pl.Date, "issue_date": pl.Date, "maturity_date": pl.Date, "competitive_close_at": UTC_DATETIME, "noncompetitive_close_at": UTC_DATETIME, "reopening": pl.Boolean, "soma_holdings": pl.Float64, "maturing_amount": pl.Float64, **PROVENANCE, } RESULT_SCHEMA: dict[str, pl.DataType] = { "result_id": pl.String, "auction_id": pl.String, "cusip": pl.String, "security_type": pl.String, "security_term": pl.String, "auction_date": pl.Date, "issue_date": pl.Date, "maturity_date": pl.Date, "result_release_at": UTC_DATETIME, "knowledge_at": UTC_DATETIME, "knowledge_date": UTC_DATETIME, "knowledge_time_precision": pl.String, "auction_format": pl.String, "offering_amount": pl.Float64, "total_tendered": pl.Float64, "total_accepted": pl.Float64, "competitive_tendered": pl.Float64, "competitive_accepted": pl.Float64, "noncompetitive_tendered": pl.Float64, "noncompetitive_accepted": pl.Float64, "high_yield": pl.Float64, "median_yield": pl.Float64, "low_yield": pl.Float64, "high_discount_rate": pl.Float64, "median_discount_rate": pl.Float64, "low_discount_rate": pl.Float64, "investment_rate": pl.Float64, "interest_rate": pl.Float64, "price_per_100": pl.Float64, "bid_to_cover": pl.Float64, "allocation_percentage": pl.Float64, "primary_dealer_tendered": pl.Float64, "primary_dealer_accepted": pl.Float64, "direct_bidder_tendered": pl.Float64, "direct_bidder_accepted": pl.Float64, "indirect_bidder_tendered": pl.Float64, "indirect_bidder_accepted": pl.Float64, "soma_tendered": pl.Float64, "soma_accepted": pl.Float64, "treasury_retail_accepted": pl.Float64, # Deterministic shares of what was accepted, computed only from numbers in # the same result. Null where the denominator is absent, never zero. "dealer_accepted_pct": pl.Float64, "direct_accepted_pct": pl.Float64, "indirect_accepted_pct": pl.Float64, "noncompetitive_accepted_pct": pl.Float64, "tendered_to_offering": pl.Float64, # Always null. The tail is the high yield minus the when-issued market # yield, and no official point-in-time when-issued series exists. The daily # par curve is a different measurement and would not be a tail. "wi_yield": pl.Float64, "auction_tail": pl.Float64, "status": pl.String, **PROVENANCE, } CURVE_SCHEMA: dict[str, pl.DataType] = { "curve_id": pl.String, "curve_date": pl.Date, "curve_type": pl.String, "maturity": pl.String, "source_label": pl.String, "maturity_years": pl.Float64, "yield_pct": pl.Float64, "market_observation_at": UTC_DATETIME, "publication_at": UTC_DATETIME, "knowledge_at": UTC_DATETIME, "knowledge_date": UTC_DATETIME, "knowledge_time_precision": pl.String, "availability_time_basis": pl.String, "methodology_id": pl.String, **PROVENANCE, } BILL_RATE_SCHEMA: dict[str, pl.DataType] = { "bill_rate_id": pl.String, "rate_date": pl.Date, "maturity": pl.String, "source_label": pl.String, "maturity_weeks": pl.Float64, "bank_discount_rate": pl.Float64, "coupon_equivalent_rate": pl.Float64, "market_observation_at": UTC_DATETIME, "knowledge_at": UTC_DATETIME, "knowledge_date": UTC_DATETIME, "knowledge_time_precision": pl.String, "availability_time_basis": pl.String, **PROVENANCE, } CURVE_FEATURE_SCHEMA: dict[str, pl.DataType] = { "feature_id": pl.String, "curve_date": pl.Date, "knowledge_at": UTC_DATETIME, "knowledge_date": UTC_DATETIME, "knowledge_time_precision": pl.String, "slope_3m10y_bp": pl.Float64, "slope_2y10y_bp": pl.Float64, "slope_5y30y_bp": pl.Float64, "real_5y10y_slope_bp": pl.Float64, # A spread between two official curves published for the same day. It is # not a market breakeven: both legs are par yields struck from indicative # quotations, and neither is a traded inflation swap. "nominal_10y_minus_real_10y_bp": pl.Float64, "derived_from": pl.String, "parser_version": pl.String, "ingested_at": UTC_DATETIME, } # One row per public number, in the shape ziplime mounts a bundle from. PIT_SCHEMA: dict[str, pl.DataType] = { "entity_id": pl.String, "event_date": pl.Date, "knowledge_date": UTC_DATETIME, "subset": pl.String, "record_id": pl.String, "actual": pl.Float64, "previous": pl.Float64, "change": pl.Float64, # Always null: consensus estimates are licensed data and none are included. "forecast": pl.Float64, "unit": pl.String, "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]] = { "auction_calendar": AUCTION_CALENDAR_SCHEMA, "auction_announcements": ANNOUNCEMENT_SCHEMA, "auction_results": RESULT_SCHEMA, "nominal_yield_curve": CURVE_SCHEMA, "real_yield_curve": CURVE_SCHEMA, "bill_rates": BILL_RATE_SCHEMA, "curve_features": CURVE_FEATURE_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__ = [ "ANNOUNCEMENT_SCHEMA", "AUCTION_CALENDAR_SCHEMA", "BILL_RATE_SCHEMA", "BUILD_VERSION", "CONFIG_SCHEMAS", "CURVE_FEATURE_SCHEMA", "CURVE_SCHEMA", "CURVE_TYPES", "KNOWLEDGE_PRECISIONS", "PARSER_VERSION", "PIT_SCHEMA", "PROVENANCE", "RESULT_SCHEMA", "SECURITY_TYPES", "STATUSES", "UTC_DATETIME", "align", "empty_frame", ]