footonwonton
footonwonton

Reputation: 794

Python Pandas DateTimeIndex

Below a DateTimeIndex from a DataFrame. The Summer/Winter timezone is 'US/Eastern', however the recorded time stamps are 'Europe/London'. I am trying to re-index which will achieve a full time sequence.

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)

How do I re-index the above without messing up the hour element?

Upvotes: 3

Views: 384

Answers (1)

piRSquared
piRSquared

Reputation: 294258

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)

Upvotes: 2

Related Questions