Pandas 如何将熊猫中的索引转换为日期时间?
我有这样的索引Pandas 如何将熊猫中的索引转换为日期时间?,pandas,indexing,time-series,Pandas,Indexing,Time Series,我有这样的索引 Index(['00:00:00', '00:15:00', '00:30:00', '00:45:00', '01:00:00', '01:15:00', '01:30:00', '01:45:00', '02:00:00', '02:15:00', '02:30:00', '02:45:00'], dtype='object', name='time') 并且需要将其转换为日期时间%H:%M:%S格式 如何更改它?我想您需要: idx = pd.
Index(['00:00:00', '00:15:00', '00:30:00', '00:45:00', '01:00:00', '01:15:00',
'01:30:00', '01:45:00', '02:00:00', '02:15:00', '02:30:00', '02:45:00'],
dtype='object', name='time')
并且需要将其转换为日期时间%H:%M:%S格式
如何更改它?我想您需要:
idx = pd.Index(['00:00:00', '00:15:00', '00:30:00', '00:45:00', '01:00:00', '01:15:00',
'01:30:00', '01:45:00', '02:00:00', '02:15:00', '02:30:00', '02:45:00'],
dtype='object', name='time')
对于DatetimeIndex
需要一些日期,默认情况下今天添加:
print (pd.to_datetime(idx))
DatetimeIndex(['2018-01-25 00:00:00', '2018-01-25 00:15:00',
'2018-01-25 00:30:00', '2018-01-25 00:45:00',
'2018-01-25 01:00:00', '2018-01-25 01:15:00',
'2018-01-25 01:30:00', '2018-01-25 01:45:00',
'2018-01-25 02:00:00', '2018-01-25 02:15:00',
'2018-01-25 02:30:00', '2018-01-25 02:45:00'],
dtype='datetime64[ns]', name='time', freq=None)
或者可以添加自定义日期:
print (pd.to_datetime('2015-01-01 ' + idx))
DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 00:15:00',
'2015-01-01 00:30:00', '2015-01-01 00:45:00',
'2015-01-01 01:00:00', '2015-01-01 01:15:00',
'2015-01-01 01:30:00', '2015-01-01 01:45:00',
'2015-01-01 02:00:00', '2015-01-01 02:15:00',
'2015-01-01 02:30:00', '2015-01-01 02:45:00'],
dtype='datetime64[ns]', freq=None)
另一个解决方案是创建TimedeltaIndex
:
print (pd.to_timedelta(idx))
TimedeltaIndex(['00:00:00', '00:15:00', '00:30:00', '00:45:00', '01:00:00',
'01:15:00', '01:30:00', '01:45:00', '02:00:00', '02:15:00',
'02:30:00', '02:45:00'],
dtype='timedelta64[ns]', name='time', freq=None)