I have a dataframe df and when I run print(df.index), I get:
DatetimeIndex(['2011-08-05 00:00:00-04:00', '2011-08-05 01:00:00-04:00',
'2011-08-05 02:00:00-04:00', '2011-08-05 03:00:00-04:00',
'2011-08-05 04:00:00-04:00', '2011-08-05 05:00:00-04:00',
'2011-08-05 06:00:00-04:00', '2011-08-05 07:00:00-04:00',
'2011-08-05 08:00:00-04:00', '2011-08-05 09:00:00-04:00',
...
'2017-07-30 14:00:00-04:00', '2017-07-30 15:00:00-04:00',
'2017-07-30 16:00:00-04:00', '2017-07-30 17:00:00-04:00',
'2017-07-30 18:00:00-04:00', '2017-07-30 19:00:00-04:00',
'2017-07-30 20:00:00-04:00', '2017-07-30 21:00:00-04:00',
'2017-07-30 22:00:00-04:00', '2017-07-30 23:00:00-04:00'],
dtype='datetime64[ns, America/New_York]', name=u'Time', length=52488, freq=None)
I am trying to modify the datetimeindex object, so that the
'2011-08-05 00:00:00-04:00' to '2011-08-04 20:00:00' and'2011-08-05 00:00:00-04:00' to '2011-08-04 21:00:00', and so on. I tried pd.to_datetime(df.index, format='%Y-%m-%d %H:%M:%S'), but it returns the same datetimeindex object as above.
It is OK with me if the timestamps are converted to string, so I tried:
df.index.strftime('%Y-%m-%d %H:%M:%S')
But neither lines of code achieves my end goal.
Use tz_convert for remove timezones and add Hours:
df.index.tz_convert(None) + pd.offsets.Hour(16)
Or:
df.index.tz_convert(None) + pd.Timedelta(16, unit='h')
Sample:
idx = ['2011-08-05 00:00:00-04:00', '2011-08-05 01:00:00-04:00',
'2011-08-05 02:00:00-04:00', '2011-08-05 03:00:00-04:00']
idx = pd.DatetimeIndex(idx).tz_localize('UTC').tz_convert('America/New_York')
print (idx)
DatetimeIndex(['2011-08-05 00:00:00-04:00', '2011-08-05 01:00:00-04:00',
'2011-08-05 02:00:00-04:00', '2011-08-05 03:00:00-04:00'],
dtype='datetime64[ns, America/New_York]', freq=None)
idx = idx.tz_convert(None) + pd.offsets.Hour(16)
print (idx)
DatetimeIndex(['2011-08-05 20:00:00', '2011-08-05 21:00:00',
'2011-08-05 22:00:00', '2011-08-05 23:00:00'],
dtype='datetime64[ns]', freq='H')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With