Skip to content

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

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
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Other enhancements
- Support passing a :class:`Iterable[Hashable]` input to :meth:`DataFrame.drop_duplicates` (:issue:`59237`)
- Support reading Stata 102-format (Stata 1) dta files (:issue:`58978`)
- Support reading Stata 110-format (Stata 7) dta files (:issue:`47176`)
- Support skipna=True in operations on Float64 arrays with null values (:issue:`59965`)

.. ---------------------------------------------------------------------------
.. _whatsnew_300.notable_bug_fixes:
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/array_algos/masked_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

from pandas._libs import missing as libmissing

from pandas.core.dtypes.common import is_float_dtype

from pandas.core.missing import isna
from pandas.core.nanops import check_below_min_count

if TYPE_CHECKING:
Expand Down Expand Up @@ -57,6 +60,9 @@ def _reductions(
else:
return func(values, axis=axis, **kwargs)
else:
if is_float_dtype(values):
mask |= isna(values)

if check_below_min_count(values.shape, mask, min_count) and (
axis is None or values.ndim == 1
):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,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}, dtype="Float64")
series2 = Series({"a": 0.0, "b": 2, "c": 2}, dtype="Float64")
result = series1 / series2
assert np.isclose(result.mean(skipna=True), 0.5)
Loading