AttributeError: Can only use .dt accessor with datetimelike values. Did you mean: 'at'?

AttributeError: Can only use .dt accessor with datetimelike values. Did you mean: 'at'?
python
Ethan Jackson

I have been writing a python script using Atom, but because of issues rendering plots transitioned over to PyCharm with a virtual enviroment.

Part of the script includes the following:

df['date1'] = pd.to_datetime(df['date1'], format='%d%b%Y', errors='raise').dt.date df['date2'] = pd.to_datetime(df['date2'], format='%d%b%Y', errors='raise').dt.date df['diff'] = (df['date1'] - df['date2']).dt.days/30

It runs entirely fine on Atom, but on PyCharm I get the following error: AttributeError: Can only use .dt accessor with datetimelike values. Did you mean: 'at'?

I've seen similar questions where the errors has been raised due to a data issue & hence added errors='raise' but which raises no errors.

I presume given the error is related to the differing IDE it may be due to different versions of packages, but as far as I can tell they are the same and so am unsure on the cause/how to fix it.

Answer

You need to remove .dt.date. You can work directly with datetime64

df['date1'] = pd.to_datetime(df['date1'], format='%d%b%Y', errors='raise') df['date2'] = pd.to_datetime(df['date2'], format='%d%b%Y', errors='raise') df['diff'] = (df['date1'] - df['date2']).dt.days / 30

Related Articles