Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Split date_recorded stamp (string) into year, month, day of month, day of week features
from dateutil import parser
dfX['date_recorded_year'] = dfX['date_recorded'].apply(lambda x: int(x.split('-')[0]))
dates.append('date_recorded_year')
dfX['date_recorded_month'] = dfX['date_recorded'].apply(lambda x: int(x.split('-')[1]))
dates.append('date_recorded_month')
# WARNING : probably not usefull for this dataset
dfX['date_recorded_day'] = dfX['date_recorded'].apply(lambda x: int(x.split('-')[2]))
dates.append('date_recorded_day')
# for weekday convert to string to "categorize"
dfX['date_recorded_weekday'] = dfX['date_recorded'].apply(lambda x: str(parser.parse(x).weekday()))
dates.append('date_recorded_weekday')
# for is_weekend convert to int to "binarize"
dfX['date_recorded_isweekend'] = dfX['date_recorded'].apply(lambda x: int(parser.parse(x).weekday() in [5,6]))
dates.append('date_recorded_isweekend')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment