Prices tell you what happened; fundamentals tell you why it might matter. Putting them in one table needs a join, and the join that most often goes wrong in quant work is the one on time. A price on 3 March should carry the revenue the market knew on 3 March, not the revenue reported on 20 March. The as-of join exists to make that match by date, and getting it right is what keeps a backtest from peeking at the future.
The idea in plain words
Where we are. Topic 3 summarised within one table. Here two tables meet. The first half is ordinary key joins: attach a column, find what has no match. The second half is the time join. The quarterly fundamentals come from the same dataset as in the SQL skill, where the as-of idea was taught in portable form; join_asof is the Polars operator for it.
A join that matches each row of the left table to the latest row of the right table at or before its time, instead of an exactly equal key. It needs both tables sorted by time, and an optional tolerance can refuse matches that are too old.
Attach a column, and find what has no match
A left join keeps every row of the left table and fills the new columns with nulls where nothing matched, so the table never silently shrinks. An inner join would drop those rows without a word. An anti join answers the audit question directly: which symbols in my prices have no sector? Run it after any join to see what you lost or never had.
# Left join: KO has no sector in the lookup, so its sector is null; the anti join names the gapjoined = join_sector(bars, sectors)print(joined.group_by("symbol").agg(pl.col("sector").first()).sort("symbol").rows())print("rows kept:", joined.height, "of", bars.height)print("no sector for:", symbols_without_sector(bars, sectors))
[('AAPL', 'Tech'), ('JNJ', 'Health'), ('JPM', 'Banks'), ('KO', None), ('XOM', 'Energy')]
rows kept: 33493 of 33493
no sector for: ['KO']Match to the latest earlier row
Each filing becomes visible on its filed date, not on the quarter's end. latest_filing sorts both frames by time, then join_asof walks them together: for every price it takes the newest filing of the same symbol on or before that day, by="symbol" keeping the symbols apart. Before a symbol's first filing the revenue is null, which is correct, because nothing was known yet.
# KO around its first filing in the data (24 April 2019): null before, then the report that was publicko = latest_filing(bars, filings).filter((pl.col("symbol") == "KO") & (pl.col("date") >= pl.date(2019, 4, 22)) & (pl.col("date") <= pl.date(2019, 4, 26)))for row in ko.rows(): print(row)
('KO', datetime.date(2019, 4, 22), 42.1946, datetime.date(2019, 2, 21), 7058000000)
('KO', datetime.date(2019, 4, 23), 42.915, datetime.date(2019, 2, 21), 7058000000)
('KO', datetime.date(2019, 4, 24), 42.7105, datetime.date(2019, 2, 21), 7058000000)
('KO', datetime.date(2019, 4, 25), 42.5862, datetime.date(2019, 4, 25), 8020000000)
('KO', datetime.date(2019, 4, 26), 42.96, datetime.date(2019, 4, 25), 8020000000)A tolerance for stale matches
The latest filing can be a year old. With tolerance=timedelta(days=max_days) the join refuses a match older than that and leaves nulls instead. recent_filing makes that choice explicit: a revenue figure from three quarters ago should not be presented as current. The number of nulls that appear is itself information about how complete the fundamentals are.
# How many price rows have no filing at all, and how many have none within 100 daysanywhere = latest_filing(bars, filings)["revenue"].null_count()recent = recent_filing(bars, filings, 100)["revenue"].null_count()print("no filing yet:", anywhere, "| none in the last 100 days:", recent, "| of", bars.height, "rows")
no filing yet: 21903 | none in the last 100 days: 25684 | of 33493 rows
A quarter ends on one date and the report comes out weeks later. Joining on the period end hands the model revenue that nobody had yet, which flatters every backtest. Always join fundamentals on the filing date. Check by looking at a row just before and just after a filing: the number must change on the filing day, not earlier.
What the tasks ask
- 1`join_sector` and `symbols_without_sector`
bars.join(sectors, on="symbol", how="left");how="anti", then distinct symbols sorted. - 2`latest_filing` and `recent_filing`Sort both frames by time, then
join_asof(right, left_on="date", right_on="filed", by="symbol", strategy="backward"); addtolerance=timedelta(days=max_days); sort by symbol and date.
Lab · what's graded
4 tasks · 100 pts- Attach a column:
join_sector25 pts - Who has no match:
symbols_without_sector25 pts - Match to the latest earlier row:
latest_filing25 pts - Only if recent enough:
recent_filing25 pts