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
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 10 additions & 0 deletions pandas/tests/arrays/categorical/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ def test_min_max(self):
assert _min == "d"
assert _max == "a"

def test_min_max_empty(self):
# GH 30227
cat = Categorical([], categories=list("ABC"), ordered=True)

result = cat.min()
assert isinstance(result, float) and np.isnan(result)

result = cat.max()
assert isinstance(result, float) and np.isnan(result)

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