Skip to main content

How to Split Time Series Data into Training and Test Sets by Date

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
cutoff = pd.Timestamp("<YYYY-MM-DD>")
train = <table>[<table>["<date column>"] <= cutoff]
test = <table>[<table>["<date column>"] > cutoff]
PartWhat it means
cutoffThe last day of the training period
<= cutoffThe training rows: everything up to and including the cutoff
> cutoffThe 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​

split_by_date.py
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)")

The split_by_date.py script in a macOS-style code window: ten days of sales, a cutoff of 2026-01-07, train as the rows up to the cutoff and test as the rows after it, and two prints of each part's first and last day.

Step 2: Run It​

Terminal
uv run python split_by_date.py
Output
Train: 2026-01-01 to 2026-01-07 (7 rows)
Test: 2026-01-08 to 2026-01-10 (3 rows)

The terminal after 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:

Output
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​

MistakeWhat happensDo this instead
A random split on data over timeTraining days come after test days, so the test score is too goodSplit at a cutoff date
Rows in a mixed-up order when using shuffle=FalseIt splits by position, not by dateSort by the date first, or use a cutoff date

Troubleshooting​

SymptomLikely causeFix
The model scores well in testing but worse in real useThe test set overlapped in time with the training setCheck 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​