Skip to content

Commit

Permalink
BUG: Fix Categorical.min / max bug (#33513)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsaxton authored Apr 13, 2020
1 parent 13dc13f commit 70ca246
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.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ Categorical
- Bug where :class:`Categorical` comparison operator ``__ne__`` would incorrectly evaluate to ``False`` when either element was missing (:issue:`32276`)
- :meth:`Categorical.fillna` now accepts :class:`Categorical` ``other`` argument (:issue:`32420`)
- 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`)

Datetimelike
^^^^^^^^^^^^
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 @@ -2143,7 +2143,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 @@ -2178,7 +2178,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 70ca246

Please sign in to comment.