python - Pandas tz_localize : Infer dst when localizing timezone in data with duplicates

标签 python datetime pandas timezone

我想用 pandas tz_localize 本地化一个日期时间系列。该系列跨越 DST 日期(例如,德国 CET 为 25Oct2015)。我通常这样做

import pandas as pd
T = ['25/10/2015 02:59:00','25/10/2015 02:00:00','25/10/2015 02:01:00']
pd.to_datetime(T).tz_localize('CET',ambiguous='infer')

但是当时间序列有重复时——即使它们以明确的方式排序——我得到一个错误:

T = ['25/10/2015 02:59:00','25/10/2015 02:59:00','25/10/2015 02:00:00','25/10/2015 02:01:00']
pd.to_datetime(T).tz_localize('CET',ambiguous='infer')

AmbiguousTimeError: There are 2 dst switches when there should only be 1.

这似乎是一个不必要的限制,因为推断应该非常简单。是否有解决方法或解决方案,或者我是否需要编写自己的推断方法?

最佳答案

在最新版本中修复了一些 DST 相关的错误,0.19.0rc1 现已发布

In [1]: pd.__version__
Out[1]: u'0.19.0rc1'

In [2]: t = ['25/10/2015 02:59:00', '25/10/2015 02:00:00', '25/10/2015 02:01:00']

In [3]: pd.to_datetime(t).tz_localize('CET',ambiguous='infer')
Out[3]: DatetimeIndex(['2015-10-25 02:59:00+02:00', '2015-10-25 02:00:00+01:00', '2015-10-25 02:01:00+01:00'], dtype='datetime64[ns, CET]', freq=None)

关于python - Pandas tz_localize : Infer dst when localizing timezone in data with duplicates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39563507/

相关文章:

python - 在点击中弃用参数别名的正确方法

python - Jupyter Notebook在启动Python内核时自动导入模块

javascript - 如何使用 Flask 将 JavaScript 连接到 Python 脚本?

linux - 如何在 shell 脚本中生成顺序日期/时间?

python - Python/Pandas 中的 bool 列比较

python - 计算一列中子字符串在另一列中的出现次数

python - 自定义月份的开始和结束日期

python - np.piecewise() 与 math.pow() 一起使用?

android - 如何获得最后一次触摸屏幕的时间?

c# - 我如何在 C# 中获取当前日期?