class feature_engine.timeseries.forecasting.LagFeatures(variables=None, periods=1, freq=None, fill_value=None, sort_index=True, missing_values='raise', drop_original=False, drop_na=False)[source]#

LagFeatures adds lag features to the dataframe. A lag feature is a feature with information about a prior time step.

LagFeatures has the same functionality as pandas shift() with the exception that only one of periods or freq can be indicated at a time. LagFeatures builds on top of pandas shift() in that multiple lags can be created at the same time and the features with names will be concatenated to the original dataframe.

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

LagFeatures works only with numerical variables. You can pass a list of variables to lag. Alternatively, LagFeatures will automatically select and lag all numerical variables found 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.

periods: int, list of ints, default=1

Number of periods to shift. Can be a positive integer or list of positive integers. If list, features will be created for each one of the periods in the list. If the parameter freq is specified, periods will be ignored.

freq: str, list of str, default=None

Offset to use from the tseries module or time rule. See parameter freq in pandas shift(). It is the same functionality. If freq is a list, lag features will be created for each one of the frequency values in the list. If freq is not None, then this parameter overrides the parameter periods.

fill_value: object, optional

The scalar value to use for newly introduced missing values. The default depends on the dtype of the variable. For numeric data, np.nan is used. For datetime, timedelta, or period data, NaT is used. For extension dtypes, self.dtype.na_value is used.

sort_index: bool, default=True

Whether to order the index of the dataframe before creating the lag features.

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.

drop_na: bool, default=False.

Whether the NAN introduced in the lag features should be removed.


The group of variables that will be lagged.


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 LagFeatures
>>> 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]
>>>                     ))
>>> lf = LagFeatures(periods=[1,2])
>>> lf.fit_transform(X)
            date  x1  x2  x1_lag_1  x2_lag_1  x1_lag_2  x2_lag_2
0  2022-09-18   1   6       NaN       NaN       NaN       NaN
1  2022-09-19   2   7       1.0       6.0       NaN       NaN
2  2022-09-20   3   8       2.0       7.0       1.0       6.0
3  2022-09-21   4   9       3.0       8.0       2.0       7.0
4  2022-09-22   5  10       4.0       9.0       3.0       8.0



This transformer does not learn parameters.


Fit to data, then transform it.


Get output feature names for transformation.


Get parameters for this estimator.


Set the parameters of this estimator.


Add lag features.


Remove rows with missing data from X and y.

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 lag features.

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

The data to transform.

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

The dataframe with the original plus the new variables.


DataFrame ..

transform_x_y(X, y)[source]#

Transform, align and adjust both X and y based on the transformations applied to X, ensuring that they correspond to the same set of rows if any were removed from X.

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

The dataframe to transform.

y: pandas Series or Dataframe of length = n_samples

The target variable to transform. Can be multi-output.

X_new: pandas dataframe

The transformed dataframe of shape [n_samples - n_rows, n_features]. It may contain less rows than the original dataset.

y_new: pandas Series or DataFrame

The transformed target variable of length [n_samples - n_rows]. It contains as many rows as those left in X_new.