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
- First timestamp in the series is changed from
'2011-08-05 00:00:00-04:00'
to'2011-08-04 20:00:00'
and
- Second stamp in the series would be changed from
'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 removetimezone
s and addHour
s:Or:
Sample: