大熊猫滚动标准如何计算?

时间:2021-01-14 04:03:37

标签: python pandas

我正在使用 a.rolling(5).std() 在窗口中获取 std 系列(大小=5,a 是 pd.Series)

但我发现结果不是我想要的。

示例如下:

In [15]: a = [-49, -50, -50, -51, -48]

In [16]: pd.Series(a).rolling(5).std()
Out[16]: 
0         NaN
1         NaN
2         NaN
3         NaN
4    1.140175
dtype: float64

In [17]: np.std(a)
Out[17]: 1.0198039027185568

我认为 pd.Series(a).rolling(5).std() 的最后一个元素应该与 np.std(a) 相等,

但为什么不是?

1 个答案:

答案 0 :(得分:2)

这可能是由于 Pandas 使用 N - 1 而不是 N 进行规范化。请参阅 https://pandas.pydata.org/docs/reference/api/pandas.core.window.rolling.Rolling.std.html

的第一个注释

您可以使用自由度参数 ddof 来更改此行为,例如pd.Series(a).rolling(5).std(ddof=0)