pandas 时间序列resample

简介: resample与groupby的区别:resample:在给定的时间单位内重取样groupby:对给定的数据条目进行统计函数原型:DataFrame.resample(rule, how=None, axis=0, fill_method=None, closed=None, label=None, convention='start', kind=None, loffset=None, limit=None, base=0)其中,参数how已经废弃了。


resample与groupby的区别:
resample:在给定的时间单位内重取样
groupby:对给定的数据条目进行统计

函数原型:
DataFrame.resample(rule, how=None, axis=0, fill_method=None, closed=None, label=None, convention='start', kind=None, loffset=None, limit=None, base=0)
其中,参数how已经废弃了


下面开始练习

import numpy as np
import pandas as pd

 
Start by creating a series with 9 one minute timestamps.

index = pd.date_range('1/1/2000', periods=9, freq='T')
series = pd.Series(range(9), index=index)

 
Downsample the series into 3 minute bins and sum the values of the timestamps falling into a bin.

series.resample('3T').sum()

 
To include this value close the right side of the bin interval as illustrated in the example below this one.

series.resample('3T', label='right').sum()


Downsample the series into 3 minute bins as above, but close the right side of the bin interval.

series.resample('3T', label='right', closed='right').sum()


Upsample the series into 30 second bins.

series.resample('30S').asfreq()

 
Upsample the series into 30 second bins and fill the NaN values using the pad method.

series.resample('30S').pad()

 
Upsample the series into 30 second bins and fill the NaN values using the bfill method.

series.resample('30S').bfill()

 
Pass a custom function via apply

def custom_resampler(array_like):
    return np.sum(array_like)+5

series.resample('3T').apply(custom_resampler)

 
附:常见时间频率
A year
M month
W week
D day
H hour
T minute
S second


目录
相关文章
|
7天前
|
数据处理 索引 Python
Pandas中resample方法:轻松处理时间序列数据
Pandas中resample方法:轻松处理时间序列数据
19 0
|
机器学习/深度学习 算法 物联网
时间序列的重采样和pandas的resample方法介绍
重采样是时间序列分析中处理时序数据的一项基本技术。它是关于将时间序列数据从一个频率转换到另一个频率,它可以更改数据的时间间隔,通过上采样增加粒度,或通过下采样减少粒度。在本文中,我们将深入研究Pandas中重新采样的关键问题。
124 1
|
5月前
|
存储 数据可视化 数据挖掘
NumPy 和 Pandas 数据分析实用指南:1~6 全(下)
NumPy 和 Pandas 数据分析实用指南:1~6 全
131 0
|
数据采集 Python
Pandas数据清洗
Pandas数据清洗
92 0
|
机器学习/深度学习 关系型数据库 数据挖掘
Pandas 2.0 vs Polars:速度的全面对比
前几天的文章,我们已经简单的介绍过Pandas 和Polars的速度对比。刚刚发布的Pandas 2.0速度得到了显著的提升。但是本次测试发现NumPy数组上的一些基本操作仍然更快。并且Polars 0.17.0,也在上周发布,并且也提到了性能的改善,所以我们这里做一个更详细的关于速度方面的评测。
290 0
Pandas 2.0 vs Polars:速度的全面对比
|
数据采集 BI Python
对pandas进行数据预处理的实例讲解
对pandas进行数据预处理的实例讲解
|
存储 算法 数据挖掘
Pandas处理时间序列数据的20个关键知识点
Pandas处理时间序列数据的20个关键知识点
329 0
Pandas处理时间序列数据的20个关键知识点
|
传感器 数据可视化 数据处理
3个用于时间序列数据整理的Pandas函数
本文将演示 3 个处理时间序列数据最常用的 pandas 操作
115 0
3个用于时间序列数据整理的Pandas函数
|
数据采集 开发者 索引
Pandas 数据预处理-中|学习笔记
快速学习 Pandas 数据预处理-中
Pandas 数据预处理-中|学习笔记
|
机器学习/深度学习 API Python
使用Pandas的resample函数处理时间序列数据的技巧
使用Pandas的resample函数处理时间序列数据的技巧
345 0
使用Pandas的resample函数处理时间序列数据的技巧