Which value was known on each date?

Match each daily price to the latest earnings figure published by that date with merge_asof, measure the age of the matched figure, and drop figures older than a limit.

After this lesson, you can match each daily price to the latest earnings figure the market could know on that date.

How do I match each price to the earnings known on its date?

pd.merge_asof(prices, reports, left_on="date", right_on="published", by="ticker") gives each price the latest report of the same stock published on or before the price’s date, so no price gets a figure from the future. AAA’s earnings per share of 3.0 kronor appear from 5 May, its publication date, and BBB’s 1.5 from 7 May.

The made-up reports give each company’s quarterly earnings per share, EPS, in kronor, with the quarter’s end and the publication date, and the prices give daily closes in kronor per share. Matching on the quarter’s end, 31 March, would give the prices of 4 May a figure that no one knew until 5 May, which is look-ahead bias. Both tables must be sorted by their dates, and kind="stable" keeps AAA before BBB on each date. The match assumes that each report comes out before the close, as in the lesson on merge_asof.

The following box builds the tables and matches the prices to the reports.

import pandas as pd

reports = pd.DataFrame({
    "ticker": ["AAA", "BBB"],
    "period_end": pd.to_datetime(["2026-03-31", "2026-03-31"]),
    "published": pd.to_datetime(["2026-05-05", "2026-05-07"]),
    "eps": [3.0, 1.5],
})
prices = pd.DataFrame({
    "date": pd.to_datetime(["2026-05-04", "2026-05-05", "2026-05-06", "2026-05-07", "2026-05-08", "2026-05-11"] * 2),
    "ticker": ["AAA"] * 6 + ["BBB"] * 6,
    "close": [100, 102, 101, 103, 104, 105, 50, 51, 50, 52, 53, 54],
})
prices = prices.sort_values("date", kind="stable")
matched = pd.merge_asof(prices, reports.sort_values("published"), left_on="date", right_on="published", by="ticker")
print(matched[["date", "ticker", "close", "published", "eps"]].to_string(index=False))
      date ticker  close  published  eps
2026-05-04    AAA    100        NaT  NaN
2026-05-04    BBB     50        NaT  NaN
2026-05-05    AAA    102 2026-05-05  3.0
2026-05-05    BBB     51        NaT  NaN
2026-05-06    AAA    101 2026-05-05  3.0
2026-05-06    BBB     50        NaT  NaN
2026-05-07    AAA    103 2026-05-05  3.0
2026-05-07    BBB     52 2026-05-07  1.5
2026-05-08    AAA    104 2026-05-05  3.0
2026-05-08    BBB     53 2026-05-07  1.5
2026-05-11    AAA    105 2026-05-05  3.0
2026-05-11    BBB     54 2026-05-07  1.5

NaT marks a missing date, here before each stock’s first report.

How old is each matched figure, and how do I limit the age?

The date minus the publication date gives each figure’s age, and tolerance=pd.Timedelta(days=3) in merge_asof drops figures older than three days. On 11 May, AAA’s figure is six days old and BBB’s four, so both become NaN, while AAA’s figure on 8 May, exactly three days old, stays.

.dt.days turns each time difference into whole days. Three days is a limit chosen for this example, and quarterly earnings usually stay valid until the next report, about three months later.

The following box computes the ages and repeats the match with the limit.

matched["age_days"] = (matched["date"] - matched["published"]).dt.days
recent = pd.merge_asof(prices, reports.sort_values("published"), left_on="date", right_on="published", by="ticker", tolerance=pd.Timedelta(days=3))
matched["eps_max_3_days"] = recent["eps"]
print(matched[["date", "ticker", "eps", "age_days", "eps_max_3_days"]].to_string(index=False))
      date ticker  eps  age_days  eps_max_3_days
2026-05-04    AAA  NaN       NaN             NaN
2026-05-04    BBB  NaN       NaN             NaN
2026-05-05    AAA  3.0       0.0             3.0
2026-05-05    BBB  NaN       NaN             NaN
2026-05-06    AAA  3.0       1.0             3.0
2026-05-06    BBB  NaN       NaN             NaN
2026-05-07    AAA  3.0       2.0             3.0
2026-05-07    BBB  1.5       0.0             1.5
2026-05-08    AAA  3.0       3.0             3.0
2026-05-08    BBB  1.5       1.0             1.5
2026-05-11    AAA  3.0       6.0             NaN
2026-05-11    BBB  1.5       4.0             NaN

Your turn

Repeat the match with a limit of four days and print BBB’s dates and EPS. You should see NaN before 7 May and 1.5 on 7, 8 and 11 May.

A figure exactly four days old stays within the limit.

four_days = pd.merge_asof(prices, reports.sort_values("published"), left_on="date", right_on="published", by="ticker", tolerance=pd.Timedelta(days=4))
print(four_days.loc[four_days["ticker"] == "BBB", ["date", "eps"]].to_string(index=False))
      date  eps
2026-05-04  NaN
2026-05-05  NaN
2026-05-06  NaN
2026-05-07  1.5
2026-05-08  1.5
2026-05-11  1.5