Skip to content

Pipeline

polars_ts.pipeline

End-to-end ML forecasting pipeline.

Ties feature engineering and target transforms into a single fit/predict interface. Implements Ch 8 of "Modern Time Series Forecasting with Python" (2nd Ed.).

ForecastPipeline

End-to-end ML forecasting pipeline with feature engineering and transforms.

Combines lag features, rolling aggregations, calendar features, Fourier terms, and optional target transforms into a single fit/predict workflow.

Parameters

estimator A scikit-learn-compatible estimator with fit and predict. lags Lag offsets for lag features (e.g. [1, 2, 7]). rolling_windows Window sizes for rolling aggregations (e.g. [7, 14]). rolling_aggs Aggregation functions for rolling features (default ["mean"]). calendar Calendar features to extract (e.g. ["day_of_week", "month"]). fourier Fourier term specs as [(period, n_harmonics), ...]. target_transform Optional transform: "log", "boxcox", or "difference". transform_kwargs Arguments passed to the transform function (e.g. {"lam": 0.5}). past_covariates Column names of time-varying covariates known only up to the present. Lagged features are created automatically. future_covariates Column names of time-varying covariates known into the future (e.g. holidays, promotions). Values for the forecast horizon must be supplied in future_df at predict time. past_covariate_lags Lag offsets for past covariate features. Defaults to lags. target_col Column with the target values. id_col Column identifying each time series. time_col Column with timestamps.

fit(df)

Fit the pipeline: transform target, build features, train model.

Parameters

df Training DataFrame with id_col, time_col, target_col, and any covariate columns.

Returns

ForecastPipeline Fitted pipeline (self).

predict(df, h, future_df=None)

Generate h-step-ahead forecasts using recursive prediction.

Parameters

df DataFrame containing history to predict from. h Forecast horizon. future_df DataFrame with future covariate values for the forecast horizon. Required when future_covariates were specified at construction. Must contain id_col, time_col, and all future covariate columns.

Returns

pl.DataFrame DataFrame with columns [id_col, time_col, "y_hat"].

_build_feature_df(df, lags, rolling_windows, rolling_aggs, calendar, fourier, target_col, id_col, time_col, past_covariates=None, past_covariate_lags=None, future_covariates=None)

Apply all configured feature engineering steps to df.

_apply_transform(df, transform, kwargs, target_col, id_col, time_col)

Apply a target transform and return (transformed_df, state).

_build_step_features(buffer, timestamp, lags, rolling_windows, rolling_aggs, calendar, fourier_specs, step_index, _time_col, past_covariate_buffers=None, past_covariate_lags=None, future_covariate_values=None)

Build a single feature vector for one recursive prediction step.