Skip to content

Commit e87a9a9

Browse files
2 parents 3ad3358 + 455de19 commit e87a9a9

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

doc/source/whatsnew/v1.1.0.rst

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

440441
Datetimelike
441442
^^^^^^^^^^^^

pandas/core/arrays/categorical.py

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

21462146
good = self._codes != -1
21472147
if not good.all():
2148-
if skipna:
2148+
if skipna and good.any():
21492149
pointer = self._codes[good].min()
21502150
else:
21512151
return np.nan
@@ -2180,7 +2180,7 @@ def max(self, skipna=True):
21802180

21812181
good = self._codes != -1
21822182
if not good.all():
2183-
if skipna:
2183+
if skipna and good.any():
21842184
pointer = self._codes[good].max()
21852185
else:
21862186
return np.nan

pandas/core/arrays/integer.py

-4
Original file line numberDiff line numberDiff line change
@@ -571,10 +571,6 @@ def _reduce(self, name: str, skipna: bool = True, **kwargs):
571571
if np.isnan(result):
572572
return libmissing.NA
573573

574-
# if we have a boolean op, don't coerce
575-
if name in ["any", "all"]:
576-
pass
577-
578574
return result
579575

580576
def _maybe_mask_result(self, result, mask, other, op_name: str):

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)