From 7f0ff96bce6ecd6192a31e4c64394a97c5098527 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sat, 28 Oct 2023 12:20:48 +0200 Subject: [PATCH 1/2] BUG: fix nanmedian for CoW without bottleneck --- pandas/core/nanops.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index e60c42a20a9af..091276d9503db 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -757,6 +757,10 @@ def nanmedian(values, *, axis: AxisInt | None = None, skipna: bool = True, mask= >>> nanops.nanmedian(s.values) 2.0 """ + # for floats without mask, the data already uses NaN as missing value + # indicator, and `mask` will be calculated from that below -> in those + # cases we never need to set NaN to the masked values + using_nan_sentinel = values.dtype.kind == "f" and mask is None def get_median(x, _mask=None): if _mask is None: @@ -786,7 +790,9 @@ def get_median(x, _mask=None): except ValueError as err: # e.g. "could not convert string to float: 'a'" raise TypeError(str(err)) from err - if mask is not None: + if not using_nan_sentinel and mask is not None: + if not values.flags.writeable: + values = values.copy() values[mask] = np.nan notempty = values.size From 336cb6935e8429fe2e4bdd9e43137ce7daf3a26b Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 30 Oct 2023 13:23:16 +0100 Subject: [PATCH 2/2] don't fill with 0 for nanmedian --- pandas/core/nanops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 091276d9503db..20dc3f20bbb5e 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -778,7 +778,7 @@ def get_median(x, _mask=None): return res dtype = values.dtype - values, mask = _get_values(values, skipna, mask=mask, fill_value=0) + values, mask = _get_values(values, skipna, mask=mask, fill_value=None) if values.dtype.kind != "f": if values.dtype == object: # GH#34671 avoid casting strings to numeric