import pandas as pd
import numpy as np
import pytz
1.通过common_timezone可以获得所有时区的名称
pytz.common_timezones[-5:]
['US/Eastern', 'US/Hawaii', 'US/Mountain', 'US/Pacific', 'UTC']
2.通过timezone可以获取时间对象
tz = pytz.timezone('US/Eastern')
tz
1.pandas中的时间序列默认是naive时区,也就是没有时区的
rng = pd.date_range('3/9/2018 22:29',periods=5,freq='D') # 生成范围日期时可以加上时区信息
ts = pd.Series(np.random.randn(len(rng)),index=rng)
ts
2018-03-09 22:29:00 -0.422259
2018-03-10 22:29:00 -0.411570
2018-03-11 22:29:00 0.499641
2018-03-12 22:29:00 0.891193
2018-03-13 22:29:00 -0.966908
Freq: D, dtype: float64
2.通过tz_localize方法可以为没有时区的时间序列赋予时区
ts_utc = ts.tz_localize('UTC')
ts_utc
2018-03-09 22:29:00+00:00 -0.422259
2018-03-10 22:29:00+00:00 -0.411570
2018-03-11 22:29:00+00:00 0.499641
2018-03-12 22:29:00+00:00 0.891193
2018-03-13 22:29:00+00:00 -0.966908
Freq: D, dtype: float64
3.一旦时间序列被本地化到某个特定时区,就可以用tz_convert将其转换到别的时区了
ts_eastern = ts_utc.tz_convert('US/Eastern')
ts_eastern
2018-03-09 17:29:00-05:00 -0.422259
2018-03-10 17:29:00-05:00 -0.411570
2018-03-11 18:29:00-04:00 0.499641
2018-03-12 18:29:00-04:00 0.891193
2018-03-13 18:29:00-04:00 -0.966908
Freq: D, dtype: float64
ts_eastern.tz_convert('UTC')
2018-03-09 22:29:00+00:00 -0.422259
2018-03-10 22:29:00+00:00 -0.411570
2018-03-11 22:29:00+00:00 0.499641
2018-03-12 22:29:00+00:00 0.891193
2018-03-13 22:29:00+00:00 -0.966908
Freq: D, dtype: float64
4.tz_localize和tz_convert也是DatetimeIndex的实例方法
ts.index.tz_localize('Asia/Shanghai')
DatetimeIndex(['2018-03-09 22:29:00+08:00', '2018-03-10 22:29:00+08:00',
'2018-03-11 22:29:00+08:00', '2018-03-12 22:29:00+08:00',
'2018-03-13 22:29:00+08:00'],
dtype='datetime64[ns, Asia/Shanghai]', freq='D')
1.Timestamp对象也能被从navie本地化为时区意识型(time zone-aware)
stamp = pd.Timestamp('2018-7-5 22:40')
stamp
Timestamp('2018-07-05 22:40:00')
stamp_utc = stamp.tz_localize('utc')
2.创建Timestamp时,还可以传入一个时区信息
stamp_moscow = pd.Timestamp('2018-7-5 22:41',tz='Europe/Moscow')
stamp_moscow
Timestamp('2018-07-05 22:41:00+0300', tz='Europe/Moscow')
3.时区意识型Timestamp对象内部保存了一个utc时间戳值,这个值在时区的转换过程中是不会变化的
print(stamp_utc.value)
print(stamp_utc.tz_convert('US/Eastern').value)
1530830400000000000
1530830400000000000
4.当使用日期偏移量执行计算时,运算过程会自动关注是否存在夏令时转变期
from pandas.tseries.offsets import Hour
stamp = pd.Timestamp('2012-03-12 01:30',tz='US/Eastern')
print(stamp)
print(stamp+Hour()) # 不涉及夏令时转变期
2012-03-12 01:30:00-04:00
2012-03-12 02:30:00-04:00
stamp = pd.Timestamp('2012-11-04 00:30',tz='US/Eastern')
print(stamp)
print(stamp+Hour(2)) # 涉及夏令时转变期
2012-11-04 00:30:00-04:00
2012-11-04 01:30:00-05:00
两个时间序列的时区不同,在将它们合并到一起时,最终结果就会是UTC。
rng = pd.date_range('7/5/2018 22:50',periods=10,freq='B')
ts = pd.Series(np.random.randn(len(rng)),index=rng)
ts1 = ts[:7].tz_localize('Europe/London')
ts2 = ts1[2:].tz_convert('Europe/Moscow')
ts1
2018-07-05 22:50:00+01:00 0.816265
2018-07-06 22:50:00+01:00 -0.516242
2018-07-09 22:50:00+01:00 0.359166
2018-07-10 22:50:00+01:00 -2.238396
2018-07-11 22:50:00+01:00 -1.516365
2018-07-12 22:50:00+01:00 -1.734832
2018-07-13 22:50:00+01:00 -0.063531
Freq: B, dtype: float64
ts2
2018-07-10 00:50:00+03:00 0.359166
2018-07-11 00:50:00+03:00 -2.238396
2018-07-12 00:50:00+03:00 -1.516365
2018-07-13 00:50:00+03:00 -1.734832
2018-07-14 00:50:00+03:00 -0.063531
Freq: B, dtype: float64
result = ts1 + ts2
result
2018-07-05 21:50:00+00:00 NaN
2018-07-06 21:50:00+00:00 NaN
2018-07-09 21:50:00+00:00 0.718332
2018-07-10 21:50:00+00:00 -4.476792
2018-07-11 21:50:00+00:00 -3.032731
2018-07-12 21:50:00+00:00 -3.469663
2018-07-13 21:50:00+00:00 -0.127062
Freq: B, dtype: float64