Sub topic 2
The as-of join: latest value at or before the prediction time
Prerequisites
This chapter assumes you know supervised learning terms (features, labels, train and validation sets), SQL joins and window functions, basic pandas DataFrame operations, and timestamps with time zones.
The as-of join
A feature value is usable only if it existed when the prediction was made. An as-of join attaches to each label row the most recent feature row at or before that row's prediction timestamp, for the same entity.
import pandas as pd
labels = pd.DataFrame({
"customer_id": [1, 1, 2],
"prediction_ts": pd.to_datetime(["2026-03-10", "2026-06-01", "2026-06-01"]),
"churned": [0, 1, 0],
})
features = pd.DataFrame({
"customer_id": [1, 1, 2],
"feature_ts": pd.to_datetime(["2026-03-01", "2026-05-20", "2026-01-15"]),
"avg_spend_30d": [40.0, 55.0, 12.0],
})
train = pd.merge_asof(
labels.sort_values("prediction_ts"),
features.sort_values("feature_ts"),
left_on="prediction_ts",
right_on="feature_ts",
by="customer_id",
direction="backward",
tolerance=pd.Timedelta("30D"),
)
print(train)Both inputs must be sorted on their time keys. The direction="backward" setting matches only feature rows at or before each prediction time, so no future value can enter a row.
Limiting staleness with TTL and join tolerance
The tolerance argument sets the maximum age of a joined feature value. In the example, customer 2 has no feature row within 30 days of the prediction, so avg_spend_30d becomes NaN instead of a five-month-old value. A time-to-live (TTL) setting plays the same role in feature stores.
Choose the tolerance from how often the feature refreshes:
| Feature type | Refresh rate | Suggested tolerance |
|---|---|---|
| Daily spend totals | Daily | 2 days |
| Monthly plan tier | Monthly | 35 days |
| Account age | Static | No tolerance limit |
> Note: A rising share of NaN values after the join usually means the tolerance is too short or a feature pipeline has stopped.
Assembling the training set end to end
A complete build follows these steps:
- Load labels with an entity ID, a prediction timestamp, and the label.
- Load append-only feature history with event time and ingestion time.
- Keep only feature rows with
ingested_ts <= prediction_ts, so late-arriving corrections do not leak backward. - Run the as-of join with a tolerance.
- Check for leakage and duplicated label rows.
- Split the result by time.
Splitting by time, not at random
A random split lets the model train on rows from after some validation rows. That gives an optimistic score. Splitting on a cutoff date keeps validation strictly later than training, which matches production use.
cutoff = pd.Timestamp("2026-04-01")
train_df = train[train["prediction_ts"] < cutoff]
valid_df = train[train["prediction_ts"] >= cutoff]Exercise
In the example, why does customer 2 get NaN for avgspend30d on 2026-06-01?
Show answer
The only feature row for customer 2 is dated 2026-01-15. That is more than 30 days before the prediction time, so the 30-day tolerance rejects it.