Skip to content

Commit

Permalink
Backport PR #33513 on branch 1.0.x (BUG: Fix Categorical.min / max bu…
Browse files Browse the repository at this point in the history
…g) (#34022)

Co-authored-by: Daniel Saxton <2658661+dsaxton@users.noreply.github.com>
  • Loading branch information
simonjayhawkins and dsaxton committed May 6, 2020
1 parent 8923fd2 commit 2544c3a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Fixed regressions
- Bug in DataFrame reductions using ``numeric_only=True`` and ExtensionArrays (:issue:`33256`).
- Fix performance regression in ``memory_usage(deep=True)`` for object dtype (:issue:`33012`)
- Bug where :meth:`Categorical.replace` would replace with ``NaN`` whenever the new value and replacement value were equal (:issue:`33288`)
- Bug where an ordered :class:`Categorical` containing only ``NaN`` values would raise rather than returning ``NaN`` when taking the minimum or maximum (:issue:`33450`)
-

.. _whatsnew_104.bug_fixes:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2151,7 +2151,7 @@ def min(self, skipna=True):

good = self._codes != -1
if not good.all():
if skipna:
if skipna and good.any():
pointer = self._codes[good].min()
else:
return np.nan
Expand Down Expand Up @@ -2186,7 +2186,7 @@ def max(self, skipna=True):

good = self._codes != -1
if not good.all():
if skipna:
if skipna and good.any():
pointer = self._codes[good].max()
else:
return np.nan
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/arrays/categorical/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ def test_min_max_with_nan(self, skipna):
assert _min == 2
assert _max == 1

@pytest.mark.parametrize("function", ["min", "max"])
@pytest.mark.parametrize("skipna", [True, False])
def test_min_max_only_nan(self, function, skipna):
# https://github.com/pandas-dev/pandas/issues/33450
cat = Categorical([np.nan], categories=[1, 2], ordered=True)
result = getattr(cat, function)(skipna=skipna)
assert result is np.nan

@pytest.mark.parametrize("method", ["min", "max"])
def test_deprecate_numeric_only_min_max(self, method):
# GH 25303
Expand Down

0 comments on commit 2544c3a

Please sign in to comment.