From 218e5cda89d66ad0d3367c55a886255d4904d2b2 Mon Sep 17 00:00:00 2001 From: Kyungtae Kim Date: Tue, 8 Oct 2024 00:29:40 +0900 Subject: [PATCH] BUG: fix #59965 skipna=True operations don't skip NaN in FloatingArrays - 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 --- pandas/core/array_algos/masked_reductions.py | 3 +++ pandas/tests/series/test_reductions.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/pandas/core/array_algos/masked_reductions.py b/pandas/core/array_algos/masked_reductions.py index f2a32fbe2b0e5..4a5a8f0c4e61d 100644 --- a/pandas/core/array_algos/masked_reductions.py +++ b/pandas/core/array_algos/masked_reductions.py @@ -12,6 +12,7 @@ from pandas._libs import missing as libmissing +from pandas.core.missing import isna from pandas.core.nanops import check_below_min_count if TYPE_CHECKING: @@ -57,6 +58,8 @@ def _reductions( else: return func(values, axis=axis, **kwargs) else: + mask |= isna(values) + if check_below_min_count(values.shape, mask, min_count) and ( axis is None or values.ndim == 1 ): diff --git a/pandas/tests/series/test_reductions.py b/pandas/tests/series/test_reductions.py index 7bbb902e14a36..15ed3097db8ac 100644 --- a/pandas/tests/series/test_reductions.py +++ b/pandas/tests/series/test_reductions.py @@ -223,3 +223,11 @@ def test_median_with_convertible_string_raises(): df = ser.to_frame() with pytest.raises(TypeError, match=msg): df.median() + + +def test_mean_with_skipna(): + # GH#59965 skipna=True operations don't skip NaN in FloatingArrays + series1 = Series({"a": 0.0, "b": 1, "c": 1}) + series2 = Series({"a": 0.0, "b": 2, "c": 2}) + result = series1.convert_dtypes() / series2.convert_dtypes() + assert pd.notna(result.mean(skipna=True))