Python 3 - pd.to_datetime 无法识别时区

标签 python pandas timezone

我有以下日期时间列,它可以识别“太平洋/奥克兰”的时区:

startTime = ['2018-10-10 23:00:00+1300'
     , '2018-10-11 04:00:00+1300'
     , '2018-10-10 17:30:00+1300'
     , '2018-10-10 17:30:00+1300'
     , '2018-10-11 04:00:00+1300'
     , '2018-10-10 19:00:00+1300']

当我将 pd.to_datetime 应用于该列时,时间不会相应地转换。

我得到这个:

pd.to_datetime(startTime)

DatetimeIndex(['2018-10-10 10:00:00'
             , '2018-10-10 15:00:00'
             , '2018-10-10 04:30:00'
             , '2018-10-10 04:30:00'
             , '2018-10-10 15:00:00'
             , '2018-10-10 06:00:00']
             , dtype='datetime64[ns]'
             , freq=None)

我预计+13小时会被添加而不是被减去,这样我就可以给出新西兰奥克兰的本地时间:

DatetimeIndex(['2018-10-11 12:00:00'
             , '2018-10-11 17:00:00'
             , ‘2018-10-11 06:30:00'
             , ‘2018-10-11 06:30:00'
             , ‘2018-10-11 17:00:00'
             , ‘2018-10-11 08:00:00']
             , dtype='datetime64[ns]'
             , freq=None)

有人可以帮我理解为什么 pd.to_datetime 减去 13 小时而不是相加吗?

最佳答案

此问题已 previously raised在 Pandas 开发中,并在 #13712 下进行跟踪.

目前,一种解决方法是将时区感知字符串转换为 pd.Timestamp ,它会正确推断时区,然后应用 pd.to_datetime

from pandas import Timestamp
converted = [Timestamp(t) for t in startTime]
pd.to_datetime(converted)

这给出:

DatetimeIndex(['2018-10-10 23:00:00+13:00', '2018-10-11 04:00:00+13:00',
               '2018-10-10 17:30:00+13:00', '2018-10-10 17:30:00+13:00',
               '2018-10-11 04:00:00+13:00', '2018-10-10 19:00:00+13:00'],
              dtype='datetime64[ns, pytz.FixedOffset(780)]', freq=None)

关于Python 3 - pd.to_datetime 无法识别时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52771707/

相关文章:

python - 如何从图像上具有不同值的不同像素的坐标数据框创建热图

python - pandas left join - 为什么会有更多结果?

google-apps-script - Google 电子表格中的时区转换

python - 在Python的C api中解析无符号整数(uint32_t)

python - Altair:使用 binding_select 时选择所有值

python - 如何在不影响安全性的情况下授予 celery 足够的权限来运行根文件?

python - 将计算列添加到 Pandas 数据框

python - Pandas HDFStore 从内存中卸载数据帧

javascript - 使用 momentjs 在最终用户的时区进行操作

php - 无法使用 MySQL 中的 CONVERT_TZ 函数将 GMT 时区转换为 PDT