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 is able to extract datetime information from existing datetime or object-like variables.
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: list, default=None
The list of variables to extract date and time features from. If None, the transformer will find and select all datetime variables, including variables of type object that can be converted to datetime.
- 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 supported 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
ortransform
contain missing values. If ‘ignore’, missing data will be ignored when performing the feature extraction.- dayfirst: bool, default=”False”
Specify a date parse order if arg is str or its list-likes. If True, parses dates with the day first, eg 10/11/12 is parsed as 2012-11-10.
- yearfirst: bool, default=”False”
Specify a date parse order if arg is str or its list-likes.
If True parses dates with the year first, eg 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).
- Attributes
- variables_:
List of variables from which date and time features will be extracted.
- features_to_extract_:
The date and time features that will be extracted from each variable.
- 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.
transform:
Add the date and time features.
fit_transform:
Fit to the data, then transform it.
- 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
andy
with optional parametersfit_params
and returns a transformed version ofX
.- 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_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