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
14 changes: 11 additions & 3 deletions 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 @@ -2128,7 +2132,7 @@ def min(self, skipna=True):
self.check_for_ordered("min")

if len(self._codes) == 0:
return np.nan
return na_value_for_dtype(self.dtype)

good = self._codes != -1
if not good.all():
Expand All @@ -2147,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 @@ -2159,7 +2167,7 @@ def max(self, skipna=True):
self.check_for_ordered("max")

if len(self._codes) == 0:
return np.nan
return na_value_for_dtype(self.dtype)

good = self._codes != -1
if not good.all():
Expand Down