我有一个数据框 df,当我运行 print(df.index) 时,我得到:
DatetimeIndex(['2011-08-05 00:00:00-04:00', '2011-08-05 01:00:00-04:00',
'2011-08-05 02:00:00-04:00', '2011-08-05 03:00:00-04:00',
'2011-08-05 04:00:00-04:00', '2011-08-05 05:00:00-04:00',
'2011-08-05 06:00:00-04:00', '2011-08-05 07:00:00-04:00',
'2011-08-05 08:00:00-04:00', '2011-08-05 09:00:00-04:00',
...
'2017-07-30 14:00:00-04:00', '2017-07-30 15:00:00-04:00',
'2017-07-30 16:00:00-04:00', '2017-07-30 17:00:00-04:00',
'2017-07-30 18:00:00-04:00', '2017-07-30 19:00:00-04:00',
'2017-07-30 20:00:00-04:00', '2017-07-30 21:00:00-04:00',
'2017-07-30 22:00:00-04:00', '2017-07-30 23:00:00-04:00'],
dtype='datetime64[ns, America/New_York]', name=u'Time', length=52488, freq=None)
我正在尝试修改 datetimeindex 对象,以便
- 系列中的第一个时间戳从 更改
'2011-08-05 00:00:00-04:00'
为'2011-08-04 20:00:00'
和
- 系列中的第二个邮票将从 更改
'2011-08-05 00:00:00-04:00'
为'2011-08-04 21:00:00'
,以此类推。
我试过pd.to_datetime(df.index, format='%Y-%m-%d %H:%M:%S')
了,但它返回与datetimeindex
上面相同的对象。
如果将时间戳转换为字符串,我可以,所以我尝试了:
df.index.strftime('%Y-%m-%d %H:%M:%S')
但是这两行代码都没有达到我的最终目标。