pandas的resample重采样的使用
作者:jingsupo 时间:2023-04-07 10:33:29
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://www.cnblogs.com/jingsupo/p/pandas-resample.html
标签:pandas,resample,重采样
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaScript详解使用Promise处理回调地狱与async await修饰符
2024-04-22 22:43:58
JavaScript简单计算人的年龄示例
2024-05-03 15:04:39
Flask路由尾部有没有斜杠有什么区别
2021-03-17 12:00:58
![](https://img.aspxhome.com/file/2023/9/118349_0s.png)
Python中顺序表的实现简单代码分享
2022-08-31 22:26:36
![](https://img.aspxhome.com/file/2023/1/115141_0s.png)
Python转换字典成为对象,可以用"."方式访问对象属性实例
2021-06-01 09:01:50
如何避免mysql启动时错误及sock文件作用分析
2024-01-16 18:39:01
JS实现密码框根据焦点的获取与失去控制文字的消失与显示效果
2024-04-10 10:50:20
DHTML 打造 Picture Spelling
2013-08-22 17:01:53
Golang 语言控制并发 Goroutine的方法
2024-02-14 21:18:19
antd table按表格里的日期去排序操作
2024-04-28 10:55:56
![](https://img.aspxhome.com/file/2023/3/133063_0s.jpg)
一篇文章带你了解Python之Selenium自动化爬虫
2023-12-15 14:30:53
![](https://img.aspxhome.com/file/2023/2/98802_0s.png)
基于Python OpenCV实现图像的覆盖
2022-09-12 22:05:02
![](https://img.aspxhome.com/file/2023/2/99132_0s.jpg)
详解Oracle在out参数中访问光标
2024-01-12 21:17:50
Golang交叉编译(跨平台编译)的使用
2024-05-02 16:26:40
![](https://img.aspxhome.com/file/2023/6/130706_0s.png)
浅谈用VSCode写python的正确姿势
2022-11-20 20:52:06
![](https://img.aspxhome.com/file/2023/0/126930_0s.gif)
Vue Render函数原理及代码实例解析
2023-07-02 16:34:38
python通过exifread模块获得图片exif信息的方法
2023-08-18 05:00:15
服务器端控件是如何操作的?
2009-11-01 15:22:00
python time()的实例用法
2022-01-09 02:33:02
微软补丁KB967723造成MySQL频繁无法连接
2009-11-03 14:28:00