Datasets:
File size: 7,033 Bytes
06a67ad | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | """What the derived tables must say about a company that changed business."""
from __future__ import annotations
from datetime import date
import polars as pl
from recipe.build import (
build_by_ticker,
build_classification,
build_spans,
build_taxonomy_table,
entity_fallback,
)
from recipe.schema import CONFIG_SCHEMAS
def test_spans_are_runs_not_groups(observations):
spans = build_spans(observations)
first = spans.filter(pl.col("cik") == "0000000001").sort("valid_from")
assert first["sic"].to_list() == ["6770", "3711"]
assert first["valid_from"].to_list() == [date(2020, 3, 2), date(2021, 5, 2)]
# valid_to is the last filing that said so, not the day before the next.
assert first["valid_to"].to_list() == [date(2020, 8, 2), date(2024, 5, 2)]
assert first["observations"].to_list() == [2, 2]
def test_a_company_reclassified_back_keeps_both_spans():
"""A groupby on (filer, code) would merge these and lose the order."""
rows = [
{"cik": "1", "sic": "6770", "stated_on": date(2020, 1, 1), "knowledge_time": None,
"source": "xbrl", "source_ref": "a", "is_dated": True, "inserted_at": None},
{"cik": "1", "sic": "3711", "stated_on": date(2021, 1, 1), "knowledge_time": None,
"source": "xbrl", "source_ref": "b", "is_dated": True, "inserted_at": None},
{"cik": "1", "sic": "6770", "stated_on": date(2022, 1, 1), "knowledge_time": None,
"source": "xbrl", "source_ref": "c", "is_dated": True, "inserted_at": None},
]
spans = build_spans(pl.DataFrame(rows)).sort("valid_from")
assert spans.height == 3
assert spans["rank_for_filer"].to_list() == [3, 2, 1]
assert spans["is_latest"].to_list() == [False, False, True]
def test_the_latest_span_is_the_one_marked(observations):
spans = build_spans(observations)
latest = spans.filter(pl.col("is_latest")).sort("cik")
assert latest["cik"].to_list() == ["0000000001", "0000000002", "0000000003"]
assert latest["sic"].to_list() == ["3711", "2834", "6022"]
def test_classification_is_one_row_per_filer(observations, directory):
spans = build_spans(observations)
reference = pl.DataFrame({
"sic": ["3711", "2834", "6022"],
"title": ["MOTOR VEHICLES", "PHARMACEUTICAL PREPARATIONS", "STATE COMMERCIAL BANKS"],
"sic_level": ["industry"] * 3, "sec_office": [None] * 3,
"division": ["D", "D", "H"], "division_title": ["Manufacturing"] * 2 + ["Finance"],
"major_group": ["37", "28", "60"], "major_group_title": [None] * 3,
"industry_group": ["371", "283", "602"],
"sector_code": ["CONC", "HLTH", "FINS"],
"sector": ["Consumer Cyclical", "Health Care", "Financials"],
"industry_code": ["CONC_AUTO", "HLTH_PHARMA", "FINS_BANK"],
"industry": ["Automobiles", "Pharmaceuticals", "Banks"],
"naics_1997": [None] * 3, "naics_title": [None] * 3,
"naics_sector": [None] * 3, "naics_sector_title": [None] * 3,
"naics_candidates": [0, 0, 0],
})
frame = build_classification(spans, directory, reference, entity_fallback(pl.DataFrame()))
assert frame.height == frame["cik"].n_unique() == 3
car = frame.filter(pl.col("cik") == "0000000001").row(0, named=True)
assert (car["sector_code"], car["sic"]) == ("CONC", "3711")
assert car["is_dated"] is True
bank = frame.filter(pl.col("cik") == "0000000003").row(0, named=True)
# Its only code came from today's snapshot, so it carries no history.
assert bank["is_dated"] is False
def test_by_ticker_clips_to_the_overlap(observations, tickers, directory):
spans = build_spans(observations)
frame = build_by_ticker(tickers, spans, directory)
spac = frame.filter(pl.col("ticker") == "SPAC").row(0, named=True)
assert spac["sector_code"] == "SHEL"
assert spac["valid_to"] == date(2020, 8, 2) # the SIC span ends first
assert spac["date_basis"] == "dated"
carz = frame.filter(pl.col("ticker") == "CARZ").row(0, named=True)
assert (carz["sector_code"], carz["is_current"]) == ("CONC", True)
def test_a_ticker_that_predates_the_evidence_still_gets_a_row(tickers, observations, directory):
"""Delisted in 2007, so no filing overlaps; dropping it would leave the
universe made of survivors."""
frame = build_by_ticker(tickers, build_spans(observations), directory)
gone = frame.filter(pl.col("ticker") == "GONE").row(0, named=True)
assert gone["date_basis"] == "undated"
assert gone["sector_code"] == "FINS"
assert (gone["valid_from"], gone["valid_to"]) == (date(2006, 4, 1), date(2007, 9, 1))
def test_a_ticker_with_no_code_at_all_is_present_and_unclassified(
tickers, observations, directory
):
frame = build_by_ticker(tickers, build_spans(observations), directory)
huh = frame.filter(pl.col("ticker") == "HUH").row(0, named=True)
assert (huh["date_basis"], huh["sector_code"], huh["sic"]) == ("unknown", "UNCL", None)
def test_is_current_marks_exactly_one_row_per_live_ticker(tickers, observations, directory):
"""A live ticker whose company was reclassified must not come back current
under both its old sector and its new one."""
frame = build_by_ticker(tickers, build_spans(observations), directory)
current = frame.filter(pl.col("is_current"))
assert current.height == current["ticker"].n_unique()
assert set(current["ticker"]) == {"CARZ", "PILL"}
def test_taxonomy_table_lists_every_industry():
frame = build_taxonomy_table()
assert set(frame.columns) <= set(CONFIG_SCHEMAS["taxonomy"])
assert frame["industry_code"].n_unique() == frame.height
assert frame.filter(pl.col("industry_code") == "HLTH_PHARMA")["sic_ranges"].item()
def test_a_filer_known_only_by_its_forms_still_gets_classified(
observations, tickers, directory
):
"""A closed-end fund has no industry code and never will. Saying "fund"
beats a null, which reads the same as a failure."""
kinds = pl.DataFrame({
"cik": ["0000000004"], "entity_kind": ["investment_company"],
"source_ref": ["CIK0000000004.json"],
})
fallback = entity_fallback(kinds)
assert fallback.row(0, named=True)["sector_code"] == "FUND"
frame = build_by_ticker(tickers, build_spans(observations), directory, fallback)
huh = frame.filter(pl.col("ticker") == "HUH").row(0, named=True)
assert (huh["sector_code"], huh["industry_code"]) == ("FUND", "FUND_INVEST")
assert huh["date_basis"] == "unknown" # still no date behind it
assert huh["sic"] is None # and no code was invented for it
def test_the_fallback_never_overrides_an_assigned_code(observations, tickers, directory):
kinds = pl.DataFrame({
"cik": ["0000000002"], "entity_kind": ["investment_company"],
"source_ref": ["x"],
})
frame = build_by_ticker(
tickers, build_spans(observations), directory, entity_fallback(kinds)
)
pill = frame.filter(pl.col("ticker") == "PILL").row(0, named=True)
assert pill["sector_code"] == "HLTH"
|