class feature_engine.timeseries.forecasting.ExpandingWindowFeatures(variables=None, min_periods=None, functions='mean', periods=1, freq=None, sort_index=True, missing_values='raise', drop_original=False)[source]#

ExpandingWindowFeatures adds new features to a dataframe based on expanding window operations. Expanding window operations are operations that perform an aggregation over an expanding window of all past values relative to the value of interest. An expanding window feature is, in other words, a feature created after computing statistics (e.g., mean, min, max, etc.) using a window over all the past data. For example, the mean value of all months prior to the month of interest is an expanding window feature.

ExpandingWindowFeatures uses the pandas’ functions expanding(), agg() and shift(). With expanding(), it creates expanding windows. With agg() it applies multiple functions within those windows. With ‘shift()’ it allocates the values to the correct rows.

For supported aggregation functions, see Expanding Window Functions.

To be compatible with ExpandingWindowFeatures, the dataframe’s index must have unique values and no NaN.

ExpandingWindowFeatures works only with numerical variables. You can pass a list of variables to use as input for the expanding window. Alternatively, ExpandingWindowFeatures will automatically select all numerical variables in the training set.

More details in the User Guide.

variables: list, default=None

The list of numerical variables to transform. If None, the transformer will automatically find and select all numerical variables.

min_periods: int, default None.

Minimum number of observations in window required to have a value; otherwise, result is np.nan. See parameter min_periods in the pandas expanding() documentation for more details.

functions: str, list of str, default = ‘mean’

The functions to apply within the window. Valid functions can be found here.

periods: int, list of ints, default=1

Number of periods to shift. Can be a positive integer. See param periods in pandas shift.

freq: str, list of str, default=None

Offset to use from the tseries module or time rule. See parameter freq in pandas shift().

sort_index: bool, default=True

Whether to order the index of the dataframe before creating the expanding window feature.

missing_values: string, default=’raise’

Indicates if missing values should be ignored or raised. If 'raise' the transformer will return an error if the the datasets to fit or transform contain missing values. If 'ignore', missing data will be ignored when learning parameters or performing the transformation.

drop_original: bool, default=False

If True, the original variables to transform will be dropped from the dataframe.


The group of variables that will be used to create the expanding window features.


List with the names of features seen during fit.


The number of features in the train set used in fit.

See also



>>> import pandas as pd
>>> from feature_engine.timeseries.forecasting import ExpandingWindowFeatures
>>> X = pd.DataFrame(dict(date = ["2022-09-18",
>>>                               "2022-09-19",
>>>                               "2022-09-20",
>>>                               "2022-09-21",
>>>                               "2022-09-22"],
>>>                       x1 = [1,2,3,4,5],
>>>                       x2 = [6,7,8,9,10]
>>>                     ))
>>> ewf = ExpandingWindowFeatures()
>>> ewf.fit_transform(X)
         date  x1  x2  x1_expanding_mean  x2_expanding_mean
0  2022-09-18   1   6                NaN                NaN
1  2022-09-19   2   7                1.0                6.0
2  2022-09-20   3   8                1.5                6.5
3  2022-09-21   4   9                2.0                7.0
4  2022-09-22   5  10                2.5                7.5



This transformer does not learn parameters.


Add expanding window features.


Fit to data, then transform it.


Get output feature names for transformation.


Get parameters for this estimator.


Set the parameters of this estimator.

fit(X, y=None)[source]#

This transformer does not learn parameters.

X: pandas dataframe of shape = [n_samples, n_features]

The training dataset.

y: pandas Series, default=None

y is not needed in this transformer. You can pass None or y.

fit_transform(X, y=None, **fit_params)[source]#

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Xarray-like of shape (n_samples, n_features)

Input samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).


Additional fit parameters.

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.


Get output feature names for transformation. In other words, returns the variable names of transformed dataframe.

input_featuresarray or list, default=None

This parameter exits only for compatibility with the Scikit-learn pipeline.

  • If None, then feature_names_in_ is used as feature names in.

  • If an array or list, then input_features must match feature_names_in_.

feature_names_out: list

Transformed feature names.


List[Union[str, int]] ..


Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.


A MetadataRequest encapsulating routing information.


Get parameters for this estimator.

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.


Parameter names mapped to their values.


Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.


Estimator parameters.

selfestimator instance

Estimator instance.


Adds expanding window features.

X: pandas dataframe of shape = [n_samples, n_features]

The data to transform.

X_new: Pandas dataframe, shape = [n_samples, n_features + window_features]

The dataframe with the original plus the new variables.


DataFrame ..