python - 使用多种符号在夏令时期间本地化 Pandas 日期时间

标签 python pandas datetime timezone dst

我有 pandas DataFrames,其时间序列涵盖每年 Spring 和秋季几年的中欧夏令时扰动。 对于某些 dfs,使用 dt.tz_localize 一切都按预期工作,但我无法解决以下时间符号:

not_working = pd.to_datetime(
    pd.Series(
        [
            "2017-10-29 01:45:00",
            "2017-10-29 02:00:00",
            "2017-10-29 02:15:00",
            "2017-10-29 02:45:00",
            "2017-10-29 03:00:00",
            "2017-10-29 02:15:00",
            "2017-10-29 02:45:00",
            "2017-10-29 03:00:00",
        ]
    )
)
not_working = not_working.dt.tz_localize(tz="Europe/Berlin", ambiguous="infer")

导致此错误消息:

pytz.exceptions.AmbiguousTimeError: 2017-10-29 02:00:00

当本地化 02:00 两次时,它按预期工作。遗憾的是,我无法控制输入数据格式。因此,我想知道如何本地化我上面的数据。

working = pd.to_datetime(
pd.Series(
    [
        "2017-10-29 01:45:00",
        "2017-10-29 02:00:00",
        "2017-10-29 02:15:00",
        "2017-10-29 02:45:00",
        "2017-10-29 02:00:00",
        "2017-10-29 02:15:00",
        "2017-10-29 02:45:00",
        "2017-10-29 03:00:00",
        ]
    )
)

working = working.dt.tz_localize(tz="Europe/Berlin", ambiguous="infer")

此修复程序有效,但我更喜欢不太老套的解决方案。 nonexisting 参数不能与 ambigous 一起使用,因为我不想先构建 bool 数组。

not_working = not_working - pd.Timedelta(seconds=1)
not_working = not_working.dt.tz_localize(tz="Europe/Berlin", ambiguous="infer")
not_working = not_working + pd.Timedelta(seconds=1)

最佳答案

问题是您从时区感知的日期时间数据开始,因此 tz_localize 无法解决某些特殊日期/时间相关的歧义到夏令时。

如果您首先使用选项 utc=True 使您的数据帧数据具有时区意识,您可以将其转换为所需的时区:

working = pd.to_datetime(
    pd.Series(
        [
            "2017-10-29 01:45:00",
            "2017-10-29 02:00:00",
            "2017-10-29 02:15:00",
            "2017-10-29 02:45:00",
            "2017-10-29 03:00:00",
            "2017-10-29 02:15:00",
            "2017-10-29 02:45:00",
            "2017-10-29 03:00:00",
        ]
    ),
    utc=True
)

working.dtypes
# datetime64[ns, UTC]

working.dt.tz_convert(tz="Europe/Berlin")
# 0   2017-10-29 02:45:00+01:00
# 1   2017-10-29 03:00:00+01:00
# 2   2017-10-29 03:15:00+01:00
# 3   2017-10-29 03:45:00+01:00
# 4   2017-10-29 04:00:00+01:00
# 5   2017-10-29 03:15:00+01:00
# 6   2017-10-29 03:45:00+01:00
# 7   2017-10-29 04:00:00+01:00

如果原始数据不在UTC时区,你可以在转换之前调整它们,加上或减去适当的时间间隔

working = working-pd.Timedelta('01:00:00')

working.dt.tz_convert(tz="Europe/Berlin")
# 0   2017-10-29 02:45:00+02:00
# 1   2017-10-29 02:00:00+01:00
# 2   2017-10-29 02:15:00+01:00
# 3   2017-10-29 02:45:00+01:00
# 4   2017-10-29 03:00:00+01:00
# 5   2017-10-29 02:15:00+01:00
# 6   2017-10-29 02:45:00+01:00
# 7   2017-10-29 03:00:00+01:00
# dtype: datetime64[ns, Europe/Berlin]

关于python - 使用多种符号在夏令时期间本地化 Pandas 日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71779043/

相关文章:

python - 对 CSV Python 的嵌套 JSON 响应

c++ - 转换从公历开始到现在的天数

c# - 如何在 C# 中指定某个 DateTime 值是否在同一天?

python - 如何使用分组统计信息将列添加到数据框

python - 错误号 10061 : No connection could be made because the target machine actively refused it ( client - server )

Python != 操作 vs "is not"

python - Tensorflow,程序卡在 sess.run( ) 函数上

python - Pandas 将数据帧与包含日期时间的系列进行比较

python - Pandas dataframe groupby 到列表中,列表在单元格数据中

php - 使用 PHP 将 DateTime 插入 SQL Server