Sub topic 7
Cause 4: Time Leakage and Point-in-Time Correctness
Assume you know basic Python and pandas, understand supervised learning, and have seen how a model is deployed for batch or API predictions. This chapter focuses on one question: could a training feature use information that would not have been available when the prediction was made?
What time leakage means
Time leakage occurs when training data contains future information. The model then learns from clues that will not exist in production. Validation scores may look excellent, but production accuracy falls.
Consider a churn model. You want to predict whether a customer will churn on January 10. A feature showing the customer's activity from January 11 to January 20 cannot be used. That activity happened after the prediction time.
A training row should satisfy this rule:
| Value | Required time relationship |
|---|---|
| Feature availability time | At or before prediction time |
| Label time | After prediction time |
| Future events | Excluded from the feature |
The relevant time is often availability time, not when an event occurred. For example, a transaction may happen at 10:00, but a batch process may publish it at 10:30. A prediction made at 10:15 cannot use that transaction.
Build a point-in-time-correct training set
An as-of join attaches the latest feature value available at or before each label time. The feature value must be ordered by time before the join.
import pandas as pd
labels = pd.DataFrame({
"user_id": [1, 1],
"prediction_time": pd.to_datetime(["2026-01-10", "2026-01-20"]),
"churned": [0, 1],
}).sort_values("prediction_time")
features = pd.DataFrame({
"user_id": [1, 1, 1],
"available_time": pd.to_datetime([
"2026-01-05", "2026-01-15", "2026-01-25"
]),
"logins_30d": [12, 7, 2],
}).sort_values("available_time")
training_set = pd.merge_asof(
labels,
features,
left_on="prediction_time",
right_on="available_time",
by="user_id",
direction="backward",
)
print(training_set[
["prediction_time", "available_time", "logins_30d", "churned"]
])For the January 10 row, the join selects 12 logins from January 5. For the January 20 row, it selects 7 logins from January 15. The January 25 value is excluded because it was not available yet.
Common leakage mistakes
- Computing a customer’s lifetime total using all records, including records after the prediction time.
- Joining the latest customer profile to every historical label.
- Using a feature created after the label event.
- Splitting randomly across time when future records influence past feature calculations.
- Ignoring delays between an event and its arrival in the feature system.
Use time-based validation when possible. Train on earlier periods and validate on later periods. This better matches production behavior.
Exercise
A prediction is made at 10:00. An event happened at 09:55 but became available to the model at 10:10. Can the feature use it?
Show answer
No. The event's availability time is after the prediction time, so using it would create time leakage.