pandas.Series.dt.tz_convert #

系列.dt。tz_convert ( * args , ** kwargs ) [来源] #

将 tz 感知的日期时间数组/索引从一个时区转换为另一个时区。

参数
tz str、pytz.timezone、dateutil.tz.tzfile、datetime.tzinfo 或 None

时间的时区。相应的时间戳将转换为日期时间数组/索引的该时区。None 的 tz将转换为 UTC 并删除时区信息。

返回
数组或索引
加薪
类型错误

如果日期时间数组/索引是 tz-naive。

也可以看看

DatetimeIndex.tz

与 UTC 之间存在可变偏移的时区。

DatetimeIndex.tz_localize

将 tz-naive DatetimeIndex 本地化为给定时区,或从 tz-aware DatetimeIndex 中删除时区。

例子

使用tz参数,我们可以将 DatetimeIndex 更改为其他时区:

>>> dti = pd.date_range(start='2014-08-01 09:00',
...                     freq='h', periods=3, tz='Europe/Berlin')
>>> dti
DatetimeIndex(['2014-08-01 09:00:00+02:00',
               '2014-08-01 10:00:00+02:00',
               '2014-08-01 11:00:00+02:00'],
              dtype='datetime64[ns, Europe/Berlin]', freq='h')
>>> dti.tz_convert('US/Central')
DatetimeIndex(['2014-08-01 02:00:00-05:00',
               '2014-08-01 03:00:00-05:00',
               '2014-08-01 04:00:00-05:00'],
              dtype='datetime64[ns, US/Central]', freq='h')

使用tz=None,我们可以删除时区(如有必要,在转换为 UTC 后):

>>> dti = pd.date_range(start='2014-08-01 09:00', freq='h',
...                     periods=3, tz='Europe/Berlin')
>>> dti
DatetimeIndex(['2014-08-01 09:00:00+02:00',
               '2014-08-01 10:00:00+02:00',
               '2014-08-01 11:00:00+02:00'],
                dtype='datetime64[ns, Europe/Berlin]', freq='h')
>>> dti.tz_convert(None)
DatetimeIndex(['2014-08-01 07:00:00',
               '2014-08-01 08:00:00',
               '2014-08-01 09:00:00'],
                dtype='datetime64[ns]', freq='h')