I'm working with COVID19 data and have a date column. It was originally as Int64, so I changed it to datetime object using the below function:
us['date'] = pd.to_datetime(us['date'], format='%Y%m%d', errors='ignore')
us['date'] = us['date'].dt.strftime("%m/%d")
When I print out the result, I see all the data in the column as "1/1, 1/4, 2/1 ....", so on. (There is no null value here) I'm trying to use group this by month, so I tried to use another function to get only months as below:
us['date'].dt.strftime("%m")
But I'm getting the error:
AttributeError: Can only use .dt accessor with datetimelike values
I don't understand, since I change the datatype to datetime using pd_datetime, should this column already be datetime, not object? When I checked the data type, it shows 'O'. Why is this so?
format='%Y%m%d'doesn't make much sense to me - instead, this should require you to supply a unit, e.g.unit='s'if the datetime data is represented as UNIX time / seconds since the epoch.