Home > Mobile >  TypeError: dtype datetime64[ns] cannot be converted to timedelta64[ns]
TypeError: dtype datetime64[ns] cannot be converted to timedelta64[ns]

Time:08-17

I have a column of years from the sunspots dataset.

I want to convert column 'year' in integer e.g. 1992 to datetime format then find the time delta and eventually compute total seconds (cumulative) to represent the time index column of a time series.

I am trying to use the following code but I get the error

TypeError: dtype datetime64[ns] cannot be converted to timedelta64[ns]

sunspots_df['year'] = pd.to_timedelta(pd.to_datetime(sunspots_df['year'], format='%Y') ).dt.total_seconds()

CodePudding user response:

pandas.Timedelta "[r]epresents a duration, the difference between two dates or times." So you're trying to get Python to tell you the difference between a particular datetime and...nothing. That's why it's failing.

If it's important that you store your index this way (and there may be better ways), then you need to pick a start datetime and compute the difference to get a timedelta.

For example, this code...

import pandas as pd
df = pd.DataFrame({'year': [1990,1991,1992]})
diff = (pd.to_datetime(df['year'], format='%Y') - pd.to_datetime('1990', format='%Y'))\
    .dt.total_seconds()

...returns a series whose values are seconds from January 1st, 1990. You'll note that it doesn't invoke pd.to_timedelta(), because it doesn't need to: the result of the subtraction is automatically a pd.timedelta column.

  • Related