pandas.DatetimeIndex.tz_convert#
- DatetimeIndex.tz_convert(tz)[源代码]#
将时区感知的Datetime Array/Index从一个时区转换为另一个时区。
- Parameters:
- tzstr, pytz.timezone, dateutil.tz.tzfile, datetime.tzinfo 或 None
时间的时区。相应的 timestamps 将被转换为 Datetime Array/Index 的此时间时区。tz 为 None 将转换为 UTC 并删除时区信息。
- Returns:
- Array 或 Index
- Raises:
- TypeError
如果 Datetime Array/Index 是 tz-naive。
参见
DatetimeIndex.tz一个与 UTC 有可变偏移的时区。
DatetimeIndex.tz_localize将时区未知的 DatetimeIndex 本地化到给定时间区,或移除时区已知的 DatetimeIndex 的时区。
Examples
通过 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')