Train-Test Splitting for Time Series
What you'll learn: How to properly split time series data for training and testing while respecting temporal order.
Why Time Series is Different
Unlike traditional machine learning where you can randomly shuffle rows, time series data has a temporal order that must be preserved. Shuffling would leak future information into your training set, creating unrealistic performance estimates.
Think of it like studying for an exam: you can't learn from tomorrow's test questions to prepare for today's quiz. In time series, you must always train on the past and test on the future.
Sequential Splitting
To split time series data correctly:
- Choose a cutoff point in time (e.g., 80% of your data)
- Train set: all observations before the cutoff
- Test set: all observations after the cutoff
For example, if you have daily sales data from January to December, train on January–September and test on October–December. No shuffling, no random sampling across time.
Walk-Forward Validation
A more robust approach is walk-forward validation (also called rolling-window validation). Instead of a single split:
- Train on data up to time t
- Test on time t+1
- Move forward one step
- Retrain on data up to t+1
- Test on t+2
- Repeat
This mimics real-world forecasting where you continuously update your model as new data arrives, providing a more realistic assessment of how your model will perform in production.
Respecting Temporal Order
Never let your model peek into the future. Every observation used for training must come strictly before every observation used for testing. This prevents temporal leakage, which would artificially inflate your accuracy metrics.
Key Takeaway: In time series, always split sequentially—train on past data, test on future data—and consider walk-forward validation for more realistic performance estimates.