Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 943
James Avatar asked Feb 13 '26 03:02

James


1 Answers

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)
like image 188
piRSquared Avatar answered Feb 14 '26 21:02

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!