Pandas时期 - Period
pd.Period()创建时期
生成一个以2017-01开始,月为频率的时间构造器:
p = pd.Period('2017', freq = 'M') print(p, type(p)) >>> 2017-01 <class 'pandas._period.Period'>
我们可以通过加减整数,将周期整体移动:
p = pd.Period('2017', freq = 'M') print(p, type(p)) print(p + 1) print(p - 2) >>> 2017-02 2016-11
pd.period_range()创建时期范围
创建指定时期范围:
prng = pd.period_range('1/1/2011', '1/1/2012', freq='M') print(prng,type(prng)) >>> PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05', '2011-06', '2011-07', '2011-08', '2011-09', '2011-10', '2011-11', '2011-12', '2012-01'], dtype='int64', freq='M') <class 'pandas.tseries.period.PeriodIndex'>
结合上面的时期序列,创建时间序列:
ts = pd.Series(np.random.rand(len(prng)), index = prng) print(ts,type(ts)) print(ts.index) >>> 2011-01 0.342571 2011-02 0.826151 2011-03 0.370505 2011-04 0.137151 2011-05 0.679976 2011-06 0.265928 2011-07 0.416502 2011-08 0.874078 2011-09 0.112801 2011-10 0.112504 2011-11 0.448408 2011-12 0.851046 2012-01 0.370605 Freq: M, dtype: float64 <class 'pandas.core.series.Series'> PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05', '2011-06', '2011-07', '2011-08', '2011-09', '2011-10', '2011-11', '2011-12', '2012-01'], dtype='int64', freq='M')
pd.period - asfreq:频率转换
通过.asfreq(freq, method=None, how=None)方法可以将之前生成的频率转换成别的频率
p = pd.Period('2017','A-DEC') print(p) print(p.asfreq('M', how = 'start')) # 也可写 how = 's' print(p.asfreq('D', how = 'end')) # 也可写 how = 'e' >>> 2017 2017-01 2017-12-31
asfreq也可以转换TIMESeries的index:
prng = pd.period_range('2017','2018',freq = 'M') ts1 = pd.Series(np.random.rand(len(prng)), index = prng) ts2 = pd.Series(np.random.rand(len(prng)), index = prng.asfreq('D', how = 'start')) print(ts1.head(),len(ts1)) print(ts2.head(),len(ts2))
时间戳与时期之间的转换
使用pd.to_period()、pd.to_timestamp()可以实现时间戳与时期之间的转换。
rng = pd.date_range('2017/1/1', periods = 10, freq = 'M') prng = pd.period_range('2017','2018', freq = 'M') ts1 = pd.Series(np.random.rand(len(rng)), index = rng) print(ts1.head()) print(ts1.to_period().head()) # 每月最后一日,转化为每月 ts2 = pd.Series(np.random.rand(len(prng)), index = prng) print(ts2.head()) print(ts2.to_timestamp().head()) # 每月,转化为每月第一天 >>> 2017-01-31 0.125288 2017-02-28 0.497174 2017-03-31 0.573114 2017-04-30 0.665665 2017-05-31 0.263561 Freq: M, dtype: float64 2017-01 0.125288 2017-02 0.497174 2017-03 0.573114 2017-04 0.665665 2017-05 0.263561 Freq: M, dtype: float64 2017-01 0.748661 2017-02 0.095891 2017-03 0.280341 2017-04 0.569813 2017-05 0.067677 Freq: M, dtype: float64 2017-01-01 0.748661 2017-02-01 0.095891 2017-03-01 0.280341 2017-04-01 0.569813 2017-05-01 0.067677 Freq: MS, dtype: float64
巩固练习
- 1:请输出以下时间序列,使用pd.period_range()