Sub topic 8
Splitting by Time, Not at Random
Assume you know supervised learning, pandas DataFrames, SQL joins, and basic timestamp handling. This chapter focuses on evaluation after you have built point-in-time correct feature rows. The goal is to make validation resemble the future predictions your model will produce.
Why Random Splits Mislead
A random split mixes older and newer observations in both training and validation data. This is reasonable when rows are independent and identically distributed. It is usually wrong for time-dependent prediction.
Suppose you predict whether a customer will churn using a prediction timestamp. A random split might train on a customer’s January and March rows while validating on February. The training data then contains information from after the validation prediction time. Even if each row passed an as-of join, the split still allows the model to learn future behavior patterns.
Production has a one-way information flow:
- Train using data available before a cutoff.
- Receive new data after the cutoff.
- Predict on that later data.
A time-based split preserves this order.
| Split | Time condition | Typical use |
|---|---|---|
| Training | `prediction_ts < cutoff` | Fit model parameters |
| Validation | `prediction_ts >= cutoff` | Choose settings and compare models |
| Test | Later than validation | Final unbiased estimate |
Choosing a Time Split
Choose a cutoff that matches the production question. If the model will be retrained monthly and predict the next month, use a validation period that represents a future month. If you have enough data, reserve a still-later test period that you do not use during model development.
import pandas as pd
data = pd.DataFrame(
{
"customer_id": ["a", "b", "c", "d", "e", "f"],
"prediction_ts": pd.to_datetime(
[
"2025-10-03",
"2025-11-12",
"2025-12-20",
"2026-01-05",
"2026-02-14",
"2026-03-22",
],
utc=True,
),
"avg_spend_30d": [40, 55, 31, 72, 63, 48],
"churned": [0, 1, 0, 1, 0, 1],
}
)
cutoff = pd.Timestamp("2026-01-01", tz="UTC")
test_cutoff = pd.Timestamp("2026-03-01", tz="UTC")
train_df = data[data["prediction_ts"] < cutoff]
valid_df = data[
(data["prediction_ts"] >= cutoff)
& (data["prediction_ts"] < test_cutoff)
]
test_df = data[data["prediction_ts"] >= test_cutoff]
assert train_df["prediction_ts"].max() < valid_df["prediction_ts"].min()
assert valid_df["prediction_ts"].max() < test_df["prediction_ts"].min()The assertions make the intended ordering explicit. They also fail if a later change accidentally overlaps the periods.
Gaps and Label Availability
Labels often become known only after a prediction horizon. For example, a churn label might require observing the customer for 30 days after the prediction timestamp. Do not include rows whose labels were not available when the training job ran.
A gap between training and validation can help when feature pipelines have delays or when nearby observations are strongly related. For example, train through January, skip February, and validate in March. This reduces contamination from records close to the boundary, but it also leaves fewer samples for learning.
Use several forward-looking evaluations when performance may change over time:
- Train on January through June and validate on July.
- Train on January through July and validate on August.
- Train on January through August and validate on September.
This is often called walk-forward validation. It reveals whether performance is stable or depends on one particular period.
What to Report
Report metrics for each time period, not only one overall average. Include the number of examples, positive-label rate, and important data-quality statistics. A sharp change in label rate or feature coverage may explain a metric change better than the model itself.
Keep the final test period untouched until model selection is complete. Repeatedly checking it turns the test set into another validation set.
Exercise
Your training rows end on 2026-04-30, and validation rows begin on 2026-04-15. What is wrong, and how should you fix it?
Show answer
The periods overlap. Choose a cutoff so every training prediction timestamp is earlier than every validation prediction timestamp, then verify the ordering with assertions.