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已经废弃了


下面开始练习

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


转载于:https://www.cnblogs.com/hhh5460/p/5596340.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值