Skip to content

TST/CLN: Clean categorical min / max tests #33512

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 5 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 12 additions & 24 deletions pandas/tests/arrays/categorical/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,28 @@ def test_min_max_ordered(self):
@pytest.mark.parametrize("aggregation", ["min", "max"])
def test_min_max_ordered_empty(self, categories, expected, aggregation):
# GH 30227
cat = Categorical([], categories=list("ABC"), ordered=True)
cat = Categorical([], categories=categories, ordered=True)

agg_func = getattr(cat, aggregation)
result = agg_func()
assert result is expected

@pytest.mark.parametrize(
"values, categories",
[(["a", "b", "c", np.nan], list("cba")), ([1, 2, 3, np.nan], [3, 2, 1])],
)
@pytest.mark.parametrize("skipna", [True, False])
def test_min_max_with_nan(self, skipna):
@pytest.mark.parametrize("function", ["min", "max"])
def test_min_max_with_nan(self, values, categories, function, skipna):
# GH 25303
cat = Categorical(
[np.nan, "b", "c", np.nan], categories=["d", "c", "b", "a"], ordered=True
)
_min = cat.min(skipna=skipna)
_max = cat.max(skipna=skipna)

if skipna is False:
assert np.isnan(_min)
assert np.isnan(_max)
else:
assert _min == "c"
assert _max == "b"

cat = Categorical(
[np.nan, 1, 2, np.nan], categories=[5, 4, 3, 2, 1], ordered=True
)
_min = cat.min(skipna=skipna)
_max = cat.max(skipna=skipna)
cat = Categorical(values, categories=categories, ordered=True)
result = getattr(cat, function)(skipna=skipna)

if skipna is False:
assert np.isnan(_min)
assert np.isnan(_max)
assert result is np.nan
else:
assert _min == 2
assert _max == 1
expected = categories[0] if function == "min" else categories[2]
assert result == expected

@pytest.mark.parametrize("function", ["min", "max"])
@pytest.mark.parametrize("skipna", [True, False])
Expand Down
78 changes: 28 additions & 50 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,67 +1072,45 @@ class TestCategoricalSeriesReductions:
# were moved from a series-specific test file, _not_ that these tests are
# intended long-term to be series-specific

def test_min_max(self):
@pytest.mark.parametrize("function", ["min", "max"])
def test_min_max_unordered_raises(self, function):
# unordered cats have no min/max
cat = Series(Categorical(["a", "b", "c", "d"], ordered=False))
with pytest.raises(TypeError):
cat.min()
with pytest.raises(TypeError):
cat.max()

cat = Series(Categorical(["a", "b", "c", "d"], ordered=True))
_min = cat.min()
_max = cat.max()
assert _min == "a"
assert _max == "d"

cat = Series(
Categorical(
["a", "b", "c", "d"], categories=["d", "c", "b", "a"], ordered=True
)
)
_min = cat.min()
_max = cat.max()
assert _min == "d"
assert _max == "a"

cat = Series(
Categorical(
[np.nan, "b", "c", np.nan],
categories=["d", "c", "b", "a"],
ordered=True,
)
)
_min = cat.min()
_max = cat.max()
assert _min == "c"
assert _max == "b"
msg = f"Categorical is not ordered for operation {function}"
with pytest.raises(TypeError, match=msg):
getattr(cat, function)()

cat = Series(
Categorical(
[np.nan, 1, 2, np.nan], categories=[5, 4, 3, 2, 1], ordered=True
)
)
_min = cat.min()
_max = cat.max()
assert _min == 2
assert _max == 1
@pytest.mark.parametrize(
"values, categories",
[
(list("abc"), list("abc")),
(list("abc"), list("cba")),
(list("abc") + [np.nan], list("cba")),
([1, 2, 3], [3, 2, 1]),
([1, 2, 3, np.nan], [3, 2, 1]),
],
)
@pytest.mark.parametrize("function", ["min", "max"])
def test_min_max_ordered(self, values, categories, function):
# GH 25303
cat = Series(Categorical(values, categories=categories, ordered=True))
result = getattr(cat, function)(skipna=True)
expected = categories[0] if function == "min" else categories[2]
assert result == expected

@pytest.mark.parametrize("function", ["min", "max"])
@pytest.mark.parametrize("skipna", [True, False])
def test_min_max_skipna(self, skipna):
# GH 25303
def test_min_max_skipna(self, function, skipna):
cat = Series(
Categorical(["a", "b", np.nan, "a"], categories=["b", "a"], ordered=True)
)
_min = cat.min(skipna=skipna)
_max = cat.max(skipna=skipna)
result = getattr(cat, function)(skipna=skipna)

if skipna is True:
assert _min == "b"
assert _max == "a"
expected = "b" if function == "min" else "a"
assert result == expected
else:
assert np.isnan(_min)
assert np.isnan(_max)
assert result is np.nan


class TestSeriesMode:
Expand Down