A model that scores well on a random split can still fail in the field, and time series is where that gap is easiest to create by accident. This reading review covers two scikit-learn documentation pages, the cross-validation page and the common pitfalls page, and pulls out what they say about splitting data that has an order, and about the preprocessing steps that leak the future into the training set.

1. Random k-fold assumes something time series does not have (cross-validation page)

The page starts from the definition: learning parameters and testing on the same data is a methodological mistake, because a model that repeats the labels it has seen would score perfectly and predict nothing. Holding out data is the fix.

Then it turns to time series and names the problem. Time series data is characterized by autocorrelation, the correlation between observations near in time. Classical techniques like KFold and ShuffleSplit assume the samples are independent and identically distributed, and on time series that assumption produces what the page calls unreasonable correlation between training and testing instances, which yields poor estimates of generalization error. Its prescription is to evaluate on the "future" observations least like those used to train the model, and it offers TimeSeriesSplit as one way to do that.

Lesson: a shuffled split on ordered data answers a question nobody asked, namely how well the model predicts a point from the middle of a series it has already seen around.

2. What TimeSeriesSplit actually returns (same page)

The page describes TimeSeriesSplit as a variation of k-fold that returns the first k folds as the training set and the (k+1)th fold as the test set. Unlike standard cross-validation, successive training sets are supersets of the ones before them, and any surplus data goes into the first training partition, which is always used for training.

It also gives a constraint that is easy to skip: the class can be used for samples observed at fixed time intervals, and the folds must represent the same duration, otherwise the metrics across folds are not comparable. The printed default is TimeSeriesSplit(gap=0, max_train_size=None, n_splits=3, test_size=None), and the page's example runs three splits over six samples.

Lesson: the expanding window is the point. Each fold tests on a later block than it trains on, which is the closest a small dataset gets to the real deployment condition.

3. Leakage is usually a preprocessing decision (common pitfalls page)

The pitfalls page defines data leakage as using information that would not be available at prediction time when building the model, and says the result is overly optimistic performance estimates, for example from cross-validation, followed by poorer performance on genuinely new data.

It names the common cause plainly: not keeping the test and train subsets separate, and it states the rule as never calling fit on the test data. The subtlety is that both subsets need the same transformation, but the transformation must be learnt only from the training data. The page's example is a normalization step that divides by an average: that average has to come from the training subset, because an average over all the data lets the test subset influence the model.

Its remedies are ordered: split first, particularly before any preprocessing; use fit_transform on the training subset and transform on the test subset; and use a Pipeline, which the page says makes it easier to chain transformations and reduces the chance of forgetting one, and which also helps avoid leaking test data into training.

Lesson: a scaler, an imputer or a feature selector fitted before the split has already read the test rows, and no cross-validation scheme can undo that.

4. The two traps together

Put the pages side by side and one failure mode stands out: fitting a preprocessing step on the whole series and then running TimeSeriesSplit on the result. The splitter is then honest about time order, and the scaler has still seen the future, which is exactly the leakage the pitfalls page warns about. The docs' own advice composes: split first, fit only on the training part of each fold, and keep the transformation inside a pipeline so it travels with the estimator.

Checklist from the sources

  • Do not use KFold or ShuffleSplit on autocorrelated data; the page says the resulting estimate is unreasonable.
  • Use TimeSeriesSplit when the goal is predicting later observations, and keep folds the same duration so metrics compare.
  • Split before any preprocessing step.
  • Call fit_transform on train and transform on test, never fit on test.
  • Put scalers, imputers and feature selection inside a Pipeline so each fold refits them.
  • Treat a very good cross-validation score on time series as a reason to check the split before celebrating it.

This is a documentation review, not a benchmark. It reports no score, dataset or model result of its own.

Sources: