【发布时间】:2018-02-07 03:49:05
【问题描述】:
我有一个带有索引的数据框,其中还包含 Hour:second:... 但我只希望它包含年、月、日等日期。
索引是否可能仍然是 DateTimeIndex 但仅包含年、月、日?
当前索引如下:
my_index = DatetimeIndex(['2017-08-25', '2017-08-24', '2017-08-23', '2017-08-22',
'2017-08-21', '2017-08-20', '2017-08-19', '2017-08-18',
'2017-08-17', '2017-08-16',
...
'2015-07-19', '2015-07-18', '2015-07-17', '2015-07-16',
'2015-07-15', '2015-07-14', '2015-07-13', '2015-07-12',
'2015-07-11', '2015-07-10'],
dtype='datetime64[ns]', length=778, freq=None)
我能做到:
only_date_index = [el.date() for el in my_index]
但是,如果我想在 pandas 中使用 resample 函数,我会收到错误消息: TypeError:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但获得了“Index”实例。
【问题讨论】:
-
试试
.normalize()它会让时间走到午夜,因为我相信DateTimeIndex需要时间