maxframe.dataframe.Series.dt.tz_localize#
- Series.dt.tz_localize(tz, ambiguous: TimeAmbiguous = 'raise', nonexistent: TimeNonexistent = 'raise') Self#
将无时区的 Datetime Array/Index 本地化为有时区的 Datetime Array/Index。
此方法接受一个无时区(tz)的 Datetime Array/Index 对象,并使其具有时区信息。它不会将时间移动到另一个时区。
此方法也可用于反向操作——从有时区的对象创建一个无时区的对象。为此,请传入 tz=None。
- 参数:
tz (str, zoneinfo.ZoneInfo,, pytz.timezone, dateutil.tz.tzfile, datetime.tzinfo or None) -- 要将时间戳转换到的时区。传入
None将移除时区信息,保留本地时间。ambiguous ('infer', 'NaT', bool array, default 'raise') --
当由于夏令时导致时钟向后移动时,可能会出现时间歧义。例如在中欧时间(UTC+01)中,从03:00夏令时转换到02:00非夏令时时,02:30:00本地时间会在00:30:00 UTC和01:30:00 UTC两个时刻都出现。在这种情况下,`ambiguous`参数决定了应该如何处理这些时间歧义。
'infer' 将尝试根据顺序推断秋季 dst-转换小时
bool-ndarray,其中 True 表示 DST 时间,False 表示非 DST 时间(注意该标志仅适用于歧义时间)
'NaT' 将在出现歧义时间处返回 NaT
'raise' will raise a ValueError if there are ambiguous times.
nonexistent ('shift_forward', 'shift_backward, 'NaT', timedelta, default 'raise') --
不存在的时间在特定时区中不存在,当时钟因 DST 向前移动时。
'shift_forward' 将不存在的时间向前移动到最接近的 存在时间
'shift_backward' 将不存在的时间向后移动到最接近的 存在时间
'NaT' 将在存在不存在时间处返回 NaT
timedelta 对象将通过 timedelta 移动不存在的时间
'raise' will raise a ValueError if there are nonexistent times.
- 返回:
已转换到指定时区的 Array/Index。
- 返回类型:
Same type as self
- 抛出:
TypeError -- 如果 Datetime Array/Index 有时区信息且 tz 不为 None。
参见
DatetimeIndex.tz_convert将有时区信息的 DatetimeIndex 从一个时区转换到另一个时区。
示例
>>> import maxframe.tensor as mt >>> import maxframe.dataframe as md >>> tz_naive = md.date_range('2018-03-01 09:00', periods=3) >>> tz_naive.execute() DatetimeIndex(['2018-03-01 09:00:00', '2018-03-02 09:00:00', '2018-03-03 09:00:00'], dtype='datetime64[us]', freq='D')
将 DatetimeIndex 本地化到 US/Eastern 时区:
>>> tz_aware = tz_naive.tz_localize(tz='US/Eastern') >>> tz_aware.execute() 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[us, US/Eastern]', freq=None)
使用
tz=None,我们可以移除时区信息并保留本地时间(不会转换为 UTC):>>> tz_aware.tz_localize(None).execute() DatetimeIndex(['2018-03-01 09:00:00', '2018-03-02 09:00:00', '2018-03-03 09:00:00'], dtype='datetime64[us]', freq=None)
小心DST变化。当有连续数据时,pandas可以推断DST时间:
>>> s = md.to_datetime(md.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').execute() 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[us, CET]
在某些情况下,推断DST是不可能的。在这种情况下,您可以将ndarray传递给ambiguous参数来显式设置DST
>>> s = md.to_datetime(md.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=mt.array([True, True, False])).execute() 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[us, CET]
如果DST转换导致不存在的时间,您可以使用timedelta对象或'shift_forward'或'shift_backwards'将这些日期向前或向后移动。
>>> s = md.to_datetime(md.Series(['2015-03-29 02:30:00', ... '2015-03-29 03:30:00'], dtype="M8[ns]")) >>> s.dt.tz_localize('Europe/Warsaw', nonexistent='shift_forward').execute() 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').execute() 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=md.Timedelta('1h')).execute() 0 2015-03-29 03:30:00+02:00 1 2015-03-29 03:30:00+02:00 dtype: datetime64[ns, Europe/Warsaw]