0
点赞
收藏
分享

微信扫一扫

【python数据分析(19)】Pandas中时间序列处理(5) 时间序列数据重采样resample()方法的使用


1. 时间序列数据重采样概念

将时间序列从一个频率转换为另一个频率的过程,且会有数据的结合的过程

之前涉及到频率的转换只是单纯的改变频率,而并没有改变时间标签所对应的数值信息,比如在获得每天的销售信息时候,想要进行每个月甚至每年的销售汇总时候,就需要使用时间序列重采样

​ts.resample('5D').sum()​​​:得到一个新的聚合后的​​Series​​​,聚合方式为求和
​​​freq​​​:重采样频率 → ​​ts.resample('5D')​​​​.sum()​​:聚合方法

import pandas as pd
import numpy as np

rng = pd.date_range('20200101', periods = 12)
ts = pd.Series(np.arange(12), index = rng)
print(ts)

ts_re = ts.resample('5D')
ts_re2 = ts.resample('5D').sum()
print(ts_re, type(ts_re))
print(ts_re2, type(ts_re2))

–> 输出的结果为:(如果后面改变频率之后不加方法,就只是一个采样构建器,频率改为5天,要输出的话后面需要接方法)

2020-01-01     0
2020-01-02 1
2020-01-03 2
2020-01-04 3
2020-01-05 4
2020-01-06 5
2020-01-07 6
2020-01-08 7
2020-01-09 8
2020-01-10 9
2020-01-11 10
2020-01-12 11
Freq: D, dtype: int32

DatetimeIndexResampler [freq=<5 * Days>, axis=0, closed=left, label=left, convention=start, base=0]
<class 'pandas.core.resample.DatetimeIndexResampler'>

2020-01-01 10
2020-01-06 35
2020-01-11 21
Freq: 5D, dtype: int32
<class 'pandas.core.series.Series'>

其他的聚合方法

print(ts.resample('5D').mean(),'→ 求平均值\n')
print(ts.resample('5D').max(),'→ 求最大值\n')
print(ts.resample('5D').min(),'→ 求最小值\n')
print(ts.resample('5D').median(),'→ 求中值\n')
print(ts.resample('5D').first(),'→ 返回第一个值\n')
print(ts.resample('5D').last(),'→ 返回最后一个值\n')
print(ts.resample('5D').ohlc(),'→ OHLC重采样\n')
# OHLC:金融领域的时间序列聚合方式 → open开盘、high最大值、low最小值、close收盘

–> 输出的结果为:

2020-01-01     2.0
2020-01-06 7.0
2020-01-11 10.5
Freq: 5D, dtype: float64 求平均值

2020-01-01 4
2020-01-06 9
2020-01-11 11
Freq: 5D, dtype: int32 求最大值

2020-01-01 0
2020-01-06 5
2020-01-11 10
Freq: 5D, dtype: int32 求最小值

2020-01-01 2.0
2020-01-06 7.0
2020-01-11 10.5
Freq: 5D, dtype: float64 求中值

2020-01-01 0
2020-01-06 5
2020-01-11 10
Freq: 5D, dtype: int32 返回第一个值

2020-01-01 4
2020-01-06 9
2020-01-11 11
Freq: 5D, dtype: int32 返回最后一个值

open high low close
2020-01-01 0 4 0 4
2020-01-06 5 9 5 9
2020-01-11 10 11 10 11

2. 降采样

import pandas as pd
import numpy as np

rng = pd.date_range('20200110', periods = 12)
ts = pd.Series(np.arange(1,13), index = rng)
print(ts)

print(ts.resample('5D').sum(),'→ 默认\n')
print(ts.resample('5D', closed = 'left').sum(),'→ left\n')
print(ts.resample('5D', closed = 'right').sum(),'→ right\n')

​closed​​​:各时间段哪一端是闭合(即包含)的,默认 左闭右闭
这里​​​values​​​为1-12,按照​​5D​​​重采样 → [1,2,3,4,5],[6,7,8,9,10],[11,12]
​​​left​​​指定间隔左边为结束 → [1,2,3,4,5],[6,7,8,9,10],[11,12]
​​​right​​指定间隔右边为结束 → [1],[2,3,4,5,6],[7,8,9,10,11],[12]

输出结果为:(要特别注意当使用参数​​right​​时,间隔右边为结束,由于是以5D为频率的,所以第一个数据就是五天前,对应着1-05,然后取值就是1)

2020-01-10     1
2020-01-11 2
2020-01-12 3
2020-01-13 4
2020-01-14 5
2020-01-15 6
2020-01-16 7
2020-01-17 8
2020-01-18 9
2020-01-19 10
2020-01-20 11
2020-01-21 12
Freq: D, dtype: int32
2020-01-10 15
2020-01-15 40
2020-01-20 23
Freq: 5D, dtype: int32 默认

2020-01-10 15
2020-01-15 40
2020-01-20 23
Freq: 5D, dtype: int32 left

2020-01-05 1
2020-01-10 20
2020-01-15 45
2020-01-20 12
Freq: 5D, dtype:

​label​​​:聚合值的​​index​​,默认为取左,这种才是常见的整合的方式

print(ts.resample('5D', label = 'left').sum(),'→ leftlabel\n')
print(ts.resample('5D', label = 'right').sum(),'→ rightlabel')

输出结果为:(注意和上边的对比)

2020-01-10    15
2020-01-15 40
2020-01-20 23
Freq: 5D, dtype: int32 leftlabel

2020-01-15 15
2020-01-20 40
2020-01-25 23
Freq: 5D, dtype:

3. 升采样及插值

低频转高频,主要是如何插值
​​​.asfreq()​​​:不做填充,返回Nan
​​​.ffill()​​​:向上填充
​​​.bfill()​​:向下填充

rng = pd.date_range('2017/1/1 0:0:0', periods = 5, freq = 'H')
ts = pd.DataFrame(np.arange(15).reshape(5,3),
index = rng,
columns = ['a','b','c'])
print(ts)

print(ts.resample('15T').asfreq())
print(ts.resample('15T').ffill())
print(ts.resample('15T').bfill())

输出结果为:(三种方式)

2020-01-01 00:00:00   0   1   2
2020-01-01 01:00:00 3 4 5
2020-01-01 02:00:00 6 7 8
2020-01-01 03:00:00 9 10 11
2020-01-01 04:00:00 12 13 14

a b c
2020-01-01 00:00:00 0.0 1.0 2.0
2020-01-01 00:30:00 NaN NaN NaN
2020-01-01 01:00:00 3.0 4.0 5.0
2020-01-01 01:30:00 NaN NaN NaN
2020-01-01 02:00:00 6.0 7.0 8.0
2020-01-01 02:30:00 NaN NaN NaN
2020-01-01 03:00:00 9.0 10.0 11.0
2020-01-01 03:30:00 NaN NaN NaN
2020-01-01 04:00:00 12.0 13.0 14.0

a b c
2020-01-01 00:00:00 0 1 2
2020-01-01 00:30:00 0 1 2
2020-01-01 01:00:00 3 4 5
2020-01-01 01:30:00 3 4 5
2020-01-01 02:00:00 6 7 8
2020-01-01 02:30:00 6 7 8
2020-01-01 03:00:00 9 10 11
2020-01-01 03:30:00 9 10 11
2020-01-01 04:00:00 12 13 14

a b c
2020-01-01 00:00:00 0 1 2
2020-01-01 00:30:00 3 4 5
2020-01-01 01:00:00 3 4 5
2020-01-01 01:30:00 6 7 8
2020-01-01 02:00:00 6 7 8
2020-01-01 02:30:00 9 10 11
2020-01-01 03:00:00 9 10 11
2020-01-01 03:30:00 12 13 14
2020-01-01 04:00:00 12 13 14

4. 时期重采样 - Period

prng = pd.period_range('2020','2021',freq = 'M')
ts = pd.Series(np.arange(len(prng)), index = prng)
print(ts)

print(ts.resample('Q').sum()) # 降采样
print(ts.resample('15D').ffill()) # 升采样

输出结果为:(第一个是按照季度进行降采样,第二个是按照15D升采样)

2020-01     0
2020-02 1
2020-03 2
2020-04 3
2020-05 4
2020-06 5
2020-07 6
2020-08 7
2020-09 8
2020-10 9
2020-11 10
2020-12 11
2021-01 12
Freq: M, dtype: int32

2020Q1 3
2020Q2 12
2020Q3 21
2020Q4 30
2021Q1 12
Freq: Q-DEC, dtype: int32

2020-01-01 0
2020-01-16 0
2020-01-31 0
2020-02-15 1
2020-03-01 2
2020-03-16 2
2020-03-31 2
2020-04-15 3
2020-04-30 3
2020-05-15 4
2020-05-30 4
2020-06-14 5
2020-06-29 5
2020-07-14 6
2020-07-29 6
2020-08-13 7
2020-08-28 7
2020-09-12 8
2020-09-27 8
2020-10-12 9
2020-10-27 9
2020-11-11 10
2020-11-26 10
2020-12-11 11
2020-12-26 11
2021-01-10 12
2021-01-25 12
Freq: 15D, dtype:


举报

相关推荐

0 条评论