Skip to content
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

BUG: skipna=True operations don't skip NaN in FloatingArrays #59965

Open
3 tasks done
carlocastoldi opened this issue Oct 4, 2024 · 3 comments · May be fixed by #59997
Open
3 tasks done

BUG: skipna=True operations don't skip NaN in FloatingArrays #59965

carlocastoldi opened this issue Oct 4, 2024 · 3 comments · May be fixed by #59997
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays. Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate PDEP missing values Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint Reduction Operations sum, mean, min, max, etc.

Comments

@carlocastoldi
Copy link

carlocastoldi commented Oct 4, 2024

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

s1 = pd.Series({"a": 0.0, "b": 1, "c": 1, "d": 0})
s2 = pd.Series({"a": 0.0, "b": 2, "c": 2, "d": 2})
s3 = s1/s2
#display(s3)
s4 = s1.convert_dtypes()/s2.convert_dtypes()
#display(s4)
s5 = pd.Series([None,0.5,0.5,0]).convert_dtypes()
#display(s5)
s3.mean(skipna=True), s4.mean(skipna=True), s5.mean(skipna=True)

Issue Description

Following #59961, I understand that series/dataframes of FloatingArrays cointaing np.NaN values are possible and meant to exists. These very same dataframes/series, however, fail to skip NaN values when asked to. The above examples outputs:

(np.float64(0.3333333333333333), <NA>, np.float64(0.3333333333333333))

Expected Behavior

>>> s4.mean(skipna=True)
np.float64(0.3333333333333333)

Installed Versions

INSTALLED VERSIONS

commit : 139def2
python : 3.12.3
python-bits : 64
OS : Linux
OS-release : 6.8.0-41-generic
Version : #41-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 2 20:41:06 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 3.0.0.dev0+1545.g139def2145
numpy : 2.2.0.dev0+git20240930.3ee9e6a
dateutil : 2.9.0.post0
pip : 24.0
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pytz : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@carlocastoldi carlocastoldi added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 4, 2024
@asishm asishm added the PDEP missing values Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint label Oct 4, 2024
@rhshadrach
Copy link
Member

Thanks for the report! It looks like we should be overriding the _reduce method in FloatingArray to properly handle this case. Further investigations and PRs to fix are welcome!

@rhshadrach rhshadrach added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate ExtensionArray Extending pandas with custom dtypes or arrays. Reduction Operations sum, mean, min, max, etc. and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 5, 2024
cooolheater added a commit to cooolheater/pandas that referenced this issue Oct 7, 2024
…oatingArrays

    - Issue: The skipna was not properly handled for BaseMaskedArray
    - Fix: Added mask for NA values
    - Test: Added test to series/test_reductions since the test uses
@cooolheater
Copy link

cooolheater commented Oct 9, 2024

As I checked, the issue occurs only if isinstance(delegate, ExtensionArray) case of Series::_reduce.
Despite the skipna is passed to _reductions, it is not applied to mask.

So, to apply skipna=True, the 'isna' need to be applied to 'mask'.
So I suggested a PR fixing that.

@rhshadrach
Copy link
Member

When I first triaged this issue, I was not aware of #53887. Since pd.isna does not pick up on the NaN values, I am wondering if skipna=True should skip them. Do we think this should wait for #58988?

cc @jorisvandenbossche

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays. Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate PDEP missing values Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint Reduction Operations sum, mean, min, max, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants