Saimouer
Saimouer

Reputation: 27

Converting timeseries data given in timedeltas64[ns] to datetime64[ns]

Suppose I'm given a pandas dataframe that is indexed in timedeltas64[ns].

          A            B         C         D                E
0 days 00:00:00    0.642973 -0.041259  253.377516          0.0   
0 days 00:15:00    0.647493 -0.041230  253.309167          0.0   
0 days 00:30:00    0.723258 -0.063110  253.416138          0.0   
0 days 00:45:00    0.739604 -0.070342  253.305809          0.0   
0 days 01:00:00    0.643327 -0.041131  252.967084          0.0   
...                     ...       ...         ...          ...   
364 days 22:45:00  0.650392 -0.064805  249.658052          0.0   
364 days 23:00:00  0.652765 -0.064821  249.243891          0.0   
364 days 23:15:00  0.607198 -0.103190  249.553821          0.0   
364 days 23:30:00  0.597602 -0.107975  249.687942          0.0   
364 days 23:45:00  0.595224 -0.110376  250.059530          0.0  

There does not appear to be any "permitted" way of converting the index to datetimes. Basic operations to convert the index such as:

df.index = pd.DatetimeIndex(df.index)

Or:

test_df.time = pd.to_datetime(test_df.index,format='%Y%m%d%H%M')

Both yield:

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

Is there any permitted way to do this operation other than completely reformatting all of these (very numerous) datasets manually? The data is yearly with 15 minute intervals.

Upvotes: 1

Views: 367

Answers (1)

sedavidw
sedavidw

Reputation: 11691

Your issue is that you cannot convert a timedelta object to a datetime object because the former is the difference between two datetimes. Based on your question it sounds like all these deltas are from the same base time, so you would need to add that in. Example usages below

In [1]: import datetime

In [2]: now = datetime.datetime.now()

In [3]: delta = datetime.timedelta(minutes=5)

In [4]: print(now, delta + now)
2021-02-22 20:14:37.273444 2021-02-22 20:19:37.273444

You can see in the above that the second print datetime is 5 minutes after the now object

Upvotes: 2

Related Questions