Sub topic 1
Why a Model Can Look Good Offline and Fail in Production
A model can score well during training and validation yet perform poorly after deployment. A common cause is temporal data leakage. Temporal data leakage occurs when a training row contains information that would not have been available when the model made the prediction.
The model then learns from the future. Offline metrics, such as accuracy or area under the ROC curve, become too optimistic. Production data does not contain the same future information, so performance drops.
Prerequisites
This chapter assumes you understand basic supervised machine learning, including features, labels, and train/validation splits. You should also know basic pandas DataFrame operations, SQL joins, and timestamps.
A concrete example
Suppose you are predicting whether a customer will cancel a subscription. The prediction is made on January 10. The label records whether the customer cancels within the next 30 days.
A feature called support_tickets_30d counts support tickets before the prediction. It is valid only if it uses tickets known by January 10.
A careless training query might count all tickets in the 30-day calendar period around the prediction date. That count could include a ticket created on January 15. The ticket may be strongly associated with cancellation, so the model looks accurate offline. At serving time on January 10, however, that ticket does not exist yet.
The same problem appears when a feature table stores only the latest value. A historical training row might accidentally receive a value calculated weeks after its prediction timestamp.
import pandas as pd
labels = pd.DataFrame({
"customer_id": [1],
"prediction_ts": pd.to_datetime(["2026-01-10"]),
"churned": [1],
})
features = pd.DataFrame({
"customer_id": [1, 1],
"feature_ts": pd.to_datetime(["2026-01-05", "2026-01-15"]),
"support_tickets_30d": [2, 8],
})
# The January 15 value is future information for the January 10 prediction.
leaking_row = features.loc[features["customer_id"].eq(1)].iloc[-1]
print(leaking_row["support_tickets_30d"]) # 8The offline pipeline may silently choose 8, while production would have seen only 2. This difference is often called training-serving skew: training and production compute or retrieve features under different rules.
The correct mental model
Each training row represents one prediction made for one entity at one moment. The row may use only information available at that moment.
- The entity is the object being scored, such as a customer.
- The prediction timestamp is when the score would have been produced.
- The label timestamp or outcome period describes what happened afterward.
- A feature is valid only when its source data was available no later than the prediction timestamp.
The label can depend on the future because the label describes the outcome being predicted. Features cannot depend on that future outcome or on later observations.
How leakage inflates metrics
Leakage changes the validation task. Instead of asking, “Can the model predict using information available at deployment time?” the evaluation asks, “Can the model recognize the outcome using information collected afterward?”
This can produce:
- unusually high validation scores;
- features with suspiciously strong importance;
- a large drop in performance after deployment;
- different feature distributions between training and serving;
- failures that are difficult to reproduce because the data changes over time.
Random validation splits can hide the issue. Rows from a later period may appear in training while earlier rows appear in validation. Even without direct row duplication, the model may learn patterns from a future data-generating process.
First protections
Start by assigning a prediction timestamp to every label row. Store feature history with timestamps instead of keeping only current values. Build each training row with a point-in-time, or as-of, join: select the newest feature value whose timestamp is at or before the prediction timestamp.
Then validate the result. A useful check is:
assert (joined["feature_ts"] <= joined["prediction_ts"]).all()Also split training and validation by time. Train on earlier predictions and validate on later predictions. This better matches deployment, where the model predicts future cases.
Exercise
A customer is scored on March 1. Which feature value is valid: a value computed on February 28 or a value computed on March 3?
Show answer
The February 28 value is valid. The March 3 value is future information and creates temporal leakage.