Pandas时间序列:时期(period)及其算术运算详解

作者:BQW_ 时间:2022-09-25 07:34:38 

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')

来源:https://blog.csdn.net/bqw18744018044/article/details/80947243

标签:Pandas,时间序列,period,算术运算
0
投稿

猜你喜欢

  • 下载Internet Explorer 9 平台预览版4

    2010-08-05 20:59:00
  • python开发App基础操作API使用示例过程

    2022-01-19 18:00:35
  • 浅谈opencv自动光学检测、目标分割和检测(连通区域和findContours)

    2023-04-15 09:25:18
  • windows系统下Python环境的搭建(Aptana Studio)

    2021-03-16 20:40:37
  • python序列类型种类详解

    2022-02-27 13:53:38
  • Python中根据时间自动创建文件夹的代码实现

    2023-07-06 02:42:01
  • 驯服不听话的网页表格

    2007-12-03 11:36:00
  • 浅谈python requests 的put, post 请求参数的问题

    2023-05-06 14:54:47
  • PHP基于phpqrcode类库生成二维码过程解析

    2023-11-17 19:06:35
  • Python3.6正式版新特性预览

    2023-11-02 09:27:09
  • ASP访问统计计数器代码

    2008-03-20 13:33:00
  • python自然语言处理之字典树知识总结

    2023-12-29 01:01:21
  • Python中断多重循环的几种方式详解

    2022-05-09 21:44:04
  • Python机器学习应用之支持向量机的分类预测篇

    2023-08-29 20:42:55
  • 使用Python制作一盏 3D 花灯喜迎元宵佳节

    2021-08-15 06:35:43
  • python+pandas+时间、日期以及时间序列处理方法

    2021-09-08 04:46:32
  • python基于物品协同过滤算法实现代码

    2021-06-23 21:56:16
  • Python3自动生成MySQL数据字典的markdown文本的实现

    2021-04-29 15:21:18
  • Python中的__new__与__init__魔术方法理解笔记

    2021-12-18 14:29:26
  • celery在python爬虫中定时操作实例讲解

    2022-09-26 09:25:00
  • asp之家 网络编程 m.aspxhome.com