[英]Pandas: Add hour to timezone aware index
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.但这现在崩溃了,因为它想将 '2020-03-29 01:00+01:00' 移动到不存在的 '2020-03-29 02:00+01:00' 。
Is there an easy way to add one hour to every element of my index?有没有一种简单的方法可以为我的索引的每个元素添加一小时?
Use TimeDelta instead:改用 TimeDelta:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.