pandas.DataFrame.tz_localize#
- DataFrame.tz_localize(tz, axis=0, level=None, copy=None, ambiguous='raise', nonexistent='raise')[源代码]#
将Series或DataFrame的无时区索引本地化为目标时区。
此操作会本地化索引。 要本地化时区无关的 Series 中的值,请使用
Series.dt.tz_localize()。- Parameters:
- tz字符串或 tzinfo 或 None
要本地化的时区。 传递
None将删除时区信息并保留本地时间。- axis{0 或 ‘index’, 1 或 ‘columns’}, default 0
要本地化的轴
- level整数、字符串、默认 None
如果 axis 是 MultiIndex,则本地化特定级别。 否则必须是 None。
- copybool, default True
同时复制底层数据。
备注
copy 关键字在 pandas 3.0 中将更改行为。Copy-on-Write 将默认启用,这意味着所有带有 copy 关键字的方法都将使用惰性复制机制来延迟复制并忽略 copy 关键字。copy 关键字将在 pandas 的未来版本中移除。
通过启用 copy on write
pd.options.mode.copy_on_write = True,您可以获得未来的行为和改进。- 歧义 (ambiguous)‘infer’, bool-ndarray, ‘NaT’, 默认 ‘raise’
当 DST 导致时钟倒转时,可能会出现歧义时间。 例如,在中欧时间(UTC+01),当从 03:00 DST 变为 02:00 非 DST 时,02:30:00 的本地时间会在 00:30:00 UTC 和 01:30:00 UTC 都出现。 在这种情况下,ambiguous 参数决定了如何处理歧义时间。
‘infer’ 将尝试根据顺序推断DST转换的小时
bool-ndarray,其中 True 表示 DST 时间,False 表示非 DST 时间(请注意,此标志仅适用于歧义时间)
‘NaT’ 将在有歧义时间的地方返回 NaT
如果存在歧义时间,’raise’ 将引发 AmbiguousTimeError。
- 不存在 (nonexistent)字符串, 默认 ‘raise’
不存在的时间在特定时区不存在,因为DST导致时钟向前移动。 有效值是:
‘shift_forward’ 会将不存在的时间向前移动到最近的可用时间。
‘shift_backward’ 会将不存在的时间向后移动到最近的可用时间。
‘NaT’ 在有不存在的时间时返回 NaT。
timedelta 对象会将不存在的时间按 timedelta 移动。
‘raise’ 如果存在不存在的时间,则会引发一个 NonExistentTimeError。
- Returns:
- Series/DataFrame
与输入类型相同。
- Raises:
- TypeError
如果 TimeSeries 带有 timezone 信息且 tz 不为 None。
Examples
本地化本地时间:
>>> s = pd.Series( ... [1], ... index=pd.DatetimeIndex(['2018-09-15 01:30:00']), ... ) >>> s.tz_localize('CET') 2018-09-15 01:30:00+02:00 1 dtype: int64
传递 None 可将 timezone 转换为 naive 索引并保留本地时间:
>>> s = pd.Series([1], ... index=pd.DatetimeIndex(['2018-09-15 01:30:00+02:00'])) >>> s.tz_localize(None) 2018-09-15 01:30:00 1 dtype: int64
请注意夏令时 (DST) 的更改。当有连续数据时,pandas 可以推断 DST 时间:
>>> s = pd.Series(range(7), ... index=pd.DatetimeIndex(['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.tz_localize('CET', ambiguous='infer') 2018-10-28 01:30:00+02:00 0 2018-10-28 02:00:00+02:00 1 2018-10-28 02:30:00+02:00 2 2018-10-28 02:00:00+01:00 3 2018-10-28 02:30:00+01:00 4 2018-10-28 03:00:00+01:00 5 2018-10-28 03:30:00+01:00 6 dtype: int64
在某些情况下,无法推断 DST。在这种情况下,您可以将 ndarray 传递给 ambiguous 参数来显式设置 DST。
>>> s = pd.Series(range(3), ... index=pd.DatetimeIndex(['2018-10-28 01:20:00', ... '2018-10-28 02:36:00', ... '2018-10-28 03:46:00'])) >>> s.tz_localize('CET', ambiguous=np.array([True, True, False])) 2018-10-28 01:20:00+02:00 0 2018-10-28 02:36:00+02:00 1 2018-10-28 03:46:00+01:00 2 dtype: int64
如果 DST 转换导致不存在的时间,您可以使用 timedelta 对象或 ‘shift_forward’ 或 ‘shift_backward’ 将这些日期向前或向后移动。
>>> s = pd.Series(range(2), ... index=pd.DatetimeIndex(['2015-03-29 02:30:00', ... '2015-03-29 03:30:00'])) >>> s.tz_localize('Europe/Warsaw', nonexistent='shift_forward') 2015-03-29 03:00:00+02:00 0 2015-03-29 03:30:00+02:00 1 dtype: int64 >>> s.tz_localize('Europe/Warsaw', nonexistent='shift_backward') 2015-03-29 01:59:59.999999999+01:00 0 2015-03-29 03:30:00+02:00 1 dtype: int64 >>> s.tz_localize('Europe/Warsaw', nonexistent=pd.Timedelta('1h')) 2015-03-29 03:30:00+02:00 0 2015-03-29 03:30:00+02:00 1 dtype: int64