DataFrame加权滑窗rolling使用,win_type=‘triang‘详解

@创建于:20210312
@修改于:20210312

1、背景
2、rolling介绍
2.1 DataFrame rolling API介绍

此处对应的pandas 版本号是 1.2.2,pandas.DataFrame.rolling

DataFrame.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None)

2.2 参数介绍
序号参数英文中文
1window: intSize of the moving window. This is the number of observations used for calculating the statistic. Each window will be a fixed size.移动窗口的大小。这是用于计算统计数据的观测数。每个窗口的大小都是固定的。
2window: offsetIf its an offset then this will be the time period of each window. Each window will be a variable sized based on the observations included in the time-period. This is only valid for datetimelike indexes.如果是偏移量,则这将是每个窗口的时间段。每个窗口的大小将根据时间段中的观察结果而变化。这仅对datetimelike索引有效。
3window: BaseIndexer subclassIf a BaseIndexer subclass is passed, calculates the window boundaries based on the defined get_window_bounds method. Additional rolling keyword arguments, namely min_periods, center, and closed will be passed to get_window_bounds.如果传递了BaseIndexer子类,则根据定义的get_window_bounds方法计算窗口边界。还可以传递其他关键字参数给get_window_bounds,即min_periods、center和closed。
4min_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, min_periods will default to 1. Otherwise, min_periods will default to the size of the window.窗口中需要有值的最小观察数(否则结果为NA)。对于由偏移量指定的窗口,minu periods将默认为1。否则,min_periods将默认为窗口的大小。
5center bool, default FalseSet the labels at the center of the window.将标签设置在窗口的中心。
6win_type str, default NoneProvide a window type. If win_type=None, all points are evenly weighted; otherwise, win_type can accept a string of any scipy.signal window function…提供窗口类型。如果win_type=None,则所有点的权重相等;否则,win_type可以接受任意值的字符串scipy.signal window function
7on str, optionalFor a DataFrame, a datetime-like column or MultiIndex level on which to calculate the rolling window, rather than the DataFrame’s index. Provided integer column is ignored and excluded from result since an integer index is not used to calculate the rolling window.对于DataFrame类型,datatime类型的列或者MultiIndex类型的用于计算rolling窗口,而不是DataFrame的索引。对于dataframe而言,指定要计算滚动窗口的列,值为列名。
8axis int or str, default 0方向(轴),一般都是0
9closed str, default NoneMake the interval closed on the ‘right’, ‘left’, ‘both’ or ‘neither’ endpoints. Defaults to ‘right’. Changed in version 1.2.0: The closed parameter with fixed windows is now supported.使“right”、“left”、“both”或“neither”端点上的间隔闭合。默认为“right”。 在版本1.2.0中更改:现在支持带有固定窗口的closed参数。(仅支持datetimelike 和 offset 设置的滑窗)
3、例子
3.1 window例子
import pandas as pd
import numpy as np

df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]})
df.rolling(window=2).mean()

	B
0 	NaN
1 	0.5
2 	1.5
3 	NaN
4 	NaN

df = pd.DataFrame({'B': [0, 1, 2, 3, 4, 5]})
df.rolling(window=3, center=True).sum()

 	B
0 	NaN
1 	3.0
2 	6.0
3 	9.0
4 	12.0
5 	NaN
3.2 win_type=‘triang’例子

(1)支持的函数类型
win_type支持的函数名称详见:Window functions (scipy.signal.windows)

在这里插入图片描述
(2)triang数学模型
scipy.signal.windows.triang

在这里插入图片描述
winow = n,且n为奇数
权重分配:
1 ( n + 1 ) / 2 , 2 ( n + 1 ) / 2 , . . . , 1 , . . . , 2 ( n + 1 ) / 2 , 1 ( n + 1 ) / 2 \frac{1}{(n+1)/2},\frac{2}{(n+1)/2}, ...,1,...,\frac{2}{(n+1)/2},\frac{1}{(n+1)/2} (n+1)/21(n+1)/22...1...(n+1)/22(n+1)/21
权重和:
n + 1 2 \frac{n+1}{2} 2n+1

winow = n,且n为偶数
权重分配:
2 n + 2 , 4 n + 2 , . . . , n n + 2 , . . . , 4 n + 2 , 2 n + 2 \frac{2}{n+2},\frac{4}{n+2}, ...,\frac{n}{n+2},...,\frac{4}{n+2},\frac{2}{n+2} n+22n+24...n+2n...n+24n+22
权重和:
n 2 \frac{n}{2} 2n

(3)triang演示

df = pd.DataFrame({'B': [0, 1, 2, 3, 4, 5, 6]})
df.rolling(window=5, win_type='triang').sum()

 	B
0 	NaN
1 	NaN
2 	NaN
3 	NaN
4 	6.0
5 	9.0
6 	12.0

df.rolling(window=5, win_type='triang').mean()

 	B
0 	NaN
1 	NaN
2 	NaN
3 	NaN
4 	2.0
5 	3.0
6 	4.0

df.rolling(window=4, win_type='triang').sum()

	B
0 	NaN
1 	NaN
2 	NaN
3 	3.0
4 	5.0
5 	7.0
6 	9.0

df.rolling(window=4, win_type='triang').mean()

 	B
0 	NaN
1 	NaN
2 	NaN
3 	1.5
4 	2.5
5 	3.5
6 	4.5

(4)其他函数数学模型

scipy.signal.windows.hann
在这里插入图片描述
在这里插入图片描述scipy.signal.windows.cosine

在这里插入图片描述

scipy.signal.windows.gaussian

在这里插入图片描述
在这里插入图片描述scipy.signal.windows.boxcar
在这里插入图片描述

4、参考链接

pandas.DataFrame.rolling

scipy.signal.windows.triang

时间序列分析 - 移动平均SMA, EMA(EWMA) 之python

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值