"""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" # One dividend as a filing stated it: an amount per share for a fiscal period. # # Not an event with an ex-date. SEC's structured data carries no dividend dates # at all -- the date-typed XBRL facts exist in filings but `num.txt` holds only # numeric ones, and the XBRL API returns 404 for them. What is here is what can # be had: how much per share, over which period, known from when. DIVIDEND_SCHEMA: dict[str, pl.DataType] = { "cik": pl.String, "accession_number": pl.String, "kind": pl.String, "security_class": pl.String, "period_start": pl.Date, "period_end": pl.Date, "quarters": pl.Int32, "amount_per_share": pl.Float64, "currency": pl.String, "is_subsequent_event": pl.Boolean, "form": pl.String, "fiscal_year": pl.Int32, "fiscal_period": pl.String, "filed_date": pl.Date, "accepted_at": UTC_DATETIME, "revision": pl.Int32, "inserted_at": UTC_DATETIME, } # One split, and the window it must have happened in. # # A split has no date here either, and saying otherwise would be invention. It # is detected by the trace it leaves: a filing restates an earlier period's per # share figures by the split ratio, so the split falls between the filing that # still used the old numbers and the one that used the new. SPLIT_SCHEMA: dict[str, pl.DataType] = { "cik": pl.String, "ratio": pl.Float64, "ratio_label": pl.String, "is_reverse": pl.Boolean, "detected_after": UTC_DATETIME, "detected_before": UTC_DATETIME, "effective_period_end": pl.Date, "method": pl.String, "confidence": pl.String, "corroborated_by": pl.String, "evidence_observations": pl.Int32, "inserted_at": UTC_DATETIME, } # The number a user actually needs: multiply an as-filed per-share figure by # this to put it on the same footing as a split-adjusted price series. ADJUSTMENT_SCHEMA: dict[str, pl.DataType] = { "cik": pl.String, "valid_from": pl.Date, "valid_to": pl.Date, "cumulative_split_factor": pl.Float64, "splits_after": pl.Int32, "confidence": pl.String, "inserted_at": UTC_DATETIME, } PIT_SCHEMA: dict[str, pl.DataType] = { "pit_event_id": pl.String, "entity_id": pl.String, "event_date": pl.Date, "knowledge_date": UTC_DATETIME, "knowledge_estimated": pl.Boolean, "action_type": pl.String, "amount_per_share": pl.Float64, "currency": pl.String, "split_ratio": pl.Float64, "security_class": pl.String, "quarters": pl.Int32, "is_subsequent_event": pl.Boolean, "confidence": pl.String, "ingested_at": UTC_DATETIME, } CONFIG_SCHEMAS: dict[str, dict[str, pl.DataType]] = { "dividends": DIVIDEND_SCHEMA, "splits": SPLIT_SCHEMA, "adjustment_factors": ADJUSTMENT_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()])