如何将常规时间序列更改为夏令时时间序列?
How to change a regular timeseries to a date light saving timeseries?
我正在创建一个时间序列如下:
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
我该怎么做?
在创建日期时间索引时添加时区:
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')
我正在创建一个时间序列如下:
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
我该怎么做?
在创建日期时间索引时添加时区:
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')