Skip to content

Commit 859cf95

Browse files
authored
TST/CLN: Clean categorical min / max tests (#33512)
* TST/CLN: Clean categorical min / max tests * Clean * Add error message
1 parent b263236 commit 859cf95

File tree

2 files changed

+40
-74
lines changed

2 files changed

+40
-74
lines changed

pandas/tests/arrays/categorical/test_analytics.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,40 +53,28 @@ def test_min_max_ordered(self):
5353
@pytest.mark.parametrize("aggregation", ["min", "max"])
5454
def test_min_max_ordered_empty(self, categories, expected, aggregation):
5555
# GH 30227
56-
cat = Categorical([], categories=list("ABC"), ordered=True)
56+
cat = Categorical([], categories=categories, ordered=True)
5757

5858
agg_func = getattr(cat, aggregation)
5959
result = agg_func()
6060
assert result is expected
6161

62+
@pytest.mark.parametrize(
63+
"values, categories",
64+
[(["a", "b", "c", np.nan], list("cba")), ([1, 2, 3, np.nan], [3, 2, 1])],
65+
)
6266
@pytest.mark.parametrize("skipna", [True, False])
63-
def test_min_max_with_nan(self, skipna):
67+
@pytest.mark.parametrize("function", ["min", "max"])
68+
def test_min_max_with_nan(self, values, categories, function, skipna):
6469
# GH 25303
65-
cat = Categorical(
66-
[np.nan, "b", "c", np.nan], categories=["d", "c", "b", "a"], ordered=True
67-
)
68-
_min = cat.min(skipna=skipna)
69-
_max = cat.max(skipna=skipna)
70-
71-
if skipna is False:
72-
assert np.isnan(_min)
73-
assert np.isnan(_max)
74-
else:
75-
assert _min == "c"
76-
assert _max == "b"
77-
78-
cat = Categorical(
79-
[np.nan, 1, 2, np.nan], categories=[5, 4, 3, 2, 1], ordered=True
80-
)
81-
_min = cat.min(skipna=skipna)
82-
_max = cat.max(skipna=skipna)
70+
cat = Categorical(values, categories=categories, ordered=True)
71+
result = getattr(cat, function)(skipna=skipna)
8372

8473
if skipna is False:
85-
assert np.isnan(_min)
86-
assert np.isnan(_max)
74+
assert result is np.nan
8775
else:
88-
assert _min == 2
89-
assert _max == 1
76+
expected = categories[0] if function == "min" else categories[2]
77+
assert result == expected
9078

9179
@pytest.mark.parametrize("function", ["min", "max"])
9280
@pytest.mark.parametrize("skipna", [True, False])

pandas/tests/reductions/test_reductions.py

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,67 +1072,45 @@ class TestCategoricalSeriesReductions:
10721072
# were moved from a series-specific test file, _not_ that these tests are
10731073
# intended long-term to be series-specific
10741074

1075-
def test_min_max(self):
1075+
@pytest.mark.parametrize("function", ["min", "max"])
1076+
def test_min_max_unordered_raises(self, function):
10761077
# unordered cats have no min/max
10771078
cat = Series(Categorical(["a", "b", "c", "d"], ordered=False))
1078-
with pytest.raises(TypeError):
1079-
cat.min()
1080-
with pytest.raises(TypeError):
1081-
cat.max()
1082-
1083-
cat = Series(Categorical(["a", "b", "c", "d"], ordered=True))
1084-
_min = cat.min()
1085-
_max = cat.max()
1086-
assert _min == "a"
1087-
assert _max == "d"
1088-
1089-
cat = Series(
1090-
Categorical(
1091-
["a", "b", "c", "d"], categories=["d", "c", "b", "a"], ordered=True
1092-
)
1093-
)
1094-
_min = cat.min()
1095-
_max = cat.max()
1096-
assert _min == "d"
1097-
assert _max == "a"
1098-
1099-
cat = Series(
1100-
Categorical(
1101-
[np.nan, "b", "c", np.nan],
1102-
categories=["d", "c", "b", "a"],
1103-
ordered=True,
1104-
)
1105-
)
1106-
_min = cat.min()
1107-
_max = cat.max()
1108-
assert _min == "c"
1109-
assert _max == "b"
1079+
msg = f"Categorical is not ordered for operation {function}"
1080+
with pytest.raises(TypeError, match=msg):
1081+
getattr(cat, function)()
11101082

1111-
cat = Series(
1112-
Categorical(
1113-
[np.nan, 1, 2, np.nan], categories=[5, 4, 3, 2, 1], ordered=True
1114-
)
1115-
)
1116-
_min = cat.min()
1117-
_max = cat.max()
1118-
assert _min == 2
1119-
assert _max == 1
1083+
@pytest.mark.parametrize(
1084+
"values, categories",
1085+
[
1086+
(list("abc"), list("abc")),
1087+
(list("abc"), list("cba")),
1088+
(list("abc") + [np.nan], list("cba")),
1089+
([1, 2, 3], [3, 2, 1]),
1090+
([1, 2, 3, np.nan], [3, 2, 1]),
1091+
],
1092+
)
1093+
@pytest.mark.parametrize("function", ["min", "max"])
1094+
def test_min_max_ordered(self, values, categories, function):
1095+
# GH 25303
1096+
cat = Series(Categorical(values, categories=categories, ordered=True))
1097+
result = getattr(cat, function)(skipna=True)
1098+
expected = categories[0] if function == "min" else categories[2]
1099+
assert result == expected
11201100

1101+
@pytest.mark.parametrize("function", ["min", "max"])
11211102
@pytest.mark.parametrize("skipna", [True, False])
1122-
def test_min_max_skipna(self, skipna):
1123-
# GH 25303
1103+
def test_min_max_skipna(self, function, skipna):
11241104
cat = Series(
11251105
Categorical(["a", "b", np.nan, "a"], categories=["b", "a"], ordered=True)
11261106
)
1127-
_min = cat.min(skipna=skipna)
1128-
_max = cat.max(skipna=skipna)
1107+
result = getattr(cat, function)(skipna=skipna)
11291108

11301109
if skipna is True:
1131-
assert _min == "b"
1132-
assert _max == "a"
1110+
expected = "b" if function == "min" else "a"
1111+
assert result == expected
11331112
else:
1134-
assert np.isnan(_min)
1135-
assert np.isnan(_max)
1113+
assert result is np.nan
11361114

11371115

11381116
class TestSeriesMode:

0 commit comments

Comments
 (0)