@创建于: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 参数介绍
序号 | 参数 | 英文 | 中文 |
---|---|---|---|
1 | window: int | Size of the moving window. This is the number of observations used for calculating the statistic. Each window will be a fixed size. | 移动窗口的大小。这是用于计算统计数据的观测数。每个窗口的大小都是固定的。 |
2 | window: offset | If 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索引有效。 |
3 | window: BaseIndexer subclass | If 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。 |
4 | min_periods int, default None | Minimum 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将默认为窗口的大小。 |
5 | center bool, default False | Set the labels at the center of the window. | 将标签设置在窗口的中心。 |
6 | win_type str, default None | Provide 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。 |
7 | on str, optional | For 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而言,指定要计算滚动窗口的列,值为列名。 |
8 | axis int or str, default 0 | 方向(轴),一般都是0 | |
9 | closed str, default None | Make 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+22,n+24,...,n+2n,...,n+24,n+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