"""Dividends per share, as filings stated them. The tag zoo is smaller here than for the income statement but still real: `CommonStockDividendsPerShareDeclared` is what most filers use, `CommonStockDividendsPerShareCashPaid` is what the rest use, and the two mean different things -- declared is a decision, paid is a cash movement in the period. They are kept apart rather than coalesced. """ from __future__ import annotations from datetime import UTC, datetime import polars as pl from .schema import DIVIDEND_SCHEMA, align, empty_frame # tag -> (kind, security class). Preferred dividends are included because a # preferred coupon is a claim ahead of the common holder, and a yield computed # without it is wrong for exactly the companies where it matters. DIVIDEND_TAGS: dict[str, tuple[str, str]] = { "CommonStockDividendsPerShareDeclared": ("declared", "common"), "CommonStockDividendsPerShareCashPaid": ("cash_paid", "common"), "PreferredStockDividendsPerShareDeclared": ("declared", "preferred"), "PreferredStockDividendsPerShareCashPaid": ("cash_paid", "preferred"), "DividendsPayableAmountPerShare": ("payable", "common"), "CommonStockDividendsPerShareCashPaidNetOfTax": ("cash_paid", "common"), } # A filing may state a dividend declared after the period closed, tagged as a # subsequent event. It is forward-looking information -- the declaration is # already public when the filing is -- and dropping it would discard the one # dividend fact that is not history. SUBSEQUENT_EVENT = "SubsequentEvent" def build_dividends(facts: pl.LazyFrame | pl.DataFrame) -> pl.DataFrame: """Every per-share dividend fact, consolidated and dimensional alike.""" lazy = facts.lazy() if isinstance(facts, pl.DataFrame) else facts mapping = pl.DataFrame( [ {"tag": tag, "kind": kind, "security_class": security} for tag, (kind, security) in DIVIDEND_TAGS.items() ] ).lazy() frame = ( lazy.filter(pl.col("value").is_not_null() & (pl.col("value") >= 0)) .join(mapping, on="tag", how="inner") .with_columns( pl.col("segments").fill_null("").str.contains(SUBSEQUENT_EVENT) .alias("is_subsequent_event"), pl.col("unit").alias("currency"), pl.col("value").alias("amount_per_share"), ) .collect() ) if frame.is_empty(): return empty_frame(DIVIDEND_SCHEMA) # The period a per-share dividend covers is a duration, and the fact's own # `quarters` says how long. An instant-dated dividend per share is a filer # error and is dropped rather than dated to a day it did not cover. frame = frame.filter(pl.col("quarters") > 0).with_columns( pl.col("period_end") .dt.offset_by(pl.format("-{}mo", pl.col("quarters") * 3)) .alias("period_start") ) frame = frame.sort(["cik", "kind", "security_class", "period_end", "quarters", "accepted_at"]) frame = frame.with_columns( pl.col("accepted_at") .rank("ordinal") .over(["cik", "kind", "security_class", "period_end", "quarters"]) .cast(pl.Int32) .alias("revision"), pl.lit(datetime.now(UTC)).alias("inserted_at"), ) return align(frame, DIVIDEND_SCHEMA)