pandas完成时间序列分析基础

举报
北山啦 发表于 2021/04/20 22:57:03 2021/04/20
【摘要】 pandas时间序列分析的基本操作方法 推荐阅读 使用Python完成时间序列分析基础Python建立时间序列ARIMA模型实战案例SPSS建立时间序列乘法季节模型实战案例SPSS建立时间序列加法季节模型实战案例SPSS建立时间序列疏系数模型 文章目录 导入需要的库时间序列生成时间序列truncate过滤时间戳时间区间指定索引时间戳和时间...

pandas时间序列分析的基本操作方法


推荐阅读

  1. 使用Python完成时间序列分析基础
  2. Python建立时间序列ARIMA模型实战案例
  3. SPSS建立时间序列乘法季节模型实战案例
  4. SPSS建立时间序列加法季节模型实战案例
  5. SPSS建立时间序列疏系数模型


在这里插入图片描述

导入需要的库

import pandas as pd
import numpy as np
import datetime as dt

  
 
  • 1
  • 2
  • 3

时间序列

  • 时间戳(timestamp)
  • 固定周期(period)
  • 时间间隔(interval)
    在这里插入图片描述

生成时间序列

  • 可以指定开始时间与周期
  • H:小时
  • D:天
  • M:月
# TIMES #2016 Jul 1 7/1/2016 1/7/2016 2016-07-01 2016/07/01
rng = pd.date_range('2016-07-01', periods = 10, freq = '3D')
rng

  
 
  • 1
  • 2
  • 3
DatetimeIndex(['2016-07-01', '2016-07-04', '2016-07-07', '2016-07-10', '2016-07-13', '2016-07-16', '2016-07-19', '2016-07-22', '2016-07-25', '2016-07-28'], dtype='datetime64[ns]', freq='3D')

  
 
  • 1
  • 2
  • 3
  • 4
time=pd.Series(np.random.randn(20), index=pd.date_range(dt.datetime(2016,1,1),periods=20))
print(time)

  
 
  • 1
  • 2
  • 3
2016-01-01   -0.067209
2016-01-02 0.480689
2016-01-03   -0.152052
2016-01-04 0.077139
2016-01-05   -1.775043
2016-01-06   -1.184273
Freq: D, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

truncate过滤

time.truncate(before='2016-1-10')

  
 
  • 1
2016-01-10   -0.349605
2016-01-11 2.159193
2016-01-12 0.077578
2016-01-13 0.084981
2016-01-14   -0.099995
2016-01-15   -1.327124
2016-01-16 1.352626
Freq: D, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
time.truncate(after='2016-1-10')

  
 
  • 1
2016-01-01   -0.067209
2016-01-02 0.480689
2016-01-03   -0.152052
2016-01-04 0.077139
2016-01-05   -1.775043
2016-01-06   -1.184273
2016-01-07   -1.247371
2016-01-08   -0.686737
2016-01-09   -1.787544
2016-01-10   -0.349605
Freq: D, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
print(time['2016-01-15'])

  
 
  • 1
-1.3271240245020821

  
 
  • 1
print(time['2016-01-15':'2016-01-20'])

  
 
  • 1
2016-01-15   -1.327124
2016-01-16 1.352626
2016-01-17   -0.075599
2016-01-18 1.026780
2016-01-19   -0.286614
2016-01-20   -0.017546
Freq: D, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
data=pd.date_range('2010-01-01','2011-01-01',freq='M')
print(data)

  
 
  • 1
  • 2
DatetimeIndex(['2010-01-31', '2010-02-28', '2010-03-31', '2010-04-30', '2010-05-31', '2010-06-30', '2010-07-31', '2010-08-31', '2010-09-30', '2010-10-31', '2010-11-30', '2010-12-31'], dtype='datetime64[ns]', freq='M')

  
 
  • 1
  • 2
  • 3
  • 4

常见的格式
在这里插入图片描述

时间戳

pd.Timestamp('2016-07-10')

  
 
  • 1
Timestamp('2016-07-10 00:00:00')

  
 
  • 1
# 可以指定更多细节
pd.Timestamp('2016-07-10 10')

  
 
  • 1
  • 2
Timestamp('2016-07-10 10:00:00')

  
 
  • 1
pd.Timestamp('2016-07-10 10:15')

  
 
  • 1
Timestamp('2016-07-10 10:15:00')

  
 
  • 1
# How much detail can you add?

  
 
  • 1
t = pd.Timestamp('2016-07-10 10:15')

  
 
  • 1

时间区间

pd.Period('2016-01')

  
 
  • 1
Period('2016-01', 'M')

  
 
  • 1
pd.Period('2016-01-01')

  
 
  • 1
Period('2016-01-01', 'D')

  
 
  • 1
# TIME OFFSETS
pd.Timedelta('1 day')

  
 
  • 1
  • 2
Timedelta('1 days 00:00:00')

  
 
  • 1
pd.Period('2016-01-01 10:10') + pd.Timedelta('1 day')

  
 
  • 1
Period('2016-01-02 10:10', 'T')

  
 
  • 1
pd.Timestamp('2016-01-01 10:10') + pd.Timedelta('1 day')

  
 
  • 1
Timestamp('2016-01-02 10:10:00')

  
 
  • 1
pd.Timestamp('2016-01-01 10:10') + pd.Timedelta('15 ns')

  
 
  • 1
Timestamp('2016-01-01 10:10:00.000000015')

  
 
  • 1
p1 = pd.period_range('2016-01-01 10:10', freq = '25H', periods = 10)

  
 
  • 1
p2 = pd.period_range('2016-01-01 10:10', freq = '1D1H', periods = 10)

  
 
  • 1
p1

  
 
  • 1
PeriodIndex(['2016-01-01 10:00', '2016-01-02 11:00', '2016-01-03 12:00', '2016-01-04 13:00', '2016-01-05 14:00', '2016-01-06 15:00', '2016-01-07 16:00', '2016-01-08 17:00', '2016-01-09 18:00', '2016-01-10 19:00'], dtype='period[25H]', freq='25H')

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
p2

  
 
  • 1
PeriodIndex(['2016-01-01 10:00', '2016-01-02 11:00', '2016-01-03 12:00', '2016-01-04 13:00', '2016-01-05 14:00', '2016-01-06 15:00', '2016-01-07 16:00', '2016-01-08 17:00', '2016-01-09 18:00', '2016-01-10 19:00'], dtype='period[25H]', freq='25H')

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

指定索引

rng = pd.date_range('2016 Jul 1', periods = 10, freq = 'D')
rng
pd.Series(range(len(rng)), index = rng)

  
 
  • 1
  • 2
  • 3
2016-07-01 0
2016-07-02 1
2016-07-03 2
2016-07-04 3
2016-07-05 4
2016-07-06 5
2016-07-07 6
2016-07-08 7
2016-07-09 8
2016-07-10 9
Freq: D, dtype: int64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
periods = [pd.Period('2016-01'), pd.Period('2016-02'), pd.Period('2016-03')]
ts = pd.Series(np.random.randn(len(periods)), index = periods)
ts

  
 
  • 1
  • 2
  • 3
2016-01   -0.559086
2016-02   -1.021617
2016-03 0.944657
Freq: M, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
type(ts.index)

  
 
  • 1
pandas.core.indexes.period.PeriodIndex

  
 
  • 1

时间戳和时间周期可以转换


ts = pd.Series(range(10), pd.date_range('07-10-16 8:00', periods = 10, freq = 'H'))
ts

  
 
  • 1
  • 2
  • 3
2016-07-10 08:00:00 0
2016-07-10 09:00:00 1
2016-07-10 10:00:00 2
2016-07-10 11:00:00 3
2016-07-10 12:00:00 4
Freq: H, dtype: int64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
ts_period = ts.to_period()
ts_period

  
 
  • 1
  • 2
2016-07-10 08:00 0
2016-07-10 09:00 1
2016-07-10 10:00 2
2016-07-10 11:00 3
2016-07-10 12:00 4
2016-07-10 13:00 5
2016-07-10 14:00 6
2016-07-10 15:00 7
2016-07-10 16:00 8
2016-07-10 17:00 9
Freq: H, dtype: int64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
ts_period['2016-07-10 08:30':'2016-07-10 11:45'] 

  
 
  • 1
2016-07-10 08:00 0
2016-07-10 09:00 1
2016-07-10 10:00 2
2016-07-10 11:00 3
Freq: H, dtype: int64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
ts['2016-07-10 08:30':'2016-07-10 11:45'] 

  
 
  • 1
2016-07-10 09:00:00 1
2016-07-10 10:00:00 2
2016-07-10 11:00:00 3
Freq: H, dtype: int64

  
 
  • 1
  • 2
  • 3
  • 4

数据重采样

  • 时间数据由一个频率转换到另一个频率
  • 降采样
  • 升采样
import pandas as pd
import numpy as np
rng = pd.date_range('1/1/2011', periods=90, freq='D')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
ts.head()

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
2011-01-01   -0.225796
2011-01-02 0.890969
2011-01-03   -0.343222
2011-01-04   -0.884985
2011-01-05 0.859801
Freq: D, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

重采样resample

  • 以月为单位
ts.resample('M').sum()
ts.resample("M").sum()

  
 
  • 1
  • 2
2011-01-31   -3.221512
2011-02-28 9.660282
2011-03-31   -0.934169
Freq: M, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
  • 以天为单位
ts.resample('3D').sum()
ts.resample("2D").sum()

  
 
  • 1
  • 2
2011-01-01 0.665173
2011-01-03   -1.228207
2011-01-05 1.165821
2011-01-07   -2.507237
Freq: 2D, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 计算均值
day3Ts = ts.resample('3D').mean()
day3Ts

  
 
  • 1
  • 2
2011-01-01 0.107317
2011-01-04 0.093612
2011-01-07   -1.156626
2011-01-10   -0.172981

Freq: 3D, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • resample()重采样和asfreq()频度转换
print(day3Ts.resample('D').asfreq())

  
 
  • 1
2011-01-01 0.107317
2011-01-02 NaN
2011-01-03 NaN
2011-01-04 0.093612
2011-01-05 NaN ... 2011-03-25 NaN
2011-03-26 0.804057
2011-03-27 NaN
2011-03-28 NaN
2011-03-29   -0.200729
Freq: D, Length: 88, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
print(day3Ts.resample('D'))

  
 
  • 1
DatetimeIndexResampler [freq=<Day>, axis=0, closed=left, label=left, convention=start, base=0]

  
 
  • 1

插值方法

升采样可能出现问题,对于控制使用插值方法

  • ffill 空值取前面的值
    bfill 空值取后面的值
    interpolate 线性取值
day3Ts.resample('D').ffill(2)

  
 
  • 1
2011-01-01 0.107317
2011-01-02 0.107317
2011-01-03 0.107317
2011-01-04 0.093612
2011-01-05 0.093612 ... 2011-03-25   -0.045712
2011-03-26 0.804057
2011-03-27 0.804057
2011-03-28 0.804057
2011-03-29   -0.200729
Freq: D, Length: 88, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
day3Ts.resample('D').bfill(1)

  
 
  • 1
2011-01-01 0.107317
2011-01-02 NaN
2011-01-03 0.093612
2011-01-04 0.093612
2011-01-05 NaN ... 2011-03-25 0.804057
2011-03-26 0.804057
2011-03-27 NaN
2011-03-28   -0.200729
2011-03-29   -0.200729
Freq: D, Length: 88, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
day3Ts.resample('D').interpolate("linear")

  
 
  • 1
2011-01-01 0.107317
2011-01-02 0.102749
2011-01-03 0.098180
2011-01-04 0.093612
2011-01-05   -0.323134 ... 2011-03-25 0.520801
2011-03-26 0.804057
2011-03-27 0.469128
2011-03-28 0.134200
2011-03-29   -0.200729
Freq: D, Length: 88, dtype: float64

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

推荐阅读

  1. 使用Python完成时间序列分析基础
  2. Python建立时间序列ARIMA模型实战案例
  3. SPSS建立时间序列乘法季节模型实战案例
  4. SPSS建立时间序列加法季节模型实战案例
  5. SPSS建立时间序列疏系数模型

到这里就结束了,如果对你有帮助你,欢迎点赞关注,你的点赞对我很重要

文章来源: blog.csdn.net,作者:北山啦,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_45176548/article/details/109588361

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。