pandas in the relevant time zone

pytz library

Time series processing more annoying is the processing time zone, in particular daylight saving time (DST) transition.
Many people choose to Coordinated Universal Time (UTC) for time series
in python pytz time zone information from the library.

	import datetime
	import pandas as pd
	import numpy as np
	
	
	import pytz
	r1 = pytz.common_timezones[-5:]
	# print(r1)  # ['US/Eastern', 'US/Hawaii', 'US/Mountain', 'US/Pacific', 'UTC']
	# 获取时区对象
	tz = pytz.timezone("US/Eastern")
	# print(tz)

Localization and Conversion

In case of default pd time zone is purely native time zone

	rng = pd.date_range('3/9/2012', periods=6, freq="D")
	# print(rng)
	ts = pd.Series(np.random.randn(len(rng)), index=rng)
	# print(ts)
	# 其索引的 tz 字段是 None
	# print(ts.index.tz)  # None
	# 在生成日期范围的时候可以加上时区集
	rng = pd.date_range('3/9/2012', periods=10, freq="D", tz="UTC")
	# print(rng)
	
	# 从单纯到本地化的转换是通过 tz_localize实现的
	# print(ts)
	ts_utc = ts.tz_localize("UTC")
	# print(ts_utc)
	# print(ts_utc.index)
	
	# 一旦时间序列被本地化到某个特定的时区,就可以用 tz_convert 转为别的时区
	ts_eas = ts_utc.tz_convert("US/Eastern")
	# print(ts_eas.index)
	
	# tz_localize 和 tz_convert 也是 DataFrameIndex 的实例方法
	ts_shanghai = ts.index.tz_localize("Asia/Shanghai")
	# print(ts_shanghai)

Operating time zone awareness Timestamp object type

	stamp = pd.Timestamp("2011-03-12 04:00")
	# print(stamp)
	stamp_utc = stamp.tz_localize("utc")
	# print(stamp_utc)
	eas_utc = stamp_utc.tz_convert("US/Eastern")
	# print(eas_utc)
	
	# 在创建 Timestamp 的时候,就可以传入一个时区信息
	stamp_moscow = pd.Timestamp("2011-03-12 04:00", tz="Europe/Moscow")
	# print(stamp_moscow)
	"""
	时区意识型TimeStamp对象在内部保留了一个 UTC 时间戳对象
	这个 UTC 值在时区的转换过程中是不会发生变化的 
	"""
	v1 = stamp_utc.value
	v2 = eas_utc.value
	v3 = stamp_moscow.value
	
	# print(v1 == v2)
	# print(v1 == v3)
	"""
	当使用 pd 的 DateOffset 对象执行算术运算 
	运算过程会自动关注是否存在夏令时转变期
	"""
	from pandas.tseries.offsets import Hour
	stamp = pd.Timestamp('2012-03-12 01:30', tz="US/Eastern")
	# print(stamp)
	# print(stamp.tz_convert("UTC"))
	
	# print()
	
	t2 = stamp + Hour()
	# print(t2)
	# print(t2.tz_convert('UTC'))

Operation between the different time zone

	"""
	不同的时区之间的运算 
	最终的结果会是 UTC
	
	由于时间戳其实是以 UTC 格式存储的,所以是一个很简单的运算,并不需要任何转换 
	"""
	rng = pd.date_range("3/7/2012 9:30", periods=10, freq="B")
	ts = pd.Series(np.random.randn(len(rng)), index=rng)
	# print(ts)
	
	ts1 = ts[:7].tz_localize("Europe/London")
	ts2 = ts1[2:].tz_convert("Europe/Moscow")
	
	# print(ts1)
	# print(ts2)
	res = ts1 + ts2
	print(res.index)

Guess you like

Origin blog.csdn.net/Enjolras_fuu/article/details/90730612