pandas窗口函数--rolling
在HQL中我经常使用开窗函数,后来做mysql(5.7)的数据处理,只能使用order等分组方式替代开窗函数。
而pandas中带有各种移动窗口,它都是以rolling打头的函数,后接具体的函数,来显示该移动窗口函数的功能。
总共有3+1类。
主要有如下类:
还有pandas.rolling_xx方法
第一类 DataFrame的rolling
df.rolling(window, min_periods=None, freq=None, center=False, win_type=None, on=None, axis=0, closed=None)
参数详解
window : int, or offsetSize of the moving window. This is the number of observations used forcalculating the statistic. Each window will be a fixed size.If its an offset then this will be the time period of each window. Eachwindow will be a variable sized based on the observations included inthe time-period. This is only valid for datetimelike indexes. This isnew in 0.19.0 表示时间窗的大小,注意有两种形式(int or offset)。如果使用int,则数值表示计算统计量的观测值的数量即向前几个数据。如果是offset类型,表示时间窗的大小。pandas offset相关可以参考这里。min_periods : int, default NoneMinimum number of observations in window required to have a value(otherwise result is NA). For a window that is specified by an offset,this will default to 1. 最少需要有值的观测点的数量,对于int类型,默认与window相等。对于offset类型,默认为1。
也开始这样理解:取窗口内非空的。超过这个限制就是带上空值计算freq : string or DateOffset object, optional (default None).. deprecated:: 0.18.0Frequency to conform the data to before computing the statistic.Specified as a frequency string or DateOffset object.center : boolean, default FalseSet the labels at the center of the window. 是否使用window的中间值作为label,默认为false。只能在window是int时使用win_type : string, default NoneProvide a window type. See the notes below. 窗口类型,默认为None一般不特殊指定,了解支持的其他窗口类型,参考这里。on : string, optionalFor a DataFrame, column on which to calculatethe rolling window, rather than the index 对于DataFrame如果不使用index(索引)作为rolling的列,那么用on来指定使用哪列。closed : string, default NoneMake the interval closed on the 'right', 'left', 'both' or'neither' endpoints.For offset-based windows, it defaults to 'right'.For fixed windows, defaults to 'both'. Remaining cases not implementedfor fixed windows. 定义区间的开闭,曾经支持int类型的window,新版本已经不支持了。对于offset类型默认是左开右闭的即默认为right。可以根据情况指定为left both等。axis : int or string, default 0 方向(轴),一般都是0。
案例
案例一
min_periods参数
df = pd.DataFrame({'A': [0, 1, 2, np.nan, 3]})df
df.rolling(3).sum()df.rolling(3, min_periods=1).sum()df.rolling(3, min_periods=2).sum()df.rolling(3, min_periods=3).sum()
df.rolling(3, min_periods=1).sum() #m默认center=Truedf.rolling(3, min_periods=1,center=True).sum()
center参数
df.rolling(3, min_periods=1).sum() #m默认center=Truedf.rolling(3, min_periods=1,center=True).sum()
第二类pandas.core.groupby.rolling
案例
import pandas as pd# A地有两个仓库,都运往B。df = pd.DataFrame({'1': ['A1', 'A2', 'A1', 'A2', 'A2', 'A1', 'A2'], '2': ['B1', 'B1', 'B1', 'B1', 'B1', 'B1', 'B1'], 'num': [1,2,1,3,4,2,1]}, index = [pd.Timestamp('20200101 09:00:00'), pd.Timestamp('20200101 09:00:01'), pd.Timestamp('20200101 09:00:02'), pd.Timestamp('20200101 09:00:03'), pd.Timestamp('20200101 09:00:04'), pd.Timestamp('20200101 09:00:05'), pd.Timestamp('20200101 09:00:06')])df
# 以9:00:04秒为例,由于时间窗是3s,默认的closed是right,所以我们相加04,03,02秒的num,共有4+3+0=7df.groupby(['1', '2'])['num'].rolling('3s').sum()
第三类略
不常用
第四类pandas.rolling_xx方法
大多数人都以为是才智成就了科学家,他们错了,是品格。---爱因斯坦
作者:wqbin
来源链接:https://www.cnblogs.com/wqbin/p/12090466.html
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。