Skip to content

BUG: Pandas rolling std precision error #46049

Closed
@juanmpga

Description

@juanmpga

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

data = pd.Series([-3.0, -3.0, -4.0, -5.0, -4.0, -3.0, -4.0, -4.0, -3.0, -4.0, -3.0, -4.0, -3.0, -2.0, -2.0, -3.0, -2.0, -2.0, -3.0, -3.0, -4.0, -4.0, -4.0, -4.0, -4.0, -5.0, -5.0, -5.0, -5.0, -5.0])

print(data.rolling(5).std().iloc[-1]) # <-- yields 1.2904784139758924e-08 instead of 0
print(data.tail(25).rolling(5).std().iloc[-1]) # <-- correctly yields 0

Issue Description

There seems to be a precision problem with rolling.std(). Including more data in the pd.Series affects noticeably the result of calculations that are quite far apart (several times the rolling window). I think it may be an error introduced by the online algorithm that is used.

Related:
#37055
#37051
#39872

Expected Behavior

print(data.rolling(5).std().iloc[-1])
The above should yield 0 as the 5 last data points are equal.

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.8.12.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18363
machine : AMD64
processor : Intel64 Family 6 Model 85 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : es_ES.cp1252

pandas : 1.4.1
numpy : 1.21.5
pytz : 2021.3
dateutil : 2.8.2
pip : 20.3.3
setuptools : 58.0.4
Cython : None
pytest : 7.0.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : 7.31.1
pandas_datareader: None
bs4 : None
bottleneck : 1.3.2
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.1
numba : 0.55.1
numexpr : 2.8.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
sqlalchemy : 1.3.24
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
zstandard : 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