Adding offset to timestamp in pandas

和自甴很熟 提交于 2019-12-24 06:14:40

问题


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

  1. First timestamp in the series is changed from '2011-08-05 00:00:00-04:00' to '2011-08-04 20:00:00' and
  2. 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.


回答1:


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')


来源:https://stackoverflow.com/questions/47620882/adding-offset-to-timestamp-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!