time series split
**Time Series Cross-Validation** is a **specialized evaluation strategy for temporal data that respects chronological order** — training only on past data and testing on future data in each fold, because standard K-Fold cross-validation shuffles data randomly and would use "future" information to predict "the past" (a devastating form of data leakage that produces impossibly optimistic performance estimates for stock prices, sales forecasts, weather predictions, and any time-dependent prediction task).
**What Is Time Series Cross-Validation?**
- **Definition**: A cross-validation approach (also called "walk-forward validation" or "rolling origin") where each fold uses an expanding or sliding training window of past data and tests on the immediately following future period — never allowing future data to inform past predictions.
- **Why Standard K-Fold Fails**: K-Fold randomly shuffles data into folds. For time series, this means 2025 data could be in training while 2024 data is in testing — the model literally uses the future to predict the past. This leakage produces accuracy estimates that are impossible to achieve in real deployment.
- **The Golden Rule**: "You cannot use tomorrow's data to predict today."
**Standard K-Fold vs Time Series Split**
| Aspect | Standard K-Fold | Time Series Split |
|--------|----------------|-------------------|
| **Data order** | Shuffled randomly | Chronological order preserved |
| **Future in training?** | Yes ⚠️ (data leakage) | Never ✓ |
| **Training window** | Random subset | Past data only (expanding or sliding) |
| **Test window** | Random subset | Next future period only |
**How Time Series Split Works (Expanding Window)**
| Fold | Training Data | Test Data | Gap |
|------|-------------|-----------|-----|
| 1 | Jan - Mar | Apr | None |
| 2 | Jan - Apr | May | None |
| 3 | Jan - May | Jun | None |
| 4 | Jan - Jun | Jul | None |
| 5 | Jan - Jul | Aug | None |
Training window expands each fold. The model always predicts the next unseen period.
**Sliding Window Variant**
| Fold | Training Data | Test Data | Window Size |
|------|-------------|-----------|-------------|
| 1 | Jan - Mar | Apr | 3 months |
| 2 | Feb - Apr | May | 3 months |
| 3 | Mar - May | Jun | 3 months |
| 4 | Apr - Jun | Jul | 3 months |
Fixed-size training window — older data drops off, simulating concept drift.
**Python Implementation**
```python
from sklearn.model_selection import TimeSeriesSplit
tscv = TimeSeriesSplit(n_splits=5, gap=0)
for train_idx, test_idx in tscv.split(X):
X_train, X_test = X[train_idx], X[test_idx]
y_train, y_test = y[train_idx], y[test_idx]
# With gap (e.g., 7-day gap to prevent immediate correlation leakage)
tscv = TimeSeriesSplit(n_splits=5, gap=7)
```
**The Gap Parameter**
| Gap | Purpose | Use Case |
|-----|---------|----------|
| **gap=0** | Test immediately after training period | Standard forecasting |
| **gap=7** | 7-day buffer between train and test | Avoid autocorrelation from consecutive days |
| **gap=30** | 30-day buffer | Monthly forecasting with weekly seasonality |
**Common Applications**
| Domain | Time Unit | Typical Setup |
|--------|-----------|---------------|
| **Stock Prediction** | Daily | Train on 2 years, test on next month |
| **Sales Forecasting** | Weekly | Train on 52 weeks, test on next 4 weeks |
| **Weather** | Hourly | Train on 6 months, test on next week |
| **Demand Planning** | Daily | Expanding window, 1-week test horizon |
**Time Series Cross-Validation is the only correct evaluation strategy for temporal data** — respecting the chronological ordering that standard K-Fold violates, preventing the future-to-past data leakage that produces unrealistically optimistic performance estimates, and simulating the real deployment scenario where models must always predict from historically available data.