Description
Edit[rhshadrach]: The original report about inconsistent skipna arguments in groupby is captured in #15675. For this issue, it suffices to add notes to the documentation about the default incompatibility between pandas and NumPy as described below.
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
np_series = [
np.array(['A','A','A', 'B', 'B']),
np.array([1, 2, 3, 4, 5]),
np.array([np.nan, 2, 3, 4, 5])
]
# NumPy behavior
display(np.std(np_series[1], ddof=1)) # 1.5811388300841898
display(np.std(np_series[2], ddof=1)) # nan
# Pandas DataFrame behavior
df = pd.DataFrame(dict(zip(['Group', 'Val1', 'Val2'], np_series)))
display(df[['Val1', 'Val2']].std().to_frame())
display(df[['Val1', 'Val2']].std(skipna=False).to_frame())
# Pandas Series behavior
display(pd.Series(np_series[1]).std())
display(pd.Series(np_series[2]).std())
display(pd.Series(np_series[2]).std(skipna=False)) # equivalent to the default numpy behavior when called with ddof=1
# GroupBy behavior
display(df.groupby('Group').std()) # no nans reported, no option to pass skipna parameter
Issue Description
I've wasted quite some time trying to find out why in some edge cases (on a large dataset) a different value was reported by DataFrame.std vs numpy.std. The posts and documentation I've found focus on the DDOF parameter only. It turned out to be related to fewer NaN values being reported.
Expected Behavior
ChatGPT hinted to me that the Pandas Series has a skipna=True argument. This is different from the numpy.std, which does NOT skip na by default. And even has no such optional argument.
I would suggest to:
- Add documentation to all DataFrame, Series and GroupBy classes regarding this different default behavior.
- Add the skipna=True argument to the Series.std and GroupBy.std methods
I can contribute this patch if this is indeed the right way to go.
Installed Versions
INSTALLED VERSIONS
commit : 0f43794
python : 3.11.6.final.0
python-bits : 64
OS : Linux
OS-release : 4.18.0-372.19.1.el8_6.x86_64
Version : #1 SMP Tue Aug 2 16:19:42 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.0.3
numpy : 1.26.0
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.2.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.1.6
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.9
jinja2 : 3.1.2
IPython : 8.16.1
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : 1.1.0
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.0
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 13.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.3
snappy : None
sqlalchemy : 1.4.49
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None