Skip to content

BUG: Rolling.skew and Rolling.kurt return NaN for low-variance windows #62946

@Alvaro-Kothe

Description

@Alvaro-Kothe

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np
import scipy.stats as st

sigma = 1e-8
mu = 0e0

window_size = 5
n = 10

rng = np.random.default_rng(505)
arr = rng.normal(loc=mu, scale=sigma, size=n)
ser = pd.Series(arr)

rolling_fns = ["skew", "kurt"]
scipy_fns = ["skew", "kurtosis"]

window = ser.rolling(window=window_size)

for method, st_method in zip(rolling_fns, scipy_fns, strict=True):
    print(method)
    pd_result = getattr(window, method)()
    scipy_fn = lambda x: getattr(st, st_method)(x, bias=False)
    scipy_result = window.apply(scipy_fn)
    df = pd.DataFrame({"Pandas": pd_result, "Scipy": scipy_result})
    print(df)
    print("=" * 80)

Issue Description

This issue is similar to #57972, where an absolute threshold in the kurtosis and skewness calculation leads to incorrect results for low-variance data, forcing the results to NaN.

SciPy's equivalent functions handle these cases correctly by computing the values without an arbitrary absolute cutoff.

The output of the example above is

skew
   Pandas     Scipy
0     NaN       NaN
1     NaN       NaN
2     NaN       NaN
3     NaN       NaN
4     NaN  0.197621
5     NaN -0.254492
6     NaN -1.230099
7     NaN -0.425309
8     NaN  0.177343
9     NaN  0.081693
================================================================================
kurt
   Pandas     Scipy
0     NaN       NaN
1     NaN       NaN
2     NaN       NaN
3     NaN       NaN
4     NaN -1.610228
5     NaN -1.808220
6     NaN  1.749034
7     NaN  1.221267
8     NaN -2.435851
9     NaN -2.686508

The first 4 rows are NaN in both due to insufficient window size, which is expected. But the remaining values in pandas should be similar to Scipy.

Expected Behavior

Pandas results should match SciPy's for indices 4-9, computing the skew and kurtosis without forcing NaN due to low variance.

Installed Versions

INSTALLED VERSIONS

commit : ea75dd7
python : 3.13.9
python-bits : 64
OS : Linux
OS-release : 6.17.5-200.fc42.x86_64
Version : #1 SMP PREEMPT_DYNAMIC Fri Oct 24 14:10:01 UTC 2025
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : pt_BR.UTF-8
LOCALE : pt_BR.UTF-8

pandas : 3.0.0.dev0+2592.gea75dd7b3b
numpy : 2.3.4
dateutil : 2.9.0.post0
pip : 25.2
Cython : 3.2.0b1
sphinx : 8.2.3
IPython : 9.6.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.14.2
bottleneck : 1.6.0
fastparquet : 2024.11.0
fsspec : 2025.9.0
html5lib : 1.1
hypothesis : 6.142.3
gcsfs : 2025.9.0
jinja2 : 3.1.6
lxml.etree : 6.0.2
matplotlib : 3.10.7
numba : 0.62.1
numexpr : 2.14.1
odfpy : None
openpyxl : 3.1.5
psycopg2 : 2.9.11
pymysql : 1.4.6
pyarrow : 21.0.0
pyiceberg : 0.10.0
pyreadstat : 1.3.1
pytest : 8.4.2
python-calamine : None
pytz : 2025.2
pyxlsb : 1.0.10
s3fs : 2025.9.0
scipy : 1.16.2
sqlalchemy : 2.0.44
tables : 3.10.2
tabulate : 0.9.0
xarray : 2025.10.1
xlrd : 2.0.2
xlsxwriter : 3.2.9
zstandard : 0.25.0
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions