DatetimeFeatures#

class feature_engine.datetime.DatetimeFeatures(variables=None, features_to_extract=None, drop_original=True, missing_values='raise', dayfirst=False, yearfirst=False, utc=None)[source]#

DatetimeFeatures extracts date and time features from datetime variables, adding new columns to the dataset. DatetimeFeatures can extract datetime information from existing datetime or object-like variables or from the dataframe index.

DatetimeFeatures uses pandas.to_datetime to convert object variables to datetime and pandas.dt to extract the features from datetime.

The transformer supports the extraction of the following features:

  • “month”

  • “quarter”

  • “semester”

  • “year”

  • “week”

  • “day_of_week”

  • “day_of_month”

  • “day_of_year”

  • “weekend”

  • “month_start”

  • “month_end”

  • “quarter_start”

  • “quarter_end”

  • “year_start”

  • “year_end”

  • “leap_year”

  • “days_in_month”

  • “hour”

  • “minute”

  • “second”

More details in the User Guide.

Parameters
variables: str, list, default=None

List with the variables from which date and time information will be extracted. If None, the transformer will find and select all datetime variables, including variables of type object that can be converted to datetime. If “index”, the transformer will extract datetime features from the index of the dataframe.

features_to_extract: list, default=None

The list of date features to extract. If None, the following features will be extracted: “month”, “year”, “day_of_week”, “day_of_month”, “hour”, “minute” and “second”. If “all”, all supported features will be extracted. Alternatively, you can pass a list with the names of the features you want to extract.

drop_original: bool, default=”True”

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

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 performing the feature extraction. Missing data is only evaluated in the variables that will be used to derive the date and time features. If features are derived from the dataframe index, missing data will be checked in the index.

dayfirst: bool, default=”False”

Specify a date parse order if arg is str or is list-like. If True, parses dates with the day first, e.g. 10/11/12 is parsed as 2012-11-10. Same as in pandas.to_datetime.

yearfirst: bool, default=”False”

Specify a date parse order if arg is str or is list-like. Same as in pandas.to_datetime.

  • If True parses dates with the year first, e.g. 10/11/12 is parsed as 2010-11-12.

  • If both dayfirst and yearfirst are True, yearfirst is preceded.

utc: bool, default=None

Return UTC DatetimeIndex if True (converting any tz-aware datetime.datetime objects as well). Same as in pandas.to_datetime.

Attributes
variables_:

List of variables from which date and time features will be extracted. If None, features will be extracted from the dataframe index.

features_to_extract_:

The date and time features that will be extracted from each variable or the index.

feature_names_in_:

List with the names of features seen during fit.

n_features_in_:

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

See also

pandas.to_datetime
pandas.dt

Methods

fit:

This transformer does not learn parameters.

fit_transform:

Fit to data, then transform it.

get_feature_names_out:

Get output feature names for transformation.

get_params:

Get parameters for this estimator.

set_params:

Set the parameters of this estimator.

transform:

Add the date and time features.

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

This transformer does not learn any parameter.

Finds datetime variables or checks that the variables selected by the user can be converted to datetime.

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

The training input samples. Can be the entire dataframe, not just the variables to transform.

y: pandas Series, default=None

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

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.

Parameters
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).

**fit_paramsdict

Additional fit parameters.

Returns
X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_feature_names_out(input_features=None)[source]#

Get output feature names for transformation.

Parameters
input_features: str, list, default=None

Input features. If None, then the names of all the variables in the transformed dataset (original + new variables) is returned. If ‘index’, then the name of the features created from the index is returned. If list with feature names, the names for the datetime features derived from the variables in the list will be returned.”

Returns
feature_names_out: list

The feature names.

:rtype:py:class:~typing.List
get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters
deepbool, default=True

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

Returns
paramsdict

Parameter names mapped to their values.

set_params(**params)[source]#

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.

Parameters
**paramsdict

Estimator parameters.

Returns
selfestimator instance

Estimator instance.

transform(X)[source]#

Extract the date and time features and add them to the dataframe.

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

The data to transform.

Returns
X_new: Pandas dataframe, shape = [n_samples, n_features x n_df_features]

The dataframe with the original variables plus the new variables.

:rtype:py:class:~pandas.core.frame.DataFrame