Skip to content

BUG: Series rolling standard deviation gives zero for small numbers #40448

Closed
@wuyuanyi135

Description

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import pandas as pd
s = pd.Series([5, 5, 6, 7, 5, 2, 5])
print("## Working sample:")
print(s.rolling(3).std())

s = pd.Series([5, 5, 6, 7, 5, 2, 5]) * 1e-8
print("## Doesn't work for small data:")
std = s.rolling(3).std()
print(std)

assert std.iloc[-2] == 0

data = list(s.rolling(3))[-2]
std_manual = data.std()
print("## Manually calculated std:", std_manual)

assert std.iloc[-2] == std_manual, "Rolling std and manually calculated rolling std do not match"

Problem description

I found that when the series consists of small floating numbers (about the order of 1e-8) the rolling standard deviation will be zero. If I use the same window and compute the standard deviation directly the output is non-zero. Is there an internal precision setting for the rolling std function?

Expected Output

s = pd.Series([5, 5, 6, 7, 5, 2, 5]) * 1e-8
std = s.rolling(3).std()

std should be nonzero for the last few elements.

Output of pd.show_versions()

INSTALLED VERSIONS

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

pandas : 1.2.3
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 49.6.0.post20210108
Cython : None
pytest : None
hypothesis : None
sphinx : 3.2.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.21.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : None
tables : None
tabulate : 0.8.7
xarray : None
xlrd : None
xlwt : None
numba : 0.52.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions