BUG: fix nanmedian for CoW without bottleneck #55742
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixing a failing test that turned up in #55732.
This PR specifically fixes
pandas/tests/frame/test_reductions.py::test_reduction_axis_none_returns_scalar[float64-True-median]
, i.e. callingdf.median(axis=None)
, for builds that don't have bottleneck installed.If the underlying data of the DataFrame or Series is float64 data, the
values
insidenanops.nanmedian(..)
will be the original values from the DataFrame, and with CoW set to be read-only (for other dtypes, we cast to float64 inside the function, and so that will always give a copy).And then we do
values[mask] = np.nan
, which raises an error if values is read-only. In practice, this didn't give any problems with mutating the original DataFrame, because if you start with float64 values, themask
always corresponds with NaN values already (so we were actually mutating the calling dataframe, just without any effect).