<jupyter_start><jupyter_text>Load modules<jupyter_code>import sys
sys.path.append('..')

from datefeatures import MonthCircle

import numpy as np
import pandas as pd
from randdate import randdate
from datetime import datetime

from sklearn.pipeline import Pipeline, FeatureUnion
from mlxtend.feature_selection import ColumnSelector<jupyter_output><empty_output><jupyter_text>Example 1<jupyter_code># generate fake dates
X = np.c_[np.array(randdate(10)), np.array(randdate(10))]

# transform date variable to fetures
cmp = MonthCircle()
cmp.fit(X)
Z = cmp.transform(X)

Z.head()
cmp.feature_names_<jupyter_output><empty_output><jupyter_text>Example 2<jupyter_code># generate fake dates
X = np.c_[np.array(randdate(10)), np.array(randdate(10))]

# emulate missing value
X[1,0] = np.nan

# transform date variable to fetures
cmp = MonthCircle()
cmp.fit(X)
Z = cmp.transform(X)

Z.head()<jupyter_output><empty_output><jupyter_text>Example 3<jupyter_code>n_samples = 100000
X = np.c_[np.array(randdate(n_samples)), np.array(randdate(n_samples)), np.array(randdate(n_samples))]
cmp = MonthCircle()
%time Z = cmp.fit_transform(X)<jupyter_output>CPU times: user 4.29 s, sys: 122 ms, total: 4.41 s
Wall time: 4.68 s<jupyter_text>Example 4<jupyter_code># generate fake dates
n_samples = 5
X = np.c_[np.array(randdate(n_samples))]
X[1,0] = np.nan

# make pipeline
pipe = Pipeline(steps=[
    ('pre', MonthCircle(out=['sin', 'cos', 'frac']))
])

Z = pipe.fit_transform(X)
Z<jupyter_output><empty_output><jupyter_text>Example 5<jupyter_code># generate fake dates
n_samples = 5
X = pd.DataFrame(data=randdate(n_samples), columns=['this_date'])
X['some_numbers'] = np.random.randn(n_samples)
X
# make pipeline
pipe = Pipeline(steps=[
    # process column by column
    ('col_by_col', FeatureUnion(transformer_list=[
        ('dates', Pipeline(steps=[
            ('sel1', ColumnSelector(cols=('this_date'))),
            ('pre1', MonthCircle())
        ])),
        ('numbers', ColumnSelector(cols=('some_numbers')))
    ]))
    # do some other stuff ..
])

Z = pipe.fit_transform(X)
Z
colnam = list(pipe.steps[0][1].transformer_list[0][1].steps[1][1].feature_names_)
colnam += ['some_numbers']
colnam
pd.DataFrame(Z, columns=colnam)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14768683274021352, 'fraction_numerical': 0.02001779359430605, 'mean_word_length': 5.984472049689441, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '502689', 'license': 'Apache-2.0', 'n_tokens_mistral': 860, 'n_tokens_neox': 826, 'n_words': 165, 'path': 'examples/MonthCircle.ipynb', 'repo_name': 'kmedian/datefeatures', 'size': '2248'}