python - 如何将常规时间序列更改为夏令时时间序列?

标签 python python-3.x pandas datetime dst

我正在创建一个时间序列,如下所示:

import pandas as pd
dti = pd.date_range('2020-01-01 00:00', '2021-01-01 00:00', freq='0.25H')

现在,我想根据欧洲/柏林的夏令时修改此时间序列。含义:2020 年 3 月 29 日时钟从 29-03-2020 01:59:59 跳至 29-03-2020 03:00:00 code> 并且在 2020 年 10 月 25 日,时钟将从 25-10-2020 02:59:59 拨回一小时至 25-10-2020 02 :00:00 等等。

我尝试这样做:

dti_CEST = dti.tz_localize(tz='Europe/Berlin', ambiguous='infer')

但它会抛出以下错误:

 dti_CEST = dti.tz_localize(tz='Europe/Berlin', ambiguous='infer')
Traceback (most recent call last):

  File "<ipython-input-3-cb1d671c16a6>", line 1, in <module>
    dti_CEST = dti.tz_localize(tz='Europe/Berlin', ambiguous='infer')

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\accessor.py", line 93, in f
    return self._delegate_method(name, *args, **kwargs)

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\indexes\datetimelike.py", line 813, in _delegate_method
    result = operator.methodcaller(name, *args, **kwargs)(self._data)

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\arrays\datetimes.py", line 1151, in tz_localize
    self.asi8, tz, ambiguous=ambiguous, nonexistent=nonexistent

  File "pandas\_libs\tslibs\tzconversion.pyx", line 196, in pandas._libs.tslibs.tzconversion.tz_localize_to_utc

AmbiguousTimeError: 2020-10-25 02:00:00

我当前的时间序列dti:

date
#march
2020-03-29 00:00:00
2020-03-29 00:15:00
2020-03-29 00:30:00
2020-03-29 00:45:00
2020-03-29 01:00:00
2020-03-29 01:15:00
2020-03-29 01:30:00
2020-03-29 01:45:00
2020-03-29 02:00:00
2020-03-29 02:15:00
2020-03-29 02:30:00
2020-03-29 02:45:00
2020-03-29 03:00:00
2020-03-29 03:15:00
2020-03-29 03:30:00
2020-03-29 03:45:00
2020-03-29 04:00:00
# october
2020-10-25 00:00:00
2020-10-25 00:15:00
2020-10-25 00:30:00
2020-10-25 00:45:00
2020-10-25 01:00:00
2020-10-25 01:15:00
2020-10-25 01:30:00
2020-10-25 01:45:00
2020-10-25 02:00:00
2020-10-25 02:15:00
2020-10-25 02:30:00
2020-10-25 02:45:00
2020-10-25 03:00:00
2020-10-25 03:15:00
2020-10-25 03:30:00
2020-10-25 03:45:00
2020-10-25 04:00:00

所需输出

date
    #march
    2020-03-29 00:00:00
    2020-03-29 00:15:00
    2020-03-29 00:30:00
    2020-03-29 00:45:00
    2020-03-29 01:00:00
    2020-03-29 01:15:00
    2020-03-29 01:30:00
    2020-03-29 01:45:00
    2020-03-29 03:00:00 #changed here
    2020-03-29 03:15:00
    2020-03-29 03:30:00
    2020-03-29 03:45:00
    2020-03-29 04:00:00
    2020-03-29 04:15:00
    2020-03-29 04:30:00
    2020-03-29 04:45:00
    2020-03-29 05:00:00
    # october
    2020-10-25 00:00:00
    2020-10-25 00:15:00
    2020-10-25 00:30:00
    2020-10-25 00:45:00
    2020-10-25 01:00:00
    2020-10-25 01:15:00
    2020-10-25 01:30:00
    2020-10-25 01:45:00
    2020-10-25 02:00:00
    2020-10-25 02:15:00
    2020-10-25 02:30:00
    2020-10-25 02:45:00
    2020-10-25 02:00:00 #changed here
    2020-10-25 02:15:00
    2020-10-25 02:30:00
    2020-10-25 02:45:00
    2020-10-25 03:00:00

我该怎么做?

最佳答案

在创建日期时间索引时添加时区:

dti = pd.date_range('2020-01-01 00:00', '2021-01-01 00:00', freq='0.25H', tz='Europe/Berlin')

dti[8450:]

DatetimeIndex(['2020-03-29 00:30:00+01:00', '2020-03-29 00:45:00+01:00',
               '2020-03-29 01:00:00+01:00', '2020-03-29 01:15:00+01:00',
               '2020-03-29 01:30:00+01:00', '2020-03-29 01:45:00+01:00',
     ------->  '2020-03-29 03:00:00+02:00', '2020-03-29 03:15:00+02:00',
               '2020-03-29 03:30:00+02:00', '2020-03-29 03:45:00+02:00',
               ...
               '2020-12-31 21:45:00+01:00', '2020-12-31 22:00:00+01:00',
               '2020-12-31 22:15:00+01:00', '2020-12-31 22:30:00+01:00',
               '2020-12-31 22:45:00+01:00', '2020-12-31 23:00:00+01:00',
               '2020-12-31 23:15:00+01:00', '2020-12-31 23:30:00+01:00',
               '2020-12-31 23:45:00+01:00', '2021-01-01 00:00:00+01:00'],
              dtype='datetime64[ns, Europe/Berlin]', length=26687, freq='15T')

关于python - 如何将常规时间序列更改为夏令时时间序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61013042/

相关文章:

python - 计算具有 Nan 值的最频繁组

python - pandas dataframe,按另一列中的值排名,不使用 Python FOR 循环

python - 如果数据在不同时间段之间分割,如何正确连接 Pandas Series?

python - 使用 docker-compose 启动 Jupyter Notebook 时激活 Docker 容器内的 conda env

python - 如何在 Python 3.3 中复制自定义类的实例?

python-3.x - 将日期时间转换为没有时区信息的numpy日期时间

javascript - 在 Bokeh 上将文件从服务器发送到客户端

python - 通过弃用周期切换参数名称?

Python:如何读取具有所有属性的 XML 文件?

python - 如何使用 mysql 作为数据库在 Pyramid 中设置登录系统来存储电子邮件和密码?