def add_date_features_df(df, date_col):
# Pandas nicely tries to prevent you from doing stupid things, like setting values on a copy of a df, not your real one
# However, it's a bit overzealous in this case, so we'll side-step a bunch of warnings by setting is_copy to false here
df.is_copy = False
df[date_col] = pd.to_datetime(df[date_col])
df[date_col + '_day_of_week'] = df[date_col].apply(lambda x: x.weekday()).astype(int, raise_on_error=False)
try:
df[date_col + '_hour'] = df[date_col].apply(lambda x: x.hour).astype(int, raise_on_error=False)
df[date_col + '_minutes_into_day'] = df[date_col].apply(lambda x: x.hour * 60 + x.minute)
except AttributeError:
pass
df[date_col + '_is_weekend'] = df[date_col].apply(lambda x: x.weekday() in (5,6))
df[date_col + '_day_part'] = df[date_col + '_minutes_into_day'].apply(minutes_into_day_parts)
df = df.drop([date_col], axis=1)
return df
# Same logic as above, except implemented for a single dictionary, which is much faster at prediction time when getting just a single prediction
评论列表
文章目录