tubular.dates.DatetimeSinusoidCalculator

class tubular.dates.DatetimeSinusoidCalculator(columns: Union[str, List[str]], method: Union[str, List[str]], units: Union[str, dict], period: Union[int, float, dict] = 6.283185307179586)[source]

Bases: tubular.base.BaseTransformer

Transformer to derive a feature in a dataframe by calculating the sine or cosine of a datetime column in a given unit (e.g hour), with the option to scale period of the sine or cosine to match the natural period of the unit (e.g. 24).

Parameters
  • columns (str or list) – Columns to take the sine or cosine of. Must be a datetime[64] column.

  • method (str or list) – Argument to specify which function is to be calculated. Accepted values are ‘sin’, ‘cos’ or a list containing both.

  • units (str or dict) – Which time unit the calculation is to be carried out on. Accepted values are ‘year’, ‘month’, ‘day’, ‘hour’, ‘minute’, ‘second’, ‘microsecond’. Can be a string or a dict containing key-value pairs of column name and units to be used for that column.

  • period (int, float or dict, default = 2*np.pi) – The period of the output in the units specified above. To leave the period of the sinusoid output as 2 pi, specify 2*np.pi (or leave as default). Can be a string or a dict containing key-value pairs of column name and period to be used for that column.

columns

Columns to take the sine or cosine of.

Type

str or list

method

The function to be calculated; either sin, cos or a list containing both.

Type

str or list

units

Which time unit the calculation is to be carried out on. Will take any of ‘year’, ‘month’, ‘day’, ‘hour’, ‘minute’, ‘second’, ‘microsecond’. Can be a string or a dict containing key-value pairs of column name and units to be used for that column.

Type

str or dict

period

The period of the output in the units specified above. Can be a string or a dict containing key-value pairs of column name and units to be used for that column.

Type

str, float or dict, default = 2*np.pi

__init__(columns: Union[str, List[str]], method: Union[str, List[str]], units: Union[str, dict], period: Union[int, float, dict] = 6.283185307179586)None[source]

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

Methods

__init__(columns, method, units[, period])

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 - creates column containing sine or cosine of another datetime column.

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: pandas.core.frame.DataFrame)pandas.core.frame.DataFrame[source]

Transform - creates column containing sine or cosine of another datetime column.

Which function is used is stored in the self.method attribute.

Parameters

X (pd.DataFrame) – Data to transform.

Returns

X – Input X with additional columns added, these are named “<method>_<original_column>”

Return type

pd.DataFrame