Skip to content

Commit 46a4573

Browse files
simonjayhawkinsjreback
authored andcommitted
TST: Add message checks to tests/arrays/categorical/ (pandas-dev#30242)
1 parent 04fce81 commit 46a4573

File tree

5 files changed

+113
-84
lines changed

5 files changed

+113
-84
lines changed

pandas/tests/arrays/categorical/test_algos.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,21 @@ def test_isin_cats():
6060

6161

6262
@pytest.mark.parametrize(
63-
"to_replace, value, result",
64-
[("b", "c", ["a", "c"]), ("c", "d", ["a", "b"]), ("b", None, ["a", None])],
63+
"to_replace, value, result, expected_error_msg",
64+
[
65+
("b", "c", ["a", "c"], "Categorical.categories are different"),
66+
("c", "d", ["a", "b"], None),
67+
("b", None, ["a", None], "Categorical.categories length are different"),
68+
],
6569
)
66-
def test_replace(to_replace, value, result):
70+
def test_replace(to_replace, value, result, expected_error_msg):
6771
# GH 26988
6872
cat = pd.Categorical(["a", "b"])
6973
expected = pd.Categorical(result)
7074
result = cat.replace(to_replace, value)
7175
tm.assert_categorical_equal(result, expected)
7276
if to_replace == "b": # the "c" test is supposed to be unchanged
73-
with pytest.raises(AssertionError):
77+
with pytest.raises(AssertionError, match=expected_error_msg):
7478
# ensure non-inplace call does not affect original
7579
tm.assert_categorical_equal(cat, expected)
7680
cat.replace(to_replace, value, inplace=True)
@@ -104,13 +108,21 @@ def test_take_positive_no_warning(self):
104108
def test_take_bounds(self, allow_fill):
105109
# https://github.com/pandas-dev/pandas/issues/20664
106110
cat = pd.Categorical(["a", "b", "a"])
107-
with pytest.raises(IndexError):
111+
if allow_fill:
112+
msg = "indices are out-of-bounds"
113+
else:
114+
msg = "index 4 is out of bounds for size 3"
115+
with pytest.raises(IndexError, match=msg):
108116
cat.take([4, 5], allow_fill=allow_fill)
109117

110118
def test_take_empty(self, allow_fill):
111119
# https://github.com/pandas-dev/pandas/issues/20664
112120
cat = pd.Categorical([], categories=["a", "b"])
113-
with pytest.raises(IndexError):
121+
if allow_fill:
122+
msg = "indices are out-of-bounds"
123+
else:
124+
msg = "cannot do a non-empty take from an empty axes"
125+
with pytest.raises(IndexError, match=msg):
114126
cat.take([0], allow_fill=allow_fill)
115127

116128
def test_positional_take(self, ordered_fixture):

pandas/tests/arrays/categorical/test_analytics.py

+26-24
Original file line numberDiff line numberDiff line change
@@ -271,40 +271,42 @@ def test_map(self):
271271
# GH 12766: Return an index not an array
272272
tm.assert_index_equal(result, Index(np.array([1] * 5, dtype=np.int64)))
273273

274-
def test_validate_inplace(self):
274+
@pytest.mark.parametrize("value", [1, "True", [1, 2, 3], 5.0])
275+
def test_validate_inplace_raises(self, value):
275276
cat = Categorical(["A", "B", "B", "C", "A"])
276-
invalid_values = [1, "True", [1, 2, 3], 5.0]
277-
278-
for value in invalid_values:
279-
with pytest.raises(ValueError):
280-
cat.set_ordered(value=True, inplace=value)
277+
msg = (
278+
'For argument "inplace" expected type bool, '
279+
f"received type {type(value).__name__}"
280+
)
281+
with pytest.raises(ValueError, match=msg):
282+
cat.set_ordered(value=True, inplace=value)
281283

282-
with pytest.raises(ValueError):
283-
cat.as_ordered(inplace=value)
284+
with pytest.raises(ValueError, match=msg):
285+
cat.as_ordered(inplace=value)
284286

285-
with pytest.raises(ValueError):
286-
cat.as_unordered(inplace=value)
287+
with pytest.raises(ValueError, match=msg):
288+
cat.as_unordered(inplace=value)
287289

288-
with pytest.raises(ValueError):
289-
cat.set_categories(["X", "Y", "Z"], rename=True, inplace=value)
290+
with pytest.raises(ValueError, match=msg):
291+
cat.set_categories(["X", "Y", "Z"], rename=True, inplace=value)
290292

291-
with pytest.raises(ValueError):
292-
cat.rename_categories(["X", "Y", "Z"], inplace=value)
293+
with pytest.raises(ValueError, match=msg):
294+
cat.rename_categories(["X", "Y", "Z"], inplace=value)
293295

294-
with pytest.raises(ValueError):
295-
cat.reorder_categories(["X", "Y", "Z"], ordered=True, inplace=value)
296+
with pytest.raises(ValueError, match=msg):
297+
cat.reorder_categories(["X", "Y", "Z"], ordered=True, inplace=value)
296298

297-
with pytest.raises(ValueError):
298-
cat.add_categories(new_categories=["D", "E", "F"], inplace=value)
299+
with pytest.raises(ValueError, match=msg):
300+
cat.add_categories(new_categories=["D", "E", "F"], inplace=value)
299301

300-
with pytest.raises(ValueError):
301-
cat.remove_categories(removals=["D", "E", "F"], inplace=value)
302+
with pytest.raises(ValueError, match=msg):
303+
cat.remove_categories(removals=["D", "E", "F"], inplace=value)
302304

303-
with pytest.raises(ValueError):
304-
cat.remove_unused_categories(inplace=value)
305+
with pytest.raises(ValueError, match=msg):
306+
cat.remove_unused_categories(inplace=value)
305307

306-
with pytest.raises(ValueError):
307-
cat.sort_values(inplace=value)
308+
with pytest.raises(ValueError, match=msg):
309+
cat.sort_values(inplace=value)
308310

309311
def test_isna(self):
310312
exp = np.array([False, False, True])

pandas/tests/arrays/categorical/test_api.py

+30-25
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@ def test_rename_categories(self):
8383
)
8484
tm.assert_index_equal(cat.categories, Index([1, 2, 3]))
8585

86-
# Lengthen
87-
with pytest.raises(ValueError):
88-
cat.rename_categories([1, 2, 3, 4])
89-
90-
# Shorten
91-
with pytest.raises(ValueError):
92-
cat.rename_categories([1, 2])
86+
@pytest.mark.parametrize("new_categories", [[1, 2, 3, 4], [1, 2]])
87+
def test_rename_categories_wrong_length_raises(self, new_categories):
88+
cat = Categorical(["a", "b", "c", "a"])
89+
msg = (
90+
"new categories need to have the same number of items as the"
91+
" old categories!"
92+
)
93+
with pytest.raises(ValueError, match=msg):
94+
cat.rename_categories(new_categories)
9395

9496
def test_rename_categories_series(self):
9597
# https://github.com/pandas-dev/pandas/issues/17981
@@ -149,19 +151,19 @@ def test_reorder_categories(self):
149151
assert res is None
150152
tm.assert_categorical_equal(cat, new)
151153

152-
# not all "old" included in "new"
154+
@pytest.mark.parametrize(
155+
"new_categories",
156+
[
157+
["a"], # not all "old" included in "new"
158+
["a", "b", "d"], # still not all "old" in "new"
159+
["a", "b", "c", "d"], # all "old" included in "new", but too long
160+
],
161+
)
162+
def test_reorder_categories_raises(self, new_categories):
153163
cat = Categorical(["a", "b", "c", "a"], ordered=True)
154-
155-
with pytest.raises(ValueError):
156-
cat.reorder_categories(["a"])
157-
158-
# still not all "old" in "new"
159-
with pytest.raises(ValueError):
160-
cat.reorder_categories(["a", "b", "d"])
161-
162-
# all "old" included in "new", but too long
163-
with pytest.raises(ValueError):
164-
cat.reorder_categories(["a", "b", "c", "d"])
164+
msg = "items in new_categories are not the same as in old categories"
165+
with pytest.raises(ValueError, match=msg):
166+
cat.reorder_categories(new_categories)
165167

166168
def test_add_categories(self):
167169
cat = Categorical(["a", "b", "c", "a"], ordered=True)
@@ -184,10 +186,6 @@ def test_add_categories(self):
184186
tm.assert_categorical_equal(cat, new)
185187
assert res is None
186188

187-
# new is in old categories
188-
with pytest.raises(ValueError):
189-
cat.add_categories(["d"])
190-
191189
# GH 9927
192190
cat = Categorical(list("abc"), ordered=True)
193191
expected = Categorical(list("abc"), categories=list("abcde"), ordered=True)
@@ -201,6 +199,13 @@ def test_add_categories(self):
201199
res = cat.add_categories(["d", "e"])
202200
tm.assert_categorical_equal(res, expected)
203201

202+
def test_add_categories_existing_raises(self):
203+
# new is in old categories
204+
cat = Categorical(["a", "b", "c", "d"], ordered=True)
205+
msg = re.escape("new categories must not include old categories: {'d'}")
206+
with pytest.raises(ValueError, match=msg):
207+
cat.add_categories(["d"])
208+
204209
def test_set_categories(self):
205210
cat = Categorical(["a", "b", "c", "a"], ordered=True)
206211
exp_categories = Index(["c", "b", "a"])
@@ -453,13 +458,13 @@ def test_codes_immutable(self):
453458
tm.assert_numpy_array_equal(c.codes, exp)
454459

455460
# Assignments to codes should raise
456-
with pytest.raises(ValueError):
461+
with pytest.raises(ValueError, match="cannot set Categorical codes directly"):
457462
c.codes = np.array([0, 1, 2, 0, 1], dtype="int8")
458463

459464
# changes in the codes array should raise
460465
codes = c.codes
461466

462-
with pytest.raises(ValueError):
467+
with pytest.raises(ValueError, match="assignment destination is read-only"):
463468
codes[4] = 1
464469

465470
# But even after getting the codes, the original array should still be

pandas/tests/arrays/categorical/test_indexing.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def test_setitem_different_unordered_raises(self, other):
6363
# GH-24142
6464
target = pd.Categorical(["a", "b"], categories=["a", "b"])
6565
mask = np.array([True, False])
66-
with pytest.raises(ValueError):
66+
msg = "Cannot set a Categorical with another, without identical categories"
67+
with pytest.raises(ValueError, match=msg):
6768
target[mask] = other[mask]
6869

6970
@pytest.mark.parametrize(
@@ -78,8 +79,8 @@ def test_setitem_same_ordered_rasies(self, other):
7879
# Gh-24142
7980
target = pd.Categorical(["a", "b"], categories=["a", "b"], ordered=True)
8081
mask = np.array([True, False])
81-
82-
with pytest.raises(ValueError):
82+
msg = "Cannot set a Categorical with another, without identical categories"
83+
with pytest.raises(ValueError, match=msg):
8384
target[mask] = other[mask]
8485

8586

@@ -152,13 +153,15 @@ def test_categories_assigments(self):
152153
tm.assert_numpy_array_equal(s.__array__(), exp)
153154
tm.assert_index_equal(s.categories, Index([1, 2, 3]))
154155

155-
# lengthen
156-
with pytest.raises(ValueError):
157-
s.categories = [1, 2, 3, 4]
158-
159-
# shorten
160-
with pytest.raises(ValueError):
161-
s.categories = [1, 2]
156+
@pytest.mark.parametrize("new_categories", [[1, 2, 3, 4], [1, 2]])
157+
def test_categories_assigments_wrong_length_raises(self, new_categories):
158+
cat = Categorical(["a", "b", "c", "a"])
159+
msg = (
160+
"new categories need to have the same number of items"
161+
" as the old categories!"
162+
)
163+
with pytest.raises(ValueError, match=msg):
164+
cat.categories = new_categories
162165

163166
# Combinations of sorted/unique:
164167
@pytest.mark.parametrize(

pandas/tests/arrays/categorical/test_operators.py

+26-19
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,25 @@ def test_comparisons(self):
7373
tm.assert_numpy_array_equal(res, exp)
7474

7575
# Only categories with same categories can be compared
76-
with pytest.raises(TypeError):
76+
msg = "Categoricals can only be compared if 'categories' are the same"
77+
with pytest.raises(TypeError, match=msg):
7778
cat > cat_rev
7879

7980
cat_rev_base2 = Categorical(["b", "b", "b"], categories=["c", "b", "a", "d"])
8081

81-
with pytest.raises(TypeError):
82+
msg = (
83+
"Categoricals can only be compared if 'categories' are the same. "
84+
"Categories are different lengths"
85+
)
86+
with pytest.raises(TypeError, match=msg):
8287
cat_rev > cat_rev_base2
8388

8489
# Only categories with same ordering information can be compared
8590
cat_unorderd = cat.set_ordered(False)
8691
assert not (cat > cat).any()
8792

88-
with pytest.raises(TypeError):
93+
msg = "Categoricals can only be compared if 'ordered' is the same"
94+
with pytest.raises(TypeError, match=msg):
8995
cat > cat_unorderd
9096

9197
# comparison (in both directions) with Series will raise
@@ -131,18 +137,6 @@ def test_compare_frame(self):
131137

132138
df = DataFrame(cat)
133139

134-
for op in [
135-
operator.eq,
136-
operator.ne,
137-
operator.ge,
138-
operator.gt,
139-
operator.le,
140-
operator.lt,
141-
]:
142-
with pytest.raises(ValueError):
143-
# alignment raises unless we transpose
144-
op(cat, df)
145-
146140
result = cat == df.T
147141
expected = DataFrame([[True, True, True, True]])
148142
tm.assert_frame_equal(result, expected)
@@ -151,6 +145,15 @@ def test_compare_frame(self):
151145
expected = DataFrame([[False, True, True, False]])
152146
tm.assert_frame_equal(result, expected)
153147

148+
def test_compare_frame_raises(self, all_compare_operators):
149+
# alignment raises unless we transpose
150+
op = getattr(operator, all_compare_operators)
151+
cat = Categorical(["a", "b", 2, "a"])
152+
df = DataFrame(cat)
153+
msg = "Unable to coerce to Series, length must be 1: given 4"
154+
with pytest.raises(ValueError, match=msg):
155+
op(cat, df)
156+
154157
def test_datetime_categorical_comparison(self):
155158
dt_cat = Categorical(date_range("2014-01-01", periods=3), ordered=True)
156159
tm.assert_numpy_array_equal(dt_cat > dt_cat[0], np.array([False, True, True]))
@@ -255,7 +258,8 @@ def test_comparisons(self, data, reverse, base):
255258
tm.assert_numpy_array_equal(res_rev.values, exp_rev2)
256259

257260
# Only categories with same categories can be compared
258-
with pytest.raises(TypeError):
261+
msg = "Categoricals can only be compared if 'categories' are the same"
262+
with pytest.raises(TypeError, match=msg):
259263
cat > cat_rev
260264

261265
# categorical cannot be compared to Series or numpy array, and also
@@ -367,7 +371,9 @@ def test_numeric_like_ops(self):
367371

368372
# numpy ops
369373
s = Series(Categorical([1, 2, 3, 4]))
370-
with pytest.raises(TypeError):
374+
with pytest.raises(
375+
TypeError, match="Categorical cannot perform the operation sum"
376+
):
371377
np.sum(s)
372378

373379
# numeric ops on a Series
@@ -384,7 +390,8 @@ def test_numeric_like_ops(self):
384390
getattr(s, op)(2)
385391

386392
# invalid ufunc
387-
with pytest.raises(TypeError):
393+
msg = "Object with dtype category cannot perform the numpy op log"
394+
with pytest.raises(TypeError, match=msg):
388395
np.log(s)
389396

390397
def test_contains(self):
@@ -394,7 +401,7 @@ def test_contains(self):
394401
assert "b" in c
395402
assert "z" not in c
396403
assert np.nan not in c
397-
with pytest.raises(TypeError):
404+
with pytest.raises(TypeError, match="unhashable type: 'list'"):
398405
assert [1] in c
399406

400407
# assert codes NOT in index

0 commit comments

Comments
 (0)