在Series.tz_localize中使用“模棱两可”的bool参数不起作用

时间:2018-11-29 16:28:48

标签: python pandas

我试图使用 ambiguous 参数设置DatetimeIndex对象的时区,并用bool-ndarray填充它,如文档(https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.tz_localize.html)中所述:

这似乎不起作用,因为例如在使用 floor 时,我仍然收到无法推断出时间错误的错误:

import pandas as pd
import numpy as np
dates = pd.date_range(start='2018-11-04', end = '2018-11-05', freq = 'min')
dates.tz_localize(tz='US/Central', ambiguous = np.ones((1441,), dtype=bool)).floor('H')


File "pandas\_libs\tslibs\conversion.pyx", line 963, in pandas._libs.tslibs.conversion.tz_localize_to_utc

AmbiguousTimeError: Cannot infer dst time from '2018-11-04 01:00:00', try using the 'ambiguous' argument

但是,将 ambiguous 参数与非布尔值结合使用似乎可以:

dates.tz_localize(tz='US/Central', ambiguous = 'NaT').floor('H')

DatetimeIndex(['2018-11-04 00:00:00-05:00', '2018-11-04 00:00:00-05:00',
           '2018-11-04 00:00:00-05:00', '2018-11-04 00:00:00-05:00',
           '2018-11-04 00:00:00-05:00', '2018-11-04 00:00:00-05:00',
           '2018-11-04 00:00:00-05:00', '2018-11-04 00:00:00-05:00',
           '2018-11-04 00:00:00-05:00', '2018-11-04 00:00:00-05:00',
           ...
           '2018-11-04 23:00:00-06:00', '2018-11-04 23:00:00-06:00',
           '2018-11-04 23:00:00-06:00', '2018-11-04 23:00:00-06:00',
           '2018-11-04 23:00:00-06:00', '2018-11-04 23:00:00-06:00',
           '2018-11-04 23:00:00-06:00', '2018-11-04 23:00:00-06:00',
           '2018-11-04 23:00:00-06:00', '2018-11-05 00:00:00-06:00'],
          dtype='datetime64[ns, US/Central]', length=1441, freq=None)

这是为什么?您能解释一下如何使用布尔版本的参数吗?

0 个答案:

没有答案