TimeSliceCrossValidator.split#

TimeSliceCrossValidator.split(X, y=None)[source]#

Generate train/test indices for each time-slice split.

This implementation selects rows by date masks so that all coordinate levels (e.g., multiple geos) for the selected date ranges are included in each fold. It returns integer positions suitable for use with DataFrame.iloc.

Parameters:
Xpd.DataFrame

Feature matrix containing the date column.

ypd.Series, optional

Target variable. Not used but included for scikit-learn API compatibility.

Yields:
train_idxnp.ndarray

Integer indices for training rows in this fold.

test_idxnp.ndarray

Integer indices for test rows in this fold.

Raises:
ValueError

If no splits are possible with the given parameters.

Examples

>>> cv = TimeSliceCrossValidator(
...     n_init=10, forecast_horizon=5, date_column="date"
... )
>>> for train_idx, test_idx in cv.split(X, y):
...     X_train, X_test = X.iloc[train_idx], X.iloc[test_idx]
...     y_train, y_test = y.iloc[train_idx], y.iloc[test_idx]