欢迎关注微信公众号:excelwork
函数语法:
df.resample(rule, how=None, axis=0, fill_method=None, closed=None, label=None, convention='start', kind=None, loffset=None, limit=None, base=0, on=None, level=None)
rule:为用来表示频率偏移量的字符串
closed : {‘right’, ‘left’},表示区间关闭的那一边,默认left,可应用所有的频率除了 ‘M’, ‘A’, ‘Q’, ‘BM’, ‘BA’, ‘BQ’, and ‘W’
right则全部频率可用
label:{right','left'},表示区间关闭的那一边
函数介绍:
时间序列转换和重新采样的简便方法。对象必须有类似时间的索引,或者将类似时间的数值传递给on或level关键字
函数使用:
首先构造序列
i=pd.date_range('2017-01-01',periods=10,freq='S')
s=pd.Series(range(10),index=i)
构造的时间序列如下:
s
2017-01-01 00:00:00 0
2017-01-01 00:00:01 1
2017-01-01 00:00:02 2
2017-01-01 00:00:03 3
2017-01-01 00:00:04 4
2017-01-01 00:00:05 5
2017-01-01 00:00:06 6
2017-01-01 00:00:07 7
2017-01-01 00:00:08 8
2017-01-01 00:00:09 9
2017-01-01 00:00:10 10
2017-01-01 00:00:11 11
2017-01-01 00:00:12 12
2017-01-01 00:00:13 13
2017-01-01 00:00:14 14
2017-01-01 00:00:15 15
2017-01-01 00:00:16 16
2017-01-01 00:00:17 17
2017-01-01 00:00:18 18
2017-01-01 00:00:19 19
使用label和closed参数,等于right时,包含5s对应的值(2017-01-01 00:00:05 15),按5秒(00,01~05,06~10…)分组,
s.resample('5S',label='right',closed='right').sum()
2017-01-01 00:00:00 0
2017-01-01 00:00:05 15
2017-01-01 00:00:10 40
2017-01-01 00:00:15 65
2017-01-01 00:00:20 70
参数等于left时,不包含5秒对应的值,按5秒(00~04,05~09,10~14…)分组求和:
s.resample('5S',label='left',closed='left').sum()
2017-01-01 00:00:00 10
2017-01-01 00:00:05 35
2017-01-01 00:00:10 60
2017-01-01 00:00:15 85
Freq: 5S, dtype: int64
不使用label和closed参数,从第一个时间点开始,每5秒求和一次:
s.resample('5S').sum()
2017-01-01 00:00:00 10
2017-01-01 00:00:05 35
2017-01-01 00:00:10 60
2017-01-01 00:00:15 85
参考链接:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html