gators.feature_generation_dt.CyclicMonthOfYear

class gators.feature_generation_dt.CyclicMonthOfYear(columns: List[str], date_format: str = 'ymd')[source]

Create new columns based on the cyclic mapping of the month of the year.

Parameters
theta_vecList[float]

List of columns.

Examples

Imports and initialization:

>>> from gators.feature_generation_dt import CyclicMonthOfYear
>>> obj = CyclicMonthOfYear(columns=['A'])

The fit, transform, and fit_transform methods accept:

  • dask dataframes:

>>> import dask.dataframe as dd
>>> import pandas as pd
>>> X = dd.from_pandas(pd.DataFrame({'A': ['2020-01-01T23', '2020-12-15T18',  None], 'B': [0, 1, 0]}), npartitions=1)
>>> X['A'] = X['A'].astype('datetime64[ns]')
  • koalas dataframes:

>>> import databricks.koalas as ks
>>> X = ks.DataFrame({'A': ['2020-01-01T23', '2020-12-15T18',  None], 'B': [0, 1, 0]})
>>> X['A'] = X['A'].astype('datetime64[ns]')
  • and pandas dataframes:

>>> import pandas as pd
>>> X = pd.DataFrame({'A': ['2020-01-01T23', '2020-12-15T18',  None], 'B': [0, 1, 0]})
>>> X['A'] = X['A'].astype('datetime64[ns]')

The result is a transformed dataframe belonging to the same dataframe library.

>>> obj.fit_transform(X)
                    A  B  A__month_of_year_cos  A__month_of_year_sin
0 2020-01-01 23:00:00  0              1.000000                   0.0
1 2020-12-15 18:00:00  1              0.866025                  -0.5
2                 NaT  0                   NaN                   NaN
>>> X = pd.DataFrame({'A': ['2020-01-01T23', '2020-12-15T18',  None], 'B': [0, 1, 0]})
>>> X['A'] = X['A'].astype('datetime64[ns]')
>>> _ = obj.fit(X)
>>> obj.transform_numpy(X.to_numpy())
array([[Timestamp('2020-01-01 23:00:00'), 0, 1.0, 0.0],
       [Timestamp('2020-12-15 18:00:00'), 1, 0.8660254037844384,
        -0.5000000000000004],
       [NaT, 0, nan, nan]], dtype=object)
transform(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame]) → Union[pd.DataFrame, ks.DataFrame, dd.DataFrame][source]

Transform the dataframe X.

Parameters
XDataFrame.

Input dataframe.

Returns
XDataFrame

Transformed dataframe.

transform_numpy(X: numpy.ndarray) → numpy.ndarray[source]

Transform the array X.

Parameters
Xnp.ndarray

Input array.

Returns
Xnp.ndarray

Transformed array.

compute_cyclic_month_of_year(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], prefactor: float) → Union[pd.DataFrame, ks.DataFrame, dd.DataFrame][source]

Compute the cyclic hours of the day features.

Parameters
XDataFrame

Dataframe of datetime columns.

prefactorfloat.

Prefactor.

Returns
XDataFrame

Dataframe of cyclic hours of the day features.

static check_array(X: numpy.ndarray)

Validate array.

Parameters
Xnp.ndarray

Array.

check_array_is_numerics(X: numpy.ndarray)

Check if array is only numerics.

Parameters
Xnp.ndarray

Array.

static check_binary_target(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series])

Raise an error if the target is not binary.

Parameters
ySeries

Target values.

static check_dataframe(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])

Validate dataframe.

Parameters
XDataFrame

Dataframe.

static check_dataframe_contains_numerics(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])

Check if dataframe is only numerics.

Parameters
XDataFrame

Dataframe.

static check_dataframe_is_numerics(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])

Check if dataframe is only numerics.

Parameters
XDataFrame

Dataframe.

check_dataframe_with_objects(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])

Check if dataframe contains object columns.

Parameters
XDataFrame

Dataframe.

check_datatype(dtype, accepted_dtypes)

Check if dataframe is only numerics.

Parameters
XDataFrame

Dataframe.

static check_multiclass_target(y: Union[pd.Series, ks.Series, dd.Series])

Raise an error if the target is not discrete.

Parameters
ySeries

Target values.

check_nans(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], columns: List[str])

Raise an error if X contains NaN values.

Parameters
XDataFrame

Dataframe.

theta_vecList[float]

List of columns.

static check_regression_target(y: Union[pd.Series, ks.Series, dd.Series])

Raise an error if the target is not discrete.

Parameters
ySeries

Target values.

static check_target(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series])

Validate target.

Parameters
XDataFrame

Dataframe.

ySeries

Target values.

fit(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series] = None) → gators.transformers.transformer.Transformer

Fit the transformer on the dataframe X.

Parameters
XDataFrame

Input dataframe.

ySeries, default None.

Target values.

Returns
selfTransformer

Instance of itself.

fit_transform(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series] = None) → Union[pd.DataFrame, ks.DataFrame, dd.DataFrame]

Fit and Transform the dataframe X.

Parameters
XDataFrame.

Input dataframe.

ySeries, default None.

Input target.

Returns
XDataFrame

Transformed dataframe.

static get_column_names(inplace: bool, columns: List[str], suffix: str)

Return the names of the modified columns.

Parameters
inplacebool

If True return columns. If False return columns__suffix.

columnsList[str]

List of columns.

suffixstr

Suffix used if inplace is False.

Returns
List[str]

List of column names.

static get_cyclic_column_names(columns: List[str], pattern: str)

Get the column names.

Parameters
theta_vecList[float]

List of datetime features.

pattern: str

Pattern.

static get_idx(date_format: str) → Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]

[summary]

Parameters
date_formatstr

Datetime format

Returns
idx_day_boundsnp.ndarray

Start and end indices of the day.

idx_monthnp.ndarray

Start and end indices of the month.

idx_year_boundsnp.ndarray

Start and end indices of the year.

get_params(deep=True)

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)

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.