Skip to content

BUG: Inconsistency in std() defaults between Pandas compared to Xarray and NumPy produces unexpected behaviour #47903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
HarveySouth opened this issue Jul 30, 2022 · 2 comments
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@HarveySouth
Copy link

HarveySouth commented Jul 30, 2022

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 numpy as np
>>> import pandas as pd
>>> d = {'col1': [1,3,3,7]}
>>> df = pd.DataFrame(data=d)
>>> print(df.std())
col1    2.516611
dtype: float64
>>> arr = df.to_numpy()
>>> print(arr.std())
2.179449471770337

Issue Description

Due to the difference in defaults for the std() function on data between numpy and pandas, applying the same function to the same data produces, unexpectedly, a different result. In pandas, ddof=1 by default and in numpy ddof=0. The package xarray uses the numpy default for std, and so produces a different result than the pandas std on the same data. The inconsistency can be confusing for the user.

Expected Behavior

The expected behaviour would be that when the data is converted between objects the same function defaults applied to the object produce the same output. e.g.

>>> import numpy as np
>>> import pandas as pd
>>> d = {'col1': [1,3,3,7]}
>>> df = pd.DataFrame(data=d)
>>> print(df.std())
col1    2.179449
dtype: float64
>>> arr = df.to_numpy()
>>> print(arr.std())
2.179449471770337

Installed Versions

INSTALLED VERSIONS

commit : e8093ba
python : 3.10.4.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-122-generic
Version : #138-Ubuntu SMP Wed Jun 22 15:00:31 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.3
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 61.2.0
pip : 22.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : 1.3.5
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : None
matplotlib : None
numba : None
numexpr : 2.8.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : 0.20.1
xlrd : None
xlwt : None
zstandard : None

@HarveySouth HarveySouth added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 30, 2022
@HarveySouth
Copy link
Author

take

@HarveySouth HarveySouth removed their assignment Jul 30, 2022
@HarveySouth
Copy link
Author

I have seen #10242 , but it seems strange that the documentation specifically points this out. while offset by 1 might give more informative values for std, is that more valuable than consistency?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

1 participant