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
16 changes: 12 additions & 4 deletions 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,15 +35,23 @@ def test_min_max(self):
assert _min == "d"
assert _max == "a"

def test_min_max_empty(self):
@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 isinstance(result, float) and np.isnan(result)
assert result is expected

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

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