如何将常规时间序列更改为节省日期的时间序列?

问题描述 投票:0回答:1

我正在创建以下时间序列:

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

现在,我想基于Europe/Berlin中的夏时制修改此时间序列。含义:在2020年3月29日,时钟从29-03-2020 01:59:59跳转到29-03-2020 03:00:00,在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

我该怎么做?

python python-3.x pandas datetime dst
1个回答
0
投票

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

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')
© www.soinside.com 2019 - 2024. All rights reserved.