tubular.dates.DatetimeInfoExtractor

class tubular.dates.DatetimeInfoExtractor(columns, include=['timeofday', 'timeofmonth', 'timeofyear', 'dayofweek'], datetime_mappings={}, **kwargs)[source]

Bases: tubular.base.BaseTransformer

Transformer to extract various features from datetime var.

Parameters
  • columns (str or list) – datetime columns to extract information from

  • include (list of str, default = ["timeofday", "timeofmonth", "timeofyear", "dayofweek"]) – Which datetime categorical information to extract

  • datetime_mappings (dict, default = {}) –

    Optional argument to define custom mappings for datetime values. Keys of the dictionary must be contained in include All possible values of each feature must be included in the mappings, ie, a mapping for dayofweek must include all values 0-6; datetime_mappings = {“dayofweek”: {“week”: [0, 1, 2, 3, 4],

    ”weekend”: [5, 6]}}

    The values for the mapping array must be iterable; datetime_mappings = {“timeofday”: {“am”: range(0, 12),

    ”pm”: range(12, 24)}}

    The required ranges for each mapping are:

    timeofday: 0-23 timeofmonth: 1-31 timeofyear: 1-12 dayofweek: 0-6

    If in include but no mappings provided default values will be used as follows:
    timeofday_mapping = {

    “night”: range(0, 6), # Midnight - 6am “morning”: range(6, 12), # 6am - Noon “afternoon”: range(12, 18), # Noon - 6pm “evening”: range(18, 24), # 6pm - Midnight

    } timeofmonth_mapping = {

    ”start”: range(0, 11), “middle”: range(11, 21), “end”: range(21, 32),

    } timeofyear_mapping = {

    ”spring”: range(3, 6), # Mar, Apr, May “summer”: range(6, 9), # Jun, Jul, Aug “autumn”: range(9, 12), # Sep, Oct, Nov “winter”: [12, 1, 2], # Dec, Jan, Feb

    } dayofweek_mapping = {

    ”monday”: [0], “tuesday”: [1], “wednesday”: [2], “thursday”: [3], “friday”: [4], “saturday”: [5], “sunday”: [6],

    }

**kwargs

Arbitrary keyword arguments passed onto BaseTransformer.init method.

include

Which datetime categorical information to extract

Type

list of str, default = [“timeofday”, “timeofmonth”, “timeofyear”, “dayofweek”]

datetime_mappings

Optional argument to define custom mappings for datetime values.

Type

dict, default = {}

__init__(columns, include=['timeofday', 'timeofmonth', 'timeofyear', 'dayofweek'], datetime_mappings={}, **kwargs)None[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(columns[, include, datetime_mappings])

Initialize self.

check_is_fitted(attribute)

Check if particular attributes are on the object.

check_weights_column(X, weights_column)

Helper method for validating weights column in dataframe.

classname()

Method that returns the name of the current class when called.

columns_check(X)

Method to check that the columns attribute is set and all values are present in X.

columns_set_or_check(X)

Function to check or set columns attribute.

fit(X[, y])

Base transformer fit method, checks X and y types.

fit_transform(X[, y])

Fit to data, then transform it.

get_params([deep])

Get parameters for this estimator.

set_params(**params)

Set the parameters of this estimator.

transform(X)

Transform - Extracts new features from datetime variables.

check_is_fitted(attribute)

Check if particular attributes are on the object. This is useful to do before running transform to avoid trying to transform data without first running the fit method.

Wrapper for utils.validation.check_is_fitted function.

Parameters

attributes (List) – List of str values giving names of attribute to check exist on self.

static check_weights_column(X, weights_column)

Helper method for validating weights column in dataframe.

X (pd.DataFrame): df containing weight column weights_column (str): name of weight column

classname()

Method that returns the name of the current class when called.

columns_check(X)

Method to check that the columns attribute is set and all values are present in X.

Parameters

X (pd.DataFrame) – Data to check columns are in.

columns_set_or_check(X)

Function to check or set columns attribute.

If the columns attribute is None then set it to all columns in X. Otherwise run the columns_check method.

Parameters

X (pd.DataFrame) – Data to check columns are in.

fit(X, y=None)

Base transformer fit method, checks X and y types. Currently only pandas DataFrames are allowed for X and DataFrames or Series for y.

Fit calls the columns_set_or_check method which will set the columns attribute to all columns in X, if it is None.

Parameters
  • X (pd.DataFrame) – Data to fit the transformer on.

  • y (None or pd.DataFrame or pd.Series, default = None) – Optional argument only required for the transformer to work with sklearn pipelines.

fit_transform(X, y=None, **fit_params)

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
  • X (array-like of shape (n_samples, n_features)) – Input samples.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs), default=None) – Target values (None for unsupervised transformations).

  • **fit_params (dict) – Additional fit parameters.

Returns

X_new – Transformed array.

Return type

ndarray array of shape (n_samples, n_features_new)

get_params(deep=True)

Get parameters for this estimator.

Parameters

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns

params – Parameter names mapped to their values.

Return type

dict

set_params(**params)

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

**params (dict) – Estimator parameters.

Returns

self – Estimator instance.

Return type

estimator instance

transform(X)[source]

Transform - Extracts new features from datetime variables.

Parameters

X (pd.DataFrame) – Data with columns to extract info from.

Returns

X – Transformed input X with added columns of extracted information.

Return type

pd.DataFrame