Pandas中resample方法详解

本文介绍了Pandas库中resample方法的使用方法及其参数详解。该方法用于处理时间序列数据,实现数据的重新采样及频率转换。文章通过实例展示了如何调整采样频率、选择时间标签方式以及填充缺失值等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

方法的格式是:

DataFrame.resample(rulehow=Noneaxis=0fill_method=Noneclosed=Nonelabel=Noneconvention='start',kind

=Noneloffset=Nonelimit=Nonebase=0)

参数详解是:

Parameters:

rule : string

偏移量表示目标字符串或对象转换

axis : int, optional, default 0

closed : {‘right’, ‘left’}

哪一个方向的间隔是关闭的

label : {‘right’, ‘left’}

Which bin edge label to label bucket with

convention : {‘start’, ‘end’, ‘s’, ‘e’}

loffset : timedelta

调整重新取样时间标签

base : int, default 0

频率均匀细分1天,“起源”的聚合的间隔。例如,对于“5分钟”频率,基地可能范围从0到4。默认值为0

首先创建一个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

Pandas中的resample是对原样本重新处理的一个方法,用于重新采样时间序列数据。通过resample方法,可以方便地对常规时间序列数据进行重新采样和频率转换。 在使用resample方法时,可以使用不同的参数来指定重新采样的频率,例如使用'sum'方法来对时间序列数据进行求和操作。此外,也可以使用apply方法传递一个自定义函数来对时间序列数据进行处理。 对于已弃用的loffset参数,可以使用from pandas.tseries.frequencies import to_offset来替换。这个参数可以用于对重新采样后的时间索引进行偏移调整。例如,可以使用to_offset(loffset)来调整重新采样后的时间索引的偏移量。 综上所述,pandasresample方法提供了一种便捷的方式来对时间序列数据进行重新采样和频率转换,可以通过使用不同的参数和自定义函数来实现不同的处理需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Pandasresample方法详解](https://blog.csdn.net/weixin_40426830/article/details/111512471)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值