DatetimeOrdinal#
- class feature_engine.datetime.DatetimeOrdinal(variables=None, missing_values='raise', start_date=None, drop_original=True)[source]#
DatetimeOrdinal transforms datetime variables into their ordinal representation. The ordinal representation is an integer value representing the number of days since January 1, 0001 in the Gregorian calendar.
Optionally, a
start_datecan be provided to set a custom reference point, making the ordinal values relative to this date (starting from 1).More details in the User Guide.
- Parameters
- variables: str, list, default=None
List of the variables to convert into ordinal. If None, the transformer will find and select all datetime variables, including variables of type object that can be converted to datetime.
- missing_values: string, default=’raise’
Indicates if missing values should be ignored or raised. If ‘raise’ the transformer will return an error if the datasets passed to
fitortransformcontain missing values. If ‘ignore’, missing data will be ignored when performing the transformation.- start_date: str, datetime.datetime, default=None
A reference date from which the ordinal values will be calculated. If provided, the ordinal value of
start_datewill be 1, the day after will be 2, and so on. Days beforestart_datewill take negative values. If None, the transformation will represent the number of days since January 1, 0001.start_datecan be a string (e.g., “YYYY-MM-DD”) or a datetime object.- drop_original: bool, default=True
If True, the original datetime variables will be dropped from the dataframe after the transformation.
- Attributes
- variables_:
List of variables to convert into ordinals.
- start_date_ordinal_:
The ordinal value of the provided
start_date, if applicable.- 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.
Examples
>>> import pandas as pd >>> from feature_engine.datetime import DatetimeOrdinal >>> X = pd.DataFrame(dict(date = ["2023-01-01", "2023-01-02", "2023-01-03"])) >>> dtf = DatetimeOrdinal(start_date="2023-01-01") >>> dtf.fit(X) >>> dtf.transform(X) date_ordinal 0 1 1 2 2 3
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 ordinal datetime 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=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
Xandywith optional parametersfit_paramsand 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. Pass only if the estimator accepts additional params in its
fitmethod.
- 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. In other words, returns the variable names of transformed dataframe.
- Parameters
- input_featuresarray or list, default=None
This parameter exits only for compatibility with the Scikit-learn pipeline.
If
None, thenfeature_names_in_is used as feature names in.If an array or list, then
input_featuresmust matchfeature_names_in_.
- Returns
- feature_names_out: list
Transformed feature names.
- get_metadata_routing()[source]#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns
- routingMetadataRequest
A
MetadataRequestencapsulating routing information.
- 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]#
Calculate ordinal representation of datetime 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 features.
- rtype
DataFrame..