pandas.DatetimeIndex.tz_localize#

DatetimeIndex.tz_localize(tz, ambiguous='raise', nonexistent='raise')[源代码]#

将时区不敏感的Datetime Array/Index本地化为时区感知的Datetime Array/Index。

此方法接受一个时区未知 (tz-naive) 的 Datetime Array/Index 对象,并使其成为时区已知。它不会将时间移动到另一个时区。

此方法也可以用于反向操作——从一个时区已知的对象创建一个时区未知的对象。为此,请传递 tz=None

Parameters:
tzstr, pytz.timezone, dateutil.tz.tzfile, datetime.tzinfo 或 None

要将时间戳转换为的时区。传递 None 将移除时区信息,保留本地时间。

歧义 (ambiguous)‘infer’, ‘NaT’, bool 数组, 默认为 ‘raise’

当 DST 导致时钟倒转时,可能会出现歧义时间。 例如,在中欧时间(UTC+01),当从 03:00 DST 变为 02:00 非 DST 时,02:30:00 的本地时间会在 00:30:00 UTC 和 01:30:00 UTC 都出现。 在这种情况下,ambiguous 参数决定了如何处理歧义时间。

  • ‘infer’ 将尝试根据顺序推断DST转换的小时

  • 布尔 ndarray,其中 True 表示 DST 时间,False 表示非 DST 时间(请注意,此标志仅适用于模糊时间)

  • ‘NaT’ 将在有歧义时间的地方返回 NaT

  • 如果存在歧义时间,’raise’ 将引发 AmbiguousTimeError。

不存在 (nonexistent)‘shift_forward’, ‘shift_backward, ‘NaT’, timedelta, 默认为 ‘raise’

由于 DST 导致时钟向前移动,某个时区中不存在的某个时间点。

  • ‘shift_forward’ 会将不存在的时间向前移动到最近的可用时间。

  • ‘shift_backward’ 会将不存在的时间向后移动到最近的可用时间。

  • ‘NaT’ 在有不存在的时间时返回 NaT。

  • timedelta 对象会将不存在的时间按 timedelta 移动。

  • ‘raise’ 如果存在不存在的时间,则会引发一个 NonExistentTimeError。

Returns:
与 self 相同的类型

已转换为指定时区的 Array/Index。

Raises:
TypeError

如果 Datetime Array/Index 是时区已知的且 tz 不是 None。

参见

DatetimeIndex.tz_convert

将时区已知的 DatetimeIndex 从一个时区转换为另一个时区。

Examples

>>> tz_naive = pd.date_range('2018-03-01 09:00', periods=3)
>>> tz_naive
DatetimeIndex(['2018-03-01 09:00:00', '2018-03-02 09:00:00',
               '2018-03-03 09:00:00'],
              dtype='datetime64[ns]', freq='D')

将 DatetimeIndex 本地化为美国东部时间区:

>>> tz_aware = tz_naive.tz_localize(tz='US/Eastern')
>>> tz_aware
DatetimeIndex(['2018-03-01 09:00:00-05:00',
               '2018-03-02 09:00:00-05:00',
               '2018-03-03 09:00:00-05:00'],
              dtype='datetime64[ns, US/Eastern]', freq=None)

通过 tz=None,我们可以移除时区信息,同时保留本地时间(不转换为 UTC):

>>> tz_aware.tz_localize(None)
DatetimeIndex(['2018-03-01 09:00:00', '2018-03-02 09:00:00',
               '2018-03-03 09:00:00'],
              dtype='datetime64[ns]', freq=None)

请注意夏令时 (DST) 的更改。当有连续数据时,pandas 可以推断 DST 时间:

>>> s = pd.to_datetime(pd.Series(['2018-10-28 01:30:00',
...                               '2018-10-28 02:00:00',
...                               '2018-10-28 02:30:00',
...                               '2018-10-28 02:00:00',
...                               '2018-10-28 02:30:00',
...                               '2018-10-28 03:00:00',
...                               '2018-10-28 03:30:00']))
>>> s.dt.tz_localize('CET', ambiguous='infer')
0   2018-10-28 01:30:00+02:00
1   2018-10-28 02:00:00+02:00
2   2018-10-28 02:30:00+02:00
3   2018-10-28 02:00:00+01:00
4   2018-10-28 02:30:00+01:00
5   2018-10-28 03:00:00+01:00
6   2018-10-28 03:30:00+01:00
dtype: datetime64[ns, CET]

在某些情况下,无法推断 DST。在这种情况下,您可以将 ndarray 传递给 ambiguous 参数来显式设置 DST。

>>> s = pd.to_datetime(pd.Series(['2018-10-28 01:20:00',
...                               '2018-10-28 02:36:00',
...                               '2018-10-28 03:46:00']))
>>> s.dt.tz_localize('CET', ambiguous=np.array([True, True, False]))
0   2018-10-28 01:20:00+02:00
1   2018-10-28 02:36:00+02:00
2   2018-10-28 03:46:00+01:00
dtype: datetime64[ns, CET]

如果 DST 转换导致不存在的时间,您可以使用 timedelta 对象或 ‘shift_forward’‘shift_backwards’ 将这些日期向前或向后移动。

>>> s = pd.to_datetime(pd.Series(['2015-03-29 02:30:00',
...                               '2015-03-29 03:30:00']))
>>> s.dt.tz_localize('Europe/Warsaw', nonexistent='shift_forward')
0   2015-03-29 03:00:00+02:00
1   2015-03-29 03:30:00+02:00
dtype: datetime64[ns, Europe/Warsaw]
>>> s.dt.tz_localize('Europe/Warsaw', nonexistent='shift_backward')
0   2015-03-29 01:59:59.999999999+01:00
1   2015-03-29 03:30:00+02:00
dtype: datetime64[ns, Europe/Warsaw]
>>> s.dt.tz_localize('Europe/Warsaw', nonexistent=pd.Timedelta('1h'))
0   2015-03-29 03:30:00+02:00
1   2015-03-29 03:30:00+02:00
dtype: datetime64[ns, Europe/Warsaw]