39. Pandas的时间序列数据-resample重采样

在pandas里对时序的频率的调整称之重新采样,即从一个时频调整为另一个时频的操作,可以借助resample的函数来完成。有upsampling和downsampling(高频变低频)两种。resample后的数据类型有类似'groupby'的接口函数可以调用得到相关数据信息。时序数据经resample后返回Resamper Object,而Resampler 是定义在pandas.core.resample模块里的一个类,可以通过dir查看该类的一些接口函数。

liao@liao:~/md$ python

Python 2.7.12 (default, Nov 12 2018, 14:36:49)

[GCC 5.4.0 20160609] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import pandas.core.resample as pcr

>>> dir(pcr.Resampler)

['__bytes__', ......, '_wrap_result', 'agg', 'aggregate', 'apply', 'asfreq', 'ax', 'backfill', 'bfill', 'count', 'ffill', 'fillna', 'first', 'get_group', 'groups', 'indices', 'interpolate', 'last', 'max', 'mean', 'median', 'min', 'ndim', 'nearest', 'ngroups', 'nunique', 'obj', 'ohlc', 'pad', 'pipe', 'plot', 'prod', 'sem', 'size', 'std', 'sum', 'transform', 'var']

可以看出有mean、pad、ohlc、std、fisrt、fillna等接口函数可以对resample后的数据进行处理

39.1 downsampling 下(降)采用处理

以高频时间序列变低频时间粒度变大数据聚合,原来有100个时间点,假设变为低频的10个点,那么会将原数据每10个数据组成一组(bucket),原来是100个时间点,100个数据,现在是10个时间点,应该有10个数据,那么这10个数据应该是什么呢?可以对每组里的数据的均值mean,或组里的第一个值first、或最后一个last,最为重采样后的数据来进行下一步处理或....。这就是要借助resample后的数据类型调用相应的接口函数来取得。

由于resample函数的参数众多,较为难理解,现在先做一个时序,如下图所示:

import numpy as np

import pandas as pd

c = 21

v = np.arange(1, c)

tx = pd.Series(v)

tx.index = pd.date_range('2018-12-01', periods = 20, freq = "d")

print "tx", "-" * 20, "\n", tx

程序执行结果:

tx --------------------

2018-12-01 1

2018-12-02 2

2018-12-03 3

2018-12-04 4

2018-12-05 5

2018-12-06 6

2018-12-07 7

2018-12-08 8

2018-12-09 9

2018-12-10 10

2018-12-11 11

2018-12-12 12

2018-12-13 13

2018-12-14 14

2018-12-15 15

2018-12-16 16

2018-12-17 17

2018-12-18 18

2018-12-19 19

2018-12-20 20

Freq: D, dtype: int64

程序的执行结果和图是一一对应的,即2018-12-01的数据为1。

好,现在对tx这个时序进行降采样,每4天为一个组进行分段segment,那么可以这样去分组(用数学的区域概念来描述)

[2018-12-01,2018-12-05)为第一组,这样2018-12-01可以落在这个区间里,

[2018-12-05, 2018-12-09)为第二组,

[2018-12-09,2018-12-13)为第三组,

[2018-12-13,2018-12-17)为第四组,

[2018-12-17,2018-12-21)为第五组,第五组的日期2018-12-21尽管不在数据里,可以补齐。这样分组的特点的是左闭右开。

当然,也可采用左开右闭的区间描述这几个分组:

(2018-11-27,2018-12-01]是第一分组,是为了让第一个时间2018-12-01能落在第一个左开右闭的分组,

(2018-12-01, 2010-12-05]为第二组,

(2018-12-05, 2010-12-09]为第三组,

(2018-12-09, 2010-12-013]为第四组,

(2018-12-13, 2010-12-17]为第五组,

(2018-12-17, 2010-12-21]为第六组。

这里,多出来的一组是因为第一时间点要落在第一分组里的要求。

import numpy as np

import pandas as pd

v = np.arange(1, 21)

tx = pd.Series(v)

tx.index = pd.date_range('2018-12-01', periods = 20, freq = "d")

print "tx", "-" * 20, "\n", tx

tf = tx.resample("4d").sum()

print "tf closed using default", "-" * 5, "\n",tf

tf = tx.resample("4d", closed = "left").sum()

print "tf closed = 'left' ", "-" * 5, "\n",tf

tf = tx.resample("4d", closed = "right").sum()

print "tf closed = 'right' ", "-" * 5, "\n",tf

程序结果:

tx --------------------

2018-12-01 1

2018-12-02 2

........

2018-12-19 19

2018-12-20 20

Freq: D, dtype: int64

tf closed using default -----

2018-12-01 10

2018-12-05 26

2018-12-09 42

2018-12-13 58

2018-12-17 74

dtype: int64

tf closed = 'left' -----

2018-12-01 10

2018-12-05 26

2018-12-09 42

2018-12-13 58

2018-12-17 74

dtype: int64

tf closed = 'right' -----

2018-11-27 1

2018-12-01 14

2018-12-05 30

2018-12-09 46

2018-12-13 62

2018-12-17 57

dtype: int64

从语句

tf = tx.resample("4d").sum()

print "tf closed using default", "-" * 5, "\n",tf

tf = tx.resample("4d", closed = "left").sum()

print "tf closed = 'left' ", "-" * 5, "\n",tf

的输出结果可以看出,resample函数默认closed参数值为left,即左闭右开。所以2018-12-01的输出值10 = 1 + 2 + 3 + 4。2018-12-02的输出值26 = 5 + 6 + 7 + 8。而当resample采用左开右闭时,第一区间里就只有2018-12-01这一天的数据据,所以和为1,奇怪的是第一项数据输出的index不是2018-12-01而是2018-11-27,而第二项输出的index却是2018-12-01,这是为什么?这里得看resample的第二个令人费解的参数label了,label参数是指输出时使用index是用区间的左界值还是右界值呢?例如(a, b]或[a, b)是用左界值a还右边界值b?

import numpy as np

import pandas as pd

v = np.arange(1, 21)

tx = pd.Series(v)

tx.index = pd.date_range('2018-12-01', periods = 20, freq = "d")

print "tx", "-" * 20, "\n", tx

tf = tx.resample("4d").sum()

print "tf closed using default", "-" * 5, "\n",tf

tf = tx.resample("4d", closed = "left").sum()

print "tf closed = 'left' ", "-" * 5, "\n",tf

tf = tx.resample("4d", closed = "right").sum()

print "tf closed = 'right' ", "-" * 5, "\n",tf

tf = tx.resample("4d", closed = "right", label = "right").sum()

print "tf closed = 'right' label = 'right'", "-" * 0, "\n",tf

程序执行结果:

tx --------------------

2018-12-01 1

........

2018-12-20 20

Freq: D, dtype: int64

tf closed using default -----

2018-12-01 10

2018-12-05 26

2018-12-09 42

2018-12-13 58

2018-12-17 74

dtype: int64

tf closed = 'left' -----

2018-12-01 10

2018-12-05 26

2018-12-09 42

2018-12-13 58

2018-12-17 74

dtype: int64

tf closed = 'right' -----

2018-11-27 1

2018-12-01 14

2018-12-05 30

2018-12-09 46

2018-12-13 62

2018-12-17 57

dtype: int64

tf closed = 'right' label = 'right'

2018-12-01 1

2018-12-05 14

2018-12-09 30

2018-12-13 46

2018-12-17 62

2018-12-21 57

dtype: int64

从语句

tf = tx.resample("4d", closed = "right", label = "right").sum()

print "tf closed = 'right' label = 'right'", "-" * 0, "\n",tf

的输出结果可以看到,第一项输出的index已经变成了2018-12-01了,求和为1,也是正确的,第二项2018-12-05的value为14即14 = 2 + 3 + 4 + 5也是对的,且有6组数据也是和之前分析是对的。

ohlc函数

在金融领域经常关系开盘、收盘和最高最低价,resample数据后可以进行这样的操作,pandas数据经resample后可以调用嗯ohlc函数得到汇总数据。

import numpy as np

import pandas as pd

v = np.arange(1, 21)

tx = pd.Series(v)

tx.index = pd.date_range('2018-12-01', periods = 20, freq = "d")

print "tx", "-" * 20, "\n", tx

tf = tx.resample("4d", closed = "right", label = "right").ohlc()

print "tf closed = 'right' label = 'right'", "-" * 0, "\n",tf

程序执行结果:

tx --------------------

2018-12-01 1

2018-12-02 2

........

2018-12-19 19

2018-12-20 20

Freq: D, dtype: int64

tf closed = 'right' label = 'right'

open high low close

2018-12-01 1 1 1 1

2018-12-05 2 5 2 5

2018-12-09 6 9 6 9

2018-12-13 10 13 10 13

2018-12-17 14 17 14 17

2018-12-21 18 20 18 20

39.2 upsampling上(升)采样处理

低频变高频会出现大量的NaN数据,可以用method指定填充数据的方式。

import numpy as np

import pandas as pd

v = np.arange(1, 21)

#print v

t0 = pd.Series(v, index = pd.date_range('2018-12-01', periods = 20))

#print t0

print "first", "*" * 22

print t0.resample("6H").first()[:10]

print "bfill", "*" * 22

print t0.resample("6H").bfill()[:10]

print "ffill", "*" * 22

print t0.resample("6H").ffill()[:10]

print "interpolate", "*" * 16

print t0.resample("6H").interpolate()[:10]

程序执行结果如下:

first **********************

2018-12-01 00:00:00 1.0

2018-12-01 06:00:00 NaN

2018-12-01 12:00:00 NaN

2018-12-01 18:00:00 NaN

2018-12-02 00:00:00 2.0

2018-12-02 06:00:00 NaN

2018-12-02 12:00:00 NaN

2018-12-02 18:00:00 NaN

2018-12-03 00:00:00 3.0

2018-12-03 06:00:00 NaN

Freq: 6H, dtype: float64

bfill **********************

2018-12-01 00:00:00 1

2018-12-01 06:00:00 2

2018-12-01 12:00:00 2

2018-12-01 18:00:00 2

2018-12-02 00:00:00 2

2018-12-02 06:00:00 3

2018-12-02 12:00:00 3

2018-12-02 18:00:00 3

2018-12-03 00:00:00 3

2018-12-03 06:00:00 4

Freq: 6H, dtype: int32

ffill **********************

2018-12-01 00:00:00 1

2018-12-01 06:00:00 1

2018-12-01 12:00:00 1

2018-12-01 18:00:00 1

2018-12-02 00:00:00 2

2018-12-02 06:00:00 2

2018-12-02 12:00:00 2

2018-12-02 18:00:00 2

2018-12-03 00:00:00 3

2018-12-03 06:00:00 3

Freq: 6H, dtype: int32

interpolate ****************

2018-12-01 00:00:00 1.00

2018-12-01 06:00:00 1.25

2018-12-01 12:00:00 1.50

2018-12-01 18:00:00 1.75

2018-12-02 00:00:00 2.00

2018-12-02 06:00:00 2.25

2018-12-02 12:00:00 2.50

2018-12-02 18:00:00 2.75

2018-12-03 00:00:00 3.00

2018-12-03 06:00:00 3.25

Freq: 6H, dtype: float64

python resample函数_Pandas的时间序列-resample重采样相关推荐

  1. python resample函数_Pandas中resample方法详解

    Pandas中的resample,重新采样,是对原样本重新处理的一个方法,是一个对常规时间序列数据重新采样和频率转换的便捷的方法. 方法的格式是: DataFrame.resample(rule, h ...

  2. python resample函数_Python pandas.DataFrame.resample函数方法的使用

    DataFrame.resample(rule, axis=0, closed=None, label=None, convention='start', kind=None, loffset=Non ...

  3. python replace函数_Pandas DataFrame.replace()用法

    本文概述 Pandas replace()是一个非常丰富的函数, 用于从DataFrame替换字符串, 正则表达式, 字典, 列表和序列. DataFrame的值可以动态替换为其他值.它能够使用Pyt ...

  4. python cut函数_Pandas DataFrame.cut()用法例子

    本文概述 当需要将数据值分段并将其分类到bin中时, 将调用cut()方法.它用于将连续变量转换为分类变量.它还可以将元素数组分离到单独的容器中.该方法仅适用于一维数组状对象. 如果我们有大量标量数据 ...

  5. python agg函数_pandas agg函数使用方法

    DataFrame.agg(func,axis = 0,* args,** kwargs) func : 函数,函数名称,函数列表,字典{'行名/列名','函数名'} 使用指定轴上的一个或多个操作进行 ...

  6. python iloc函数_pandas入门——loc与iloc函数

    oc与iloc函数 loc函数 import pandas as pd import numpy # 导入数据 df = pd.read_csv(filepath_or_buffer="D: ...

  7. python sort_values函数_Pandas之排序函数sort_values()的实现

    一.sort_values()函数用途 pandas中的sort_values()函数原理类似于SQL中的order by,可以将数据集依照某个字段中的数据进行排序,该函数即可根据指定列数据也可根据指 ...

  8. python isin函数_pandas中isin()函数及其逆函数使用

    pandas中isin()函数及其逆函数使用 发布时间:2018-05-27 21:11, 浏览次数:2021 , 标签: pandas isin 我使用这个函数就是用来清洗数据,删选过滤掉DataF ...

  9. python extractall函数_Pandas从str.extractall('#')中给出错误

    我正试图过滤tweet文本中的所有#关键字.我使用str.extractall()来提取所有带有#关键字的关键字. 这是我第一次使用pandas从tweetText中过滤关键字.输入.代码.预期输出和 ...

最新文章

  1. [ZZ]知名互联网公司Python的16道经典面试题及答案
  2. C# 操作Excel之旁门左道 [ C# | Excel ]
  3. android 小知识点
  4. python怎么学最快-浅谈:从为什么学习python到如何学好python
  5. 让你的Android程序更省电
  6. SSIS:错误信息总结
  7. android系统关闭wifi,Android以编程方式打开/关闭WiFi HotSpot
  8. HashMap工作原理和扩容机制
  9. 猜数字小游戏(加强版)它来了
  10. L13 ansible 基础应用与常见模块
  11. Ubuntu实用安装
  12. dn什么意思_钢管中的DN表示什么意思?
  13. PhotoSweeper X for Mac(重复照片快速清理软件)
  14. rk3288 调试dvp摄像头_RK3288 uvc摄像头调试
  15. 联想笔记本打字不显示选字框
  16. Docker/Podman使用入门---从容器构建镜像 提交镜像到服务器UCloud dockerhub
  17. Software-Defined Networking:A comprehensive Survey
  18. 弹性布局flex(兼容不同浏览器)
  19. Unity3d NGUI控件知识
  20. RPA不是“万灵丹”,需人机分工内控风险

热门文章

  1. 快速搭建samba 简单samba服务
  2. OPPO号召力如此巨大,R11发布会邀来半个娱乐圈
  3. Mac硬盘管理器:Paragon Hard Disk Manager for Mac
  4. Moon River
  5. 数据仓库——维度数据建模实例
  6. 基于Selenium实现的web自动化测试框架
  7. Geogebra里给带有曲线和直线混合边界的封闭区域填充颜色
  8. rust使用睡袋_rust怎么弄睡袋 | 手游网游页游攻略大全
  9. java自学基础知识重点2
  10. 33 ArcToolBox学习系列之数据管理工具箱——投影与变换(Projections and Transformations)未完待续……...