Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
datetime replace error
#1
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'
Reply
#2
please show run-able code snippet, including imports.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error when Excelwriter saving a dataframe with datetime datatype with timezone klllmmm 3 13,088 Dec-08-2020, 11:37 AM
Last Post: Larz60+
  Attribute Error for Rename / Replace warden89 6 7,701 Jan-07-2020, 06:08 PM
Last Post: warden89
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,515 Sep-18-2019, 08:32 AM
Last Post: buran
  Trying to subtract datetime, getting error: TypeError: unsupported operand type(s) fo kneesarethebees 1 4,395 Aug-02-2018, 01:39 AM
Last Post: ichabod801
  Search & Replace - Newlines Added After Replace dj99 3 3,356 Jul-22-2018, 01:42 PM
Last Post: buran
  Error when converting MATLAB's datenum to Python's datetime zoya2385 5 8,307 Apr-03-2017, 06:01 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020