Python,Pandas:2023年非洲/开罗时区不适用夏令时(DST)

huangapple go评论72阅读模式
英文:

Python, Pandas: Africa/Cairo time zone doesn't apply daylight saving time (DST) in 2023

问题

需要将时间戳本地化为埃及本地时间。自2011年以来,埃及不再使用夏令时(DST)。然而,埃及将于2023年4月28日重新实施夏令时。

问题在于pandas中的“Africa/Cairo”时区没有考虑到这次重新实施的夏令时。它在2010年之前使用夏令时,但在2023年没有考虑到。除了为每个时间戳手动指定时间偏移量之外,是否有一种明确指定夏令时转换日期的方法来本地化时间戳?我尝试了pytz,但遇到了相同的问题。

英文:

I need to localize my timestamps to have Egypt local time. The usage of DST has been stopped in Egypt since 2011. However, Egypt will reapply DST starting on 28 April 2023.

The problem is that the "Africa/Cairo" time zone in pandas didn't get this reapplication. It uses DST until 2010 but doesn't get it back in 2023. Is there a way to explicitly specify the DST transition dates when localizing timestamps in pandas other than specifying the time offset manually for each time stamp? I tried pytz but got the same problem.

>>> pd.Timestamp('20100501').tz_localize('Africa/Cairo')
Timestamp('2010-05-01 00:00:00+0300', tz='Africa/Cairo')

>>> pd.Timestamp('20230501').tz_localize('Africa/Cairo')
Timestamp('2023-05-01 00:00:00+0200', tz='Africa/Cairo')   # Should be +3 too

答案1

得分: 1

感谢上面的评论。

解决方法是升级 pytz

pip install -U pytz

现在

>>> pd.Timestamp('20230501').tz_localize('Africa/Cairo')
Timestamp('2023-05-01 00:00:00+0300', tz='Africa/Cairo')

即使不升级 pandas 或导入 pytz 也可以。

英文:

Thanks to the comments above.

The solution was to upgrade pytz

pip install -U pytz

Now

>>> pd.Timestamp('20230501').tz_localize('Africa/Cairo')
Timestamp('2023-05-01 00:00:00+0300', tz='Africa/Cairo')

Even without upgrading pandas or importing pytz.

答案2

得分: 0

pandas 版本 2.0.0 中,它似乎运行良好。

你使用的是哪个 pandas 版本?

英文:

In pandas version 2.0.0 it seems to work just fine.
Which pandas' version do you use?

Python,Pandas:2023年非洲/开罗时区不适用夏令时(DST)

huangapple
  • 本文由 发表于 2023年4月13日 22:12:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定