Skip to content

Commit 0255ab3

Browse files
BUG: fix nanmedian for CoW without bottleneck (#55742)
1 parent bad1e09 commit 0255ab3

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

pandas/core/nanops.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,10 @@ def nanmedian(values, *, axis: AxisInt | None = None, skipna: bool = True, mask=
757757
>>> nanops.nanmedian(s.values)
758758
2.0
759759
"""
760+
# for floats without mask, the data already uses NaN as missing value
761+
# indicator, and `mask` will be calculated from that below -> in those
762+
# cases we never need to set NaN to the masked values
763+
using_nan_sentinel = values.dtype.kind == "f" and mask is None
760764

761765
def get_median(x, _mask=None):
762766
if _mask is None:
@@ -774,7 +778,7 @@ def get_median(x, _mask=None):
774778
return res
775779

776780
dtype = values.dtype
777-
values, mask = _get_values(values, skipna, mask=mask, fill_value=0)
781+
values, mask = _get_values(values, skipna, mask=mask, fill_value=None)
778782
if values.dtype.kind != "f":
779783
if values.dtype == object:
780784
# GH#34671 avoid casting strings to numeric
@@ -786,7 +790,9 @@ def get_median(x, _mask=None):
786790
except ValueError as err:
787791
# e.g. "could not convert string to float: 'a'"
788792
raise TypeError(str(err)) from err
789-
if mask is not None:
793+
if not using_nan_sentinel and mask is not None:
794+
if not values.flags.writeable:
795+
values = values.copy()
790796
values[mask] = np.nan
791797

792798
notempty = values.size

0 commit comments

Comments
 (0)