import pandas as pd
import numpy as np
一、时间类型及其在python中对应的类型
时间戳–timestamp
时间间隔–timedelta
时期–period
二、时期
时期表示的是时间区间,比如数日、数月、数季、数年等
1.定义一个Period
p = pd.Period(2007,freq='A-DEC') #表示以12月作为结束的一整年,这里表示从2007-01-01到2017-12-31的全年
p
Period('2007', 'A-DEC')
2.通过加减整数可以实现对Period的移动
p+5
Period('2012', 'A-DEC')
p-2
Period('2005', 'A-DEC')
3.如果两个Period对象拥有相同频率,则它们的差就是它们之间的单位数量
pd.Period('2014',freq='A-DEC') - p
4.period_range函数可用于创建规则的时期范围
rng = pd.period_range('1/1/2000','6/30/2000',freq='M') #创建从2001-01-01到2000-06-30所有月份的Period
pd.Series(np.random.randn(6),index=rng)
2000-01 -1.125053 2000-02 1.035250 2000-03 -0.796830 2000-04 0.381285 2000-05 0.533522 2000-06 -2.733462 Freq: M, dtype: float64
5.PeriodIndex类的构造函数允许直接使用一组字符串表示一段时期
values = ['2001Q3','2002Q2','2003Q1'] index = pd.PeriodIndex(values,freq='Q-DEC') index
PeriodIndex(['2001Q3', '2002Q2', '2003Q1'], dtype='period[Q-DEC]', freq='Q-DEC')
三、时期的频率转换-asfreq
1.通过asfreq可以将频率转换
p = pd.Period('2007',freq='A-DEC') # 2007年1月1日到2007年12月31日
p.asfreq('M',how='start') # 将评率为年(20070101-20071231)转换频率为月201701
Period('2007-01', 'M')
p.asfreq('M',how='end') # 将评率为年(20070101-20071231)转换频率为月201712
Period('2007-12', 'M')
2.不同频率经过asfreq转换后的结果不同
p = pd.Period('2007',freq='A-JUN') # 2006年7月1日到2007年6月30日
p.asfreq('D','start')
Period('2006-07-01', 'D')
p.asfreq('D','end')
Period('2007-06-30', 'D')
3.从高频率转换为低频率时,超时期(较大的时期)是由子时期(较小的时期)的位置绝对的
p = pd.Period('2007-08','M')
p.asfreq('A-JUN') # 200708对于频率A-JUN是属于2008年度的
Period('2008', 'A-JUN')
4.对于PeriodIndex或TimeSeries的频率转换方式相同
rng = pd.period_range('2006','2009',freq='A-DEC')
ts = pd.Series(np.random.randn(len(rng)),index=rng)
ts
2006 -1.202858 2007 -1.132553 2008 0.902564 2009 0.800859 Freq: A-DEC, dtype: float64
ts.asfreq('M',how='start')
2006-01 -1.202858 2007-01 -1.132553 2008-01 0.902564 2009-01 0.800859 Freq: M, dtype: float64
ts.asfreq('B',how='end')
2006-12-29 -1.202858 2007-12-31 -1.132553 2008-12-31 0.902564 2009-12-31 0.800859 Freq: B, dtype: float64
四、按季度计算的时期频率
许多季度型数据会涉及“财年末”的概念,通常是一年12个月中某月的最后一个工作日或日历日。因此,时间“2012Q4”根据财年末的不同会有不同的含义。pandas支持12种可能的季度型频率,即Q-JAN到Q-DEC。
1.财政年度和季度
p = pd.Period('2012Q4',freq='Q-JAN') # Q-JAN是指1月末的工作日是财政年末
p
Period('2012Q4', 'Q-JAN')
p.asfreq('D','start')
Period('2011-11-01', 'D')
p.asfreq('D','end')
Period('2012-01-31', 'D')
2.该季度倒数第二个工作日的下午4点
p4pm = (p.asfreq('B','e')-1).asfreq('T','s')+16*60
p4pm.to_timestamp()
Timestamp('2012-01-30 16:00:00')
3.相同的运算可以应用到TimeSeries
rng = pd.period_range('2011Q3','2012Q4',freq='Q-JAN') ts = pd.Series(np.arange(len(rng)),index=rng) ts
2011Q3 0 2011Q4 1 2012Q1 2 2012Q2 3 2012Q3 4 2012Q4 5 Freq: Q-JAN, dtype: int32
new_rng = (rng.asfreq('B','e')-1).asfreq('T','s')+16*60 ts.index = new_rng.to_timestamp() ts
2010-10-28 16:00:00 0 2011-01-28 16:00:00 1 2011-04-28 16:00:00 2 2011-07-28 16:00:00 3 2011-10-28 16:00:00 4 2012-01-30 16:00:00 5 dtype: int32
五、Timestamp与Period互相转换
1.通过to_period方法,可以将时间戳(timestamp)索引的Series和DataFrame对象转换为以时期(period)索引
rng = pd.date_range('1/1/2000',periods=3,freq='M') ts = pd.Series(np.random.randn(3),index=rng) ts
2000-01-31 -0.501502 2000-02-29 -1.299610 2000-03-31 -0.705091 Freq: M, dtype: float64
pts = ts.to_period()
pts
2000-01 -0.501502 2000-02 -1.299610 2000-03 -0.705091 Freq: M, dtype: float64
2.将timestamp转换为period是运行重复的
rng = pd.date_range('1/29/2000',periods=6,freq='D') ts2 = pd.Series(np.random.randn(6),index=rng) ts2.to_period('M')
2000-01 1.368367 2000-01 -0.256934 2000-01 0.417902 2000-02 -1.065910 2000-02 -1.694405 2000-02 0.665471 Freq: M, dtype: float64
3.to_timestamp可以将period转换为timestamp
pts.to_timestamp(how='end')
2000-01-31 -0.501502 2000-02-29 -1.299610 2000-03-31 -0.705091 Freq: M, dtype: float64
六、通过数组创建PeriodIndex
某些数据集中时间信息是分开在多个列存放的,可以通过PeriodIndex的参数将这些列组合在一起
year = [2017,2017,2017,2017,2018,2018,2018,2018] quarter = [1,2,3,4,1,2,3,4] index = pd.PeriodIndex(year=year,quarter=quarter,freq='Q-DEC') index
PeriodIndex(['2017Q1', '2017Q2', '2017Q3', '2017Q4', '2018Q1', '2018Q2', '2018Q3', '2018Q4'], dtype='period[Q-DEC]', freq='Q-DEC')
以上这篇Pandas时间序列:时期(period)及其算术运算详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]