Skip to content

Commit 70ca246

Browse files
authored
BUG: Fix Categorical.min / max bug (#33513)
1 parent 13dc13f commit 70ca246

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ Categorical
397397
- Bug where :class:`Categorical` comparison operator ``__ne__`` would incorrectly evaluate to ``False`` when either element was missing (:issue:`32276`)
398398
- :meth:`Categorical.fillna` now accepts :class:`Categorical` ``other`` argument (:issue:`32420`)
399399
- Bug where :meth:`Categorical.replace` would replace with ``NaN`` whenever the new value and replacement value were equal (:issue:`33288`)
400+
- Bug where an ordered :class:`Categorical` containing only ``NaN`` values would raise rather than returning ``NaN`` when taking the minimum or maximum (:issue:`33450`)
400401

401402
Datetimelike
402403
^^^^^^^^^^^^

pandas/core/arrays/categorical.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2143,7 +2143,7 @@ def min(self, skipna=True):
21432143

21442144
good = self._codes != -1
21452145
if not good.all():
2146-
if skipna:
2146+
if skipna and good.any():
21472147
pointer = self._codes[good].min()
21482148
else:
21492149
return np.nan
@@ -2178,7 +2178,7 @@ def max(self, skipna=True):
21782178

21792179
good = self._codes != -1
21802180
if not good.all():
2181-
if skipna:
2181+
if skipna and good.any():
21822182
pointer = self._codes[good].max()
21832183
else:
21842184
return np.nan

pandas/tests/arrays/categorical/test_analytics.py

+8
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ def test_min_max_with_nan(self, skipna):
8888
assert _min == 2
8989
assert _max == 1
9090

91+
@pytest.mark.parametrize("function", ["min", "max"])
92+
@pytest.mark.parametrize("skipna", [True, False])
93+
def test_min_max_only_nan(self, function, skipna):
94+
# https://github.com/pandas-dev/pandas/issues/33450
95+
cat = Categorical([np.nan], categories=[1, 2], ordered=True)
96+
result = getattr(cat, function)(skipna=skipna)
97+
assert result is np.nan
98+
9199
@pytest.mark.parametrize("method", ["min", "max"])
92100
def test_deprecate_numeric_only_min_max(self, method):
93101
# GH 25303

0 commit comments

Comments
 (0)