Pandas 将datetimeindex转换为timedeltaindex
我有两个朋友。一个有DatetimeIndex,另一个有TimedeltaIndexPandas 将datetimeindex转换为timedeltaindex,pandas,Pandas,我有两个朋友。一个有DatetimeIndex,另一个有TimedeltaIndex DatetimeIndex(['2017-12-11 09:00:00', '2017-12-11 09:01:00', ... '2017-12-11 14:59:00', '2017-12-11 15:00:00'], dtype='datetime64[ns]', length=361, freq='T') TimedeltaIndex(
DatetimeIndex(['2017-12-11 09:00:00', '2017-12-11 09:01:00',
...
'2017-12-11 14:59:00', '2017-12-11 15:00:00'],
dtype='datetime64[ns]', length=361, freq='T')
TimedeltaIndex(['09:00:00', '09:01:00', '09:02:00', '09:03:00', '09:04:00',
...
'14:55:00', '14:56:00', '14:57:00', '14:58:00', '14:59:00'],
dtype='timedelta64[ns]', name='ts', length=300, freq=None)
我正在尝试连接它们
pd.concat([d1, d2], axis=1)
但它会抛出
'Index' object has no attribute 'freq'
大概是因为不同的索引时间。我尝试使用d1.index.appy或to_timedelta更改索引,但失败了,因为我无法在索引上使用apply,而使用pd.to_timedelta抛出
Argument 'values' has incorrect type (expected numpy.ndarray, got Index)
我肯定错过了一些直截了当的方法。有什么建议我可以轻松地做到这一点。我不需要日期信息,因此任何解决方案都可能会删除此项。我认为您可以使用:
或:
一些,连同预期的产出将有所帮助。
d1.index = pd.to_timedelta(d1.index.strftime('%H:%M:%S'))
d1 = d1.set_index(pd.to_timedelta(d1.index.strftime('%H:%M:%S')))