admin管理员组文章数量:1595877
【课程2.11】 Pandas时期:Period
核心:pd.Period()
1.pd.Period()创建时期
p = pd.Period('2017', freq = 'M')
print(p, type(p))
# 生成一个以2017-01开始,月为频率的时间构造器
# pd.Period()参数:一个时间戳 + freq 参数 → freq 用于指明该 period 的长度,时间戳则说明该 period 在时间轴上的位置
print(p + 1)
print(p - 2)
print(pd.Period('2012', freq = 'A-DEC') - 1)
# 通过加减整数,将周期整体移动
# 这里是按照 月、年 移动
-----------------------------------------------------------------------
2017-01 <class 'pandas._period.Period'>
2017-02
2016-11
2011
2.pd.period_range()创建时期范围
prng = pd.period_range('1/1/2011', '1/1/2012', freq='M')
print(prng,type(prng))
print(prng[0],type(prng[0]))
# 数据格式为PeriodIndex,单个数值为Period
ts = pd.Series(np.random.rand(len(prng)), index = prng)
print(ts,type(ts))
print(ts.index)
# 时间序列
# Period('2011', freq = 'A-DEC')可以看成多个时间期的时间段中的游标
# Timestamp表示一个时间戳,是一个时间截面;Period是一个时期,是一个时间段!!但两者作为index时区别不大
-----------------------------------------------------------------------
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'>
2011-01 <class 'pandas._period.Period'>
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')
3.asfreq:频率转换
p = pd.Period('2017','A-DEC')
print(p)
print(p.asfreq('M', how = 'start')) # 也可写 how = 's'
print(p.asfreq('D', how = 'end')) # 也可写 how = 'e'
# 通过.asfreq(freq, method=None, how=None)方法转换成别的频率
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))
# asfreq也可以转换TIMESeries的index
-----------------------------------------------------------------------
2017
2017-01
2017-12-31
2017-01 0.060797
2017-02 0.441994
2017-03 0.971933
2017-04 0.000334
2017-05 0.545191
Freq: M, dtype: float64 13
2017-01-01 0.447614
2017-02-01 0.679438
2017-03-01 0.891729
2017-04-01 0.949993
2017-05-01 0.942548
Freq: D, dtype: float64 13
4.时间戳与时期之间的转换: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
版权声明:本文标题:Python数据分析实战【第三章】2.11- Pandas时期:Period【python】 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1728236779a1150511.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论