Python Forum
datetime replace error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: datetime replace error (/thread-15543.html)



datetime replace error - kbrummert - Jan-21-2019

I'm trying to apply a pretty basic datetime replace for a datagrid field, but it's not recognizing 'day' for some reason....

# change to datetime
df_mort['dt'] = pd.to_datetime(df_mort.dt)
# create year field for calcuating averages
df_mort['first_dt'] = df_mort.dt.replace(day=1)

....

TypeError: replace() got an unexpected keyword argument 'day'



RE: datetime replace error - Larz60+ - Jan-21-2019

please show run-able code snippet, including imports.


RE: datetime replace error - kbrummert - Jan-21-2019

Looks like it's a buggy thing, because a similar question out on SO that still hasn't been answered. I found a workaround...

df_mort['first_dt'] = df_mort['dt'].astype('datetime64[M]')
I just needed to create a field which sets the date to the first day of the respective month so I can calculate averages by month.