0

When I print

df.date.dtype

it returns:

dtype('<M8[ns]')

However when I print:

df.dtypes

it returns:

datetime64[ns]

Ever since the pandas update to 2.0 it has disallowed conversion to datetime 64 types (https://github.com/pandas-dev/pandas/blob/8cf4ab491b06274f555d1ea305c172354208747a/doc/source/whatsnew/v2.0.0.rst#disallow-astype-conversion-to-non-supported-datetime64timedelta64-dtypes)

How can I convert dtype('<M8[ns]') to datetime64[ns]? When I run my model I have issues as it says

UFuncTypeError: ufunc 'add' cannot use operands with types dtype('<M8[ns]') and dtype('<M8[ns]')

I never used to have this issue before...

I tried this but it did not work:

# df.date = df.date.astype('datetime64[ns]')
# df.date = pd.to_datetime(df.date, errors='coerce')

I tried the solution from here as well - Python numpy: cannot convert datetime64[ns] to datetime64[D] (to use with Numba)

But according to the comments - Note (2023-05-30): This answer only works for pandas version <2. Pandas 2.0.0 was released on 2023-04-03. See relevant changelog entry.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
ibarbo
  • 15
  • 1
  • 6
  • 2
  • 2
    Concerning your error, it says that you cannot *add* datetimes. This is defined by the methods of the type, not by you using the wrong type. You can only add datetime and timedelta (a duration), or subtract one datetime from another, giving you a timedelta in return. – FObersteiner Jun 08 '23 at 09:12

0 Answers0