Skip to content

BUG: Erratic behavior of rolling().std() #53289

@DevinPrescott

Description

@DevinPrescott

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 matplotlib.pyplot as plt

W = 50 # Window Size
R = 14 # Array Resolution
C = 9 # Size of array range

x = np.linspace(0,C*np.pi,int(R*W))
y = np.sin(x)*np.exp(-x)
y = (y-y.min())/(y-y.min()).max()
df = pd.DataFrame({'y':y}).set_index(x)

fig,ax = plt.subplots()
df.rolling(window=W).std().plot(lw=3,ax=ax)
df.rolling(window=W).apply(np.std).plot(ax=ax,logy=True,lw=3)
ax.grid(True,axis='both',which='both')
ax.legend(['Pandas','Numpy']);

Issue Description

Using .std() on a rolling window on a data set with a "large" range of magnitudes (ie 1e1 to 1e-10) seems to produce erratic results. Comparing to numpy.std shows the expected behavior. The problem seems to be convolved with the range of the data set and the size (1xn) of the data set. Changing just the size of the array in the example above give very different behavior, all incorrect and unexpected:

Array Size R*W, R = 30, W=50
w50r30c9

Array Size R*W, R = 17, W=50
w50r17c9

Array Size R*W, R = 14, W=50
w50r14c9

Expected Behavior

df.rolling(window=W).std() == df.rolling(window=W).apply(np.std)

Installed Versions

INSTALLED VERSIONS

commit : 37ea63d
python : 3.10.10.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.0.1
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.11.0
pandas_datareader: None
bs4 : 4.11.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
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