I have to work with timezone aware timestamps and I am now struggling to do something as simple as adding an hour to my timezone index:
Here is the setup:
import pandas as pd
df = pd.DataFrame({"dato_tid" :['2020-03-29 01:00', '2020-03-29 03:00','2020-03-29 04:00']})
df = df.set_index('dato_tid')
df.index = pd.to_datetime(df.index)
df = df.tz_localize('Europe/Oslo')
The datetimes are specifically chosen to hit the shift from daylight saving hours. Now I want to add one hour to the entire index. Normally (in a timezone naive world) i would use:
df.index = df.index +pd.DateOffset(hours=1)
But this now crashes because it wants to move the '2020-03-29 01:00+01:00' to '2020-03-29 02:00+01:00' which does not exist.
Is there an easy way to add one hour to every element of my index?
Use TimeDelta instead:
df.index = df.index + pd.Timedelta(hours=1)
Result:
dato_tid
2020-03-29 03:00:00+02:00
2020-03-29 04:00:00+02:00
2020-03-29 05:00:00+02:00
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.