Skip to content

BUG: min/max on empty categorical fails #30227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ Categorical
same type as if one used the :meth:`.str.` / :meth:`.dt.` on a :class:`Series` of that type. E.g. when accessing :meth:`Series.dt.tz_localize` on a
:class:`Categorical` with duplicate entries, the accessor was skipping duplicates (:issue:`27952`)
- Bug in :meth:`DataFrame.replace` and :meth:`Series.replace` that would give incorrect results on categorical data (:issue:`26988`)
- Bug where calling :meth:`Categorical.min` or :meth:`Categorical.max` on an empty Categorical would raise a numpy exception (:issue:`30227`)


Datetimelike
Expand Down
18 changes: 17 additions & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from pandas.core.dtypes.dtypes import CategoricalDtype
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
from pandas.core.dtypes.inference import is_hashable
from pandas.core.dtypes.missing import isna, notna
from pandas.core.dtypes.missing import isna, na_value_for_dtype, notna

from pandas._typing import ArrayLike, Dtype, Ordered
from pandas.core import ops
Expand Down Expand Up @@ -2116,6 +2116,10 @@ def min(self, skipna=True):

Only ordered `Categoricals` have a minimum!

.. versionchanged:: 1.0.0

Returns an NA value on empty arrays

Raises
------
TypeError
Expand All @@ -2126,6 +2130,10 @@ def min(self, skipna=True):
min : the minimum of this `Categorical`
"""
self.check_for_ordered("min")

if not len(self._codes):
return na_value_for_dtype(self.dtype)

good = self._codes != -1
if not good.all():
if skipna:
Expand All @@ -2143,6 +2151,10 @@ def max(self, skipna=True):

Only ordered `Categoricals` have a maximum!

.. versionchanged:: 1.0.0

Returns an NA value on empty arrays

Raises
------
TypeError
Expand All @@ -2153,6 +2165,10 @@ def max(self, skipna=True):
max : the maximum of this `Categorical`
"""
self.check_for_ordered("max")

if not len(self._codes):
return na_value_for_dtype(self.dtype)

good = self._codes != -1
if not good.all():
if skipna:
Expand Down
20 changes: 19 additions & 1 deletion pandas/tests/arrays/categorical/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pandas.compat import PYPY

from pandas import Categorical, Index, Series
from pandas import Categorical, Index, Series, date_range, NaT
from pandas.api.types import is_scalar
import pandas.util.testing as tm

Expand Down Expand Up @@ -35,6 +35,24 @@ def test_min_max(self):
assert _min == "d"
assert _max == "a"

@pytest.mark.parametrize(
"categories,expected",
[
(list("ABC"), np.NaN),
([1, 2, 3], np.NaN),
(Series(date_range("2020-01-01", periods=3), dtype="category"), NaT),
],
)
def test_min_max_empty(self, categories, expected):
# GH 30227
cat = Categorical([], categories=list("ABC"), ordered=True)

result = cat.min()
assert result is expected

result = cat.max()
assert result is expected

@pytest.mark.parametrize("skipna", [True, False])
def test_min_max_with_nan(self, skipna):
# GH 25303
Expand Down