我猜这是一个非常愚蠢的问题,但无论如何,这里是:
我有一个带有日期时间索引的数据帧,它看起来像这样:
DatetimeIndex(['1995-01-02', '1995-01-03', '1995-01-04', '1995-01-05',
'1995-01-06', '1995-01-09', '1995-01-10', '1995-01-11',
'1995-01-12', '1995-01-13'])
毫不奇怪,time
字段是"0";00:00:00":
bets.index.map(pd.datetime.time)
给出:
Index([00:00:00, 00:00:00, 00:00:00, 00:00:00, 00:00:00, 00:00:00, 00:00:00,
00:00:00, 00:00:00, 00:00:00])
我想将时间字段设置为其他内容(例如中午(,用于所有索引元素。最简单的方法是什么?
IIUC,您可以使用to_timedelta
并传入所需的小时数:
bets.index += pd.to_timedelta('12h')
输出:
DatetimeIndex(['1995-01-02 12:00:00', '1995-01-03 12:00:00',
'1995-01-04 12:00:00', '1995-01-05 12:00:00',
'1995-01-06 12:00:00', '1995-01-09 12:00:00',
'1995-01-10 12:00:00', '1995-01-11 12:00:00',
'1995-01-12 12:00:00', '1995-01-13 12:00:00'],
dtype='datetime64[ns]', freq=None)