How to Split Time Series Data into Training and Test Sets by Date
When data happens over time, such as sales, bookings or sensor readings, the test set has to come after the training set. A random split mixes the days, so the model learns from dates later than the ones it's tested on, and its score looks better than it will be in real use. Splitting at a cutoff date fixes that. This guide does it with pandas 3.1.2, and shows the random split's mix-up next to it.
Syntax
cutoff = pd.Timestamp("<YYYY-MM-DD>")
train = <table>[<table>["<date column>"] <= cutoff]
test = <table>[<table>["<date column>"] > cutoff]
| Part | What it means |
|---|---|
cutoff | The last day of the training period |
<= cutoff | The training rows: everything up to and including the cutoff |
> cutoff | The test rows: everything after it |
scikit-learn's cross-validation guide explains why: time series data is "characterized by the correlation between observations that are near in time", so "it is very important to evaluate our model for time series data on the “future” observations least like those that are used to train the model."
The Example
A shop's daily sales for the first ten days of January 2026. The model should learn from the first seven days and be tested on the last three.
Prerequisites
- pandas in your project, for example with
uv add pandas. This guide uses pandas 3.1.2 on Python 3.15.2, and scikit-learn 1.9.1 for the comparison.
Step 1: Write the Script
import pandas as pd
sales = pd.DataFrame(
{
"day": pd.date_range("2026-01-01", periods=10, freq="D"),
"units": [5, 7, 6, 9, 11, 10, 12, 14, 13, 15],
}
)
cutoff = pd.Timestamp("2026-01-07")
train = sales[sales["day"] <= cutoff]
test = sales[sales["day"] > cutoff]
print("Train:", train["day"].min().date(), "to", train["day"].max().date(), f"({len(train)} rows)")
print("Test: ", test["day"].min().date(), "to", test["day"].max().date(), f"({len(test)} rows)")

Step 2: Run It
uv run python split_by_date.py
Train: 2026-01-01 to 2026-01-07 (7 rows)
Test: 2026-01-08 to 2026-01-10 (3 rows)

Every training day comes before every test day: the model will be tested on days it couldn't have seen, as it will be when it's really used. The command works unchanged in PowerShell.
What a Random Split Does
scikit-learn's train_test_split shuffles rows by default. On the same ten days, train_test_split(sales, test_size=0.3, random_state=0) gave:
Train days: [1, 2, 4, 6, 7, 8, 10]
Test days: [3, 5, 9]
The model would train on 8 and 10 January and be tested on 3 January: it has seen the future. With shuffle=False, train_test_split keeps the order and gives the same split as the cutoff, days 1 to 7 for training and 8 to 10 for testing. Its reference describes the option: "Whether or not to shuffle the data before splitting."
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| A random split on data over time | Training days come after test days, so the test score is too good | Split at a cutoff date |
Rows in a mixed-up order when using shuffle=False | It splits by position, not by date | Sort by the date first, or use a cutoff date |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The model scores well in testing but worse in real use | The test set overlapped in time with the training set | Check that the test set's first day is after the training set's last day |
FAQ
How do I choose the cutoff?
Keep enough of the latest period for testing to cover what matters, such as a full week or month, and train on everything before it.
What about cross-validation?
scikit-learn's TimeSeriesSplit makes several train-and-test splits, each one testing on later data than it trains on.
Conclusion
Splitting at 2026-01-07 trained on 1 to 7 January and tested on 8 to 10 January, while a random split tested on 3 January after training on 8 and 10. For data that happens over time, always test on what comes after the training period.
Additional Resources
- Cross-validation of time series data — why and how.
- train_test_split — the
shuffleoption.
