Python Pandas DateTimeIndex

时间:2016-10-25 21:22:55

标签: python datetime pandas reindex

来自DataFrame的DateTimeIndex。夏季/冬季时区为“美国/东部”,但记录的时间戳为“欧洲/伦敦”。我正在尝试重新索引哪个将实现全时序列。

DatetimeIndex(['1993-10-24 21:00:00', '1993-10-25 21:00:00',
               '1993-10-26 21:00:00', '1993-10-27 21:00:00',
               '1993-10-28 21:00:00', '1993-10-31 22:00:00',
               '1993-11-01 22:00:00', '1993-11-02 22:00:00',
               '1993-11-03 22:00:00', '1993-11-04 22:00:00'],
              dtype='datetime64[ns]', name=u'TIME', freq=None)

如何在不弄乱小时元素的情况下重新编制上述索引?

1 个答案:

答案 0 :(得分:1)

tidx = pd.DatetimeIndex(
    [
        '1993-10-24 21:00:00', '1993-10-25 21:00:00',
        '1993-10-26 21:00:00', '1993-10-27 21:00:00',
        '1993-10-28 21:00:00', '1993-10-31 22:00:00',
        '1993-11-01 22:00:00', '1993-11-02 22:00:00',
        '1993-11-03 22:00:00', '1993-11-04 22:00:00'],
    dtype='datetime64[ns]', name=u'TIME', freq=None
)

ts = tidx.to_series().dt.hour.resample('D').last().ffill().rename_axis('date')
ts.index + pd.to_timedelta(ts.values, unit='H')
DatetimeIndex(['1993-10-24 21:00:00', '1993-10-25 21:00:00',
               '1993-10-26 21:00:00', '1993-10-27 21:00:00',
               '1993-10-28 21:00:00', '1993-10-29 21:00:00',
               '1993-10-30 21:00:00', '1993-10-31 22:00:00',
               '1993-11-01 22:00:00', '1993-11-02 22:00:00',
               '1993-11-03 22:00:00', '1993-11-04 22:00:00'],
              dtype='datetime64[ns]', freq=None)