how to convert timestamp column of pandas dataframe into hour and day features using transformer


from sklearn.base import BaseEstimator, TransformerMixin
import pandas as pd

class dayandhour_Transformer(BaseEstimator, TransformerMixin):

# Class Constructor

def __init__(self):

print('initialized')



# Return self, nothing else to do here

def fit(self, X, y=None):

return self




# Customized transformer method

def transform(self, X_, y=None):

X = X_.copy()

X['dayofweek']=pd.to_datetime(X['timestamp']).dt.dayofweek

X['hour']=pd.to_datetime(X['timestamp']).dt.hour

X = X.drop('timestamp',axis=1)

return X



dayandhour_transformer = dayandhour_Transformer()



df = dayandhour_transformer.transform(df)

Suppose we have a dataframe df with a column “timestamp”.

before apply the code, we have:

print(df['timestamp'])

0 2021-03-28 03:28:10.205000

1 2021-03-28 21:31:43.290000

2 2021-03-28 21:16:18.771000

3 2021-03-28 18:39:13.344000

4 2021-03-28 00:54:57.544000

after we apply the code, we have:

print( df[['hour','dayofweek']])

hour dayofweek

0 3 6

1 21 6

2 21 6

3 18 6

4 0 6

Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC