[Python] Why does Pandas rolling std output different results for different tail sizes?

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    Output of pandas rolling std is not consistent when data size changes. I tried to apply rolling std for different slices of the data, while all of them are at the tail of the series. I observed that the result for s.tail(3) is different from that of s.tail(4) and s.tail(5) -

    >>> s = pd.Series(np.random.default_rng(seed=123).random(size=5))
    >>> s[1] = 10000000 # just a large number
    >>> s
    0 6.823519e-01
    1 1.000000e+07
    2 2.203599e-01
    3 1.843718e-01
    4 1.759059e-01
    dtype: float64
    >>> s.tail(3).rolling(window=3, min_periods=1).std()
    2 NaN
    3 0.025447
    4 0.023604
    dtype: float64
    >>> s.tail(4).rolling(window=3, min_periods=1).std()
    1 NaN
    2 7.071068e+06
    3 5.773503e+06
    4 0.000000e+00
    dtype: float64
    >>> s.tail(5).rolling(window=3, min_periods=1).std()
    0 NaN
    1 7.071067e+06
    2 5.773502e+06
    3 5.773503e+06
    4 0.000000e+00
    dtype: float64


    In comparison, if I apply np.std to the rolling windows, the last result would be consistent:

    >>> s.tail(3).rolling(window=3, min_periods=1).apply(np.std)
    2 0.000000
    3 0.017994
    4 0.019273
    dtype: float64
    >>> s.tail(4).rolling(window=3, min_periods=1).apply(np.std)
    1 0.000000e+00
    2 5.000000e+06
    3 4.714045e+06
    4 1.927280e-02
    dtype: float64
    >>> s.tail(5).rolling(window=3, min_periods=1).apply(np.std)
    0 0.000000e+00
    1 5.000000e+06
    2 4.714045e+06
    3 4.714045e+06
    4 1.927280e-02
    dtype: float64


    What is causing pandas' rolling standard deviation calculation on the same windows size 3 results in different outcomes?

    Pandas version: 2.2.2 (also tried 2.3.0) on Windows

    Continue reading...

Compartilhe esta Página