DataFrame.tz_convert(tz, axis=0, level=None, copy=True)[source]
将tz-aware axis转换为目标时区。
参数:tz:str或 tzinfo object
移动的周期数,可以是正的,也可以是负的。
axis :要转换的轴
level :int, str, 默认为None
如果axis是多索引,则转换特定级别。否则,一定为None。
copy :bool, 默认为 True
是否要复制基础数据。
返回值:{klass}
具有时区转换轴的对象。
Raises:TypeError
如果坐标轴是 tz-naive.
例子,
使用DataFrame.tz_convert()函数将给定数据帧的时区转换为'Europe/Berlin'。>>> import pandas as pd
>>> df = pd.DataFrame({'Weight':[45, 88, 56, 15, 71],
'Name':['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'],
'Age':[14, 25, 55, 8, 21]})
>>> index_ = pd.date_range('2010-10-09 08:45', periods = 5, freq ='H', tz = 'US/Central')
>>> df.index = index_
# 让我们看看现在的时区
# 给定的数据格式
>>> print(df.index)
DatetimeIndex(['2010-10-09 08:45:00-05:00', '2010-10-09 09:45:00-05:00',
'2010-10-09 10:45:00-05:00', '2010-10-09 11:45:00-05:00',
'2010-10-09 12:45:00-05:00'],
dtype='datetime64[ns, US/Central]', freq='H')
#属性的时区转换
# dataframe到 “欧洲/柏林”
>>> df = df.tz_convert(tz = 'Europe/Berlin')
#让我们找出当前的时区
#指定dataframe
>>> print(df.index)
DatetimeIndex(['2010-10-09 15:45:00+02:00', '2010-10-09 16:45:00+02:00',
'2010-10-09 17:45:00+02:00', '2010-10-09 18:45:00+02:00',
'2010-10-09 19:45:00+02:00'],
dtype='datetime64[ns, Europe/Berlin]', freq='H')