| """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" |
|
|
| |
| |
| |
| |
| |
| |
| 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, |
| } |
|
|
| |
| |
| |
| |
| |
| |
| 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, |
| } |
|
|
| |
| |
| 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()]) |
|
|