Pandas中的resample,重新采样,是对原样本重新处理的一个方法,是一个对常规时间序列数据重新采样和频率转换的便捷的方法。

降采样:高频数据到低频数据

升采样:低频数据到高频数据

主要函数:resample()(pandas对象都会有这个方法)

resample方法的参数

参数 说明
freq 表示重采样频率,例如‘M’、‘5min’,Second(15)
how=’mean’ 用于产生聚合值的函数名或数组函数,例如‘mean’、‘ohlc’、np.max等,默认是‘mean’,其他常用的值由:‘first’、‘last’、‘median’、‘max’、‘min’
axis=0 默认是纵轴,横轴设置axis=1
fill_method = None 升采样时如何插值,比如‘ffill’、‘bfill’等
closed = ‘right’ 在降采样时,各时间段的哪一段是闭合的,‘right’或‘left’,默认‘right’
label= ‘right’ 在降采样时,如何设置聚合值的标签,例如,9:30-9:35会被标记成9:30还是9:35,默认9:35
loffset = None 面元标签的时间校正值,比如‘-1s’或Second(-1)用于将聚合标签调早1秒
limit=None 在向前或向后填充时,允许填充的最大时期数
kind = None 聚合到时期(‘period’)或时间戳(‘timestamp’),默认聚合到时间序列的索引类型
convention = None 当重采样时期时,将低频率转换到高频率所采用的约定(start或end)。默认‘end’

首先创建一个Series,采样频率为一分钟。

>>> index = pd.date_range('1/1/2000', periods=9, freq='T')
>>> series = pd.Series(range(9), index=index)
>>> series
2000-01-01 00:00:00 0
2000-01-01 00:01:00 1
2000-01-01 00:02:00 2
2000-01-01 00:03:00 3
2000-01-01 00:04:00 4
2000-01-01 00:05:00 5
2000-01-01 00:06:00 6
2000-01-01 00:07:00 7
2000-01-01 00:08:00 8
Freq: T, dtype: int64

降低采样频率为三分钟

>>> series.resample('3T').sum()
2000-01-01 00:00:00 3
2000-01-01 00:03:00 12
2000-01-01 00:06:00 21
Freq: 3T, dtype: int64

降低采样频率为三分钟,但是每个标签使用right来代替left。请注意,bucket中值的用作标签。

>>> series.resample('3T', label='right').sum()
2000-01-01 00:03:00 3
2000-01-01 00:06:00 12
2000-01-01 00:09:00 21
Freq: 3T, dtype: int64

降低采样频率为三分钟,但是关闭right区间。

>>> series.resample('3T', label='right', closed='right').sum()
2000-01-01 00:00:00 0
2000-01-01 00:03:00 6
2000-01-01 00:06:00 15
2000-01-01 00:09:00 15
Freq: 3T, dtype: int64

增加采样频率到30秒

>>> series.resample('30S').asfreq()[0:5] #select first 5 rows
2000-01-01 00:00:00 0
2000-01-01 00:00:30 NaN
2000-01-01 00:01:00 1
2000-01-01 00:01:30 NaN
2000-01-01 00:02:00 2
Freq: 30S, dtype: float64

增加采样频率到30S,使用pad方法填充nan值。

>>> series.resample('30S').pad()[0:5]
2000-01-01 00:00:00 0
2000-01-01 00:00:30 0
2000-01-01 00:01:00 1
2000-01-01 00:01:30 1
2000-01-01 00:02:00 2
Freq: 30S, dtype: int64

增加采样频率到30S,使用bfill方法填充nan值。

>>> series.resample('30S').bfill()[0:5]
2000-01-01 00:00:00 0
2000-01-01 00:00:30 1
2000-01-01 00:01:00 1
2000-01-01 00:01:30 2
2000-01-01 00:02:00 2
Freq: 30S, dtype: int64

通过apply运行一个自定义函数

>>> def custom_resampler(array_like):
... return np.sum(array_like)+5
>>> series.resample('3T').apply(custom_resampler)
2000-01-01 00:00:00 8
2000-01-01 00:03:00 17
2000-01-01 00:06:00 26
Freq: 3T, dtype: int64
出处:https://blog.csdn.net/wangshuang1631/article/details/52314944

pandas的resample重采样的更多相关文章

  1. Python数据分析(三)pandas resample 重采样

    下方是pandas中resample方法的定义,帮助文档http://pandas.pydata.org/pandas-docs/stable/timeseries.html#resampling中有 ...

  2. pandas之时间重采样笔记

    周期由高频率转向低频率称为降采样:例如5分钟股票交易数据转换为日交易数据 相反,周期也可以由低频转向高频称为升采样 其他重采样:例如每周三(W-WED)转换为每周五(W-FRI) import pan ...

  3. pandas 时间序列resample

    resample与groupby的区别:resample:在给定的时间单位内重取样groupby:对给定的数据条目进行统计 函数原型:DataFrame.resample(rule, how=None ...

  4. 重采样Resample 的一些研究记录。

    最近项目有需要重采样算法,先找了一下,主流的就是几个开源算法,Speex / Opus / ffmpeg / sox 1.最早的事Speex,算法源自CCRMA(Center for Computer ...

  5. 03. Pandas 2| 时间序列

    1.时间模块:datetime datetime模块,主要掌握:datetime.date(), datetime.datetime(), datetime.timedelta() 日期解析方法:pa ...

  6. Pandas v0.23.4手册汉化

    Pandas手册汉化 此页面概述了所有公共pandas对象,函数和方法.pandas.*命名空间中公开的所有类和函数都是公共的. 一些子包是公共的,其中包括pandas.errors, pandas. ...

  7. Pandas之DataFrame——Part 2

    ''' [课程2.] 时间模块:datetime datetime模块,主要掌握:datetime.date(), datetime.datetime(), datetime.timedelta() ...

  8. pandas 之 datetime 初识

    import numpy as np import pandas as pd 认识 Time series data is an impotant from of data in many diffe ...

  9. Pandas 时间序列处理

    目录 Pandas 时间序列处理 1 Python 的日期和时间处理 1.1 常用模块 1.2 字符串和 datetime 转换 2 Pandas 的时间处理及操作 2.1 创建与基础操作 2.2 时 ...

随机推荐

  1. salesforce lightning零基础学习(十) Aura Js 浅谈三: $A、Action、Util篇

    前两篇分别介绍了Component类以及Event类,此篇将会说一下 $A , Action以及 Util.  一. Action Action类通常用于和apex后台交互,设置参数,调用后台以及对结 ...

  2. 讲解ontouchstart、ontouchend、onclick区别和坑点

    今天要讲的这个并不复杂,我用一个例子来讲解吧 <div id="box"></div> var box = document.querySelector(& ...

  3. python subprocess模块详解

    一.subprocess标准库 python执行shell脚本,通常会使用so模块中的几个方法,如system.spawn*.popen等.subprocess标准库的引入就是为了取代这些老的模块方法 ...

  4. 看图说话,idea 远程调试 tomcat下项目

    tomcat中部署了项目,准备测试远程调试. 1.配置idea 2. 修改catalina.bat,并启动tomcat catalina.bat: ...rem 下面的参数就是从idea配置中复制过来 ...

  5. maven教程5(聚合工程)

    所谓聚合项目,实际上就是对项目分模块,互联网项目一般来说按照业务分(订单模块.VIP模块.支付模块.CMS模块...),传统的软件项目,大多采用分层的方式(Dao.Serivce.Controller ...

  6. windows下mongodb基础玩法系列二CURD操作(创建、更新、读取和删除)

    windows下mongodb基础玩法系列 windows下mongodb基础玩法系列一介绍与安装 windows下mongodb基础玩法系列二CURD操作(创建.更新.读取和删除) windows下 ...

  7. sass重构响应式unofficial‘s博客轻松适应移动端

    前言: 刚刚玩博客园几天时间,发现挺不错的,对于我这个懒人又是一个爱折腾的人来说挺不错的,对于上班玩电脑,下班玩手机的用户来说,博客园中我的博客有一点给我的感觉不是很友好,电脑端看起来很美观的一个页面 ...

  8. Vue2.5开发去哪儿网App 详情页面开发

    一,banner 图的设计 1. 新建detail的路由 import Detail from '@/pages/detail/Detail' ...... { path: '/detail', na ...

  9. [Laravel] Laravel的基本数据库操作部分

    [laravel] laravel的数据库配置 找到程序目录结构下.env文件 配置基本的数据库连接信息 DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=blog ...

  10. [android] 手机卫士欢迎页检测更新

    客户端:去服务器获取最新的版本信息 服务器端: 版本信息,最新的版本2.0 最新版本的下载地址:http://xxxxxxxx/mobilesafe2.0.apk 版本的描述信息 客户端如果不升级新版 ...