用法:
Timestamp.tz_localize(tz, ambiguous='raise', nonexistent='raise')
将原始时间戳转换为本地时区,或从 timezone-aware 时间戳中删除时区。
- tz:str、pytz.timezone、dateutil.tz.tzfile 或无
Timestamp 将转换到的时间的时区。 None 将删除保存本地时间的时区。
- ambiguous:bool, ‘NaT’, 默认 ‘raise’
当时钟由于 DST 向后移动时,可能会出现不明确的时间。例如,在欧洲中部时间 (UTC+01) 中,当从 03:00 DST 到 02:00 非 DST 时,当地时间 02:30:00 发生在 00:30:00 UTC 和 01:30:00世界标准时间。在这种情况下,
ambiguous
参数指示应如何处理模棱两可的时间。行为如下:
bool 包含用于确定时间是否为 dst 的标志(请注意,此标志仅适用于不明确的秋季 dst 日期)。
'NaT' 将返回 NaT 一段不明确的时间。
‘raise’ 将在不明确的时间内引发 AmbiguousTimeError。
- nonexistent:‘shift_forward’, ‘shift_backward, ‘NaT’, timedelta, 默认 ‘raise’
由于 DST,时钟向前移动的特定时区不存在不存在的时间。
行为如下:
‘shift_forward’ 会将不存在的时间向前移动到最接近的现有时间。
‘shift_backward’ 会将不存在的时间向后移动到最接近的现有时间。
‘NaT’ 将在不存在时间的地方返回 NaT。
timedelta 对象将按 timedelta 移动不存在的时间。
如果时间不存在,‘raise’ 将引发 NonExistentTimeError。
- localized:时间戳
- TypeError
如果时间戳为 tz-aware 且 tz 不是无。
参数:
返回:
抛出:
例子:
创建一个简单的时间戳对象:
>>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651') >>> ts Timestamp('2020-03-14 15:32:52.192548651')
添加“欧洲/斯德哥尔摩”作为时区:
>>> ts.tz_localize(tz='Europe/Stockholm') Timestamp('2020-03-14 15:32:52.192548651+0100', tz='Europe/Stockholm')
类似于
pd.NaT
:>>> pd.NaT.tz_localize() NaT
相关用法
- Python pandas.Timestamp.tz_convert用法及代码示例
- Python pandas.Timestamp.tz用法及代码示例
- Python pandas.Timestamp.to_numpy用法及代码示例
- Python pandas.Timestamp.timestamp用法及代码示例
- Python pandas.Timestamp.to_period用法及代码示例
- Python pandas.Timestamp.today用法及代码示例
- Python pandas.Timestamp.to_pydatetime用法及代码示例
- Python pandas.Timestamp.to_julian_date用法及代码示例
- Python pandas.Timestamp.dayofweek用法及代码示例
- Python pandas.Timestamp.dayofyear用法及代码示例
- Python pandas.Timestamp.strftime用法及代码示例
- Python pandas.Timestamp.month_name用法及代码示例
- Python pandas.Timestamp.ceil用法及代码示例
- Python pandas.Timestamp.days_in_month用法及代码示例
- Python pandas.Timestamp.asm8用法及代码示例
- Python pandas.Timestamp.is_quarter_end用法及代码示例
- Python pandas.Timestamp.daysinmonth用法及代码示例
- Python pandas.Timestamp.replace用法及代码示例
- Python pandas.Timestamp.round用法及代码示例
- Python pandas.Timestamp.is_quarter_start用法及代码示例
注:本文由纯净天空筛选整理自pandas.pydata.org大神的英文原创作品 pandas.Timestamp.tz_localize。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。