熊猫:您可以访问滚动窗口项吗

梅林

您可以访问熊猫滚动窗口对象吗?

rs = pd.Series(range(10))
rs.rolling(window = 3)

#print's 
Rolling [window=3,center=False,axis=0]

我可以分组吗?

[0,1,2]
[1,2,3]
[2,3,4]
梅林

这是一种解决方法,但请等待是否有人有熊猫解决方案:

def rolling_window(a, step):
    shape   = a.shape[:-1] + (a.shape[-1] - step + 1, step)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

rolling_window(rs, 3)

array([[ 0,  1,  2],
       [ 1,  2,  3],
       [ 2,  3,  4],
       [ 3,  4,  5],
       [ 4,  5,  6],
       [ 5,  6,  7],
       [ 6,  7,  8],
       [ 7,  8,  9],
       [ 8,  9, 10]])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章