Skip to content

Commit 30bb0f0

Browse files
authored
add messages to tests (#31852)
* add messages to tests * changes to test_boolean.py * split error messages * change to test_isin.py * changes to test_boolean.py and test_indexing.py * revert changes to test_diff.py * cleanups to the code * changes to test_boolean.py and test_replace.py * change error message in test_to_dict.py
1 parent 02ac975 commit 30bb0f0

13 files changed

+90
-57
lines changed

pandas/tests/extension/decimal/test_decimal.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ class Reduce:
148148
def check_reduce(self, s, op_name, skipna):
149149

150150
if op_name in ["median", "skew", "kurt"]:
151-
with pytest.raises(NotImplementedError):
151+
msg = r"decimal does not support the .* operation"
152+
with pytest.raises(NotImplementedError, match=msg):
152153
getattr(s, op_name)(skipna=skipna)
153154

154155
else:

pandas/tests/extension/json/test_json.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,11 @@ def test_custom_asserts(self):
136136
self.assert_frame_equal(a.to_frame(), a.to_frame())
137137

138138
b = pd.Series(data.take([0, 0, 1]))
139-
with pytest.raises(AssertionError):
139+
msg = r"ExtensionArray are different"
140+
with pytest.raises(AssertionError, match=msg):
140141
self.assert_series_equal(a, b)
141142

142-
with pytest.raises(AssertionError):
143+
with pytest.raises(AssertionError, match=msg):
143144
self.assert_frame_equal(a.to_frame(), b.to_frame())
144145

145146

pandas/tests/extension/test_boolean.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
112112
# subtraction for bools raises TypeError (but not yet in 1.13)
113113
if _np_version_under1p14:
114114
pytest.skip("__sub__ does not yet raise in numpy 1.13")
115-
with pytest.raises(TypeError):
115+
msg = r"numpy boolean subtract"
116+
with pytest.raises(TypeError, match=msg):
116117
op(s, other)
117-
118118
return
119119

120120
result = op(s, other)

pandas/tests/extension/test_categorical.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ def _compare_other(self, s, data, op_name, other):
278278
assert (result == expected).all()
279279

280280
else:
281-
with pytest.raises(TypeError):
281+
msg = "Unordered Categoricals can only compare equality or not"
282+
with pytest.raises(TypeError, match=msg):
282283
op(data, other)
283284

284285

pandas/tests/frame/indexing/test_categorical.py

+26-21
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ def test_assigning_ops(self):
115115
tm.assert_frame_equal(df, exp_single_cats_value)
116116

117117
# - assign a single value not in the current categories set
118-
with pytest.raises(ValueError):
118+
msg1 = (
119+
"Cannot setitem on a Categorical with a new category, "
120+
"set the categories first"
121+
)
122+
msg2 = "Cannot set a Categorical with another, without identical categories"
123+
with pytest.raises(ValueError, match=msg1):
119124
df = orig.copy()
120125
df.iloc[2, 0] = "c"
121126

@@ -125,7 +130,7 @@ def test_assigning_ops(self):
125130
tm.assert_frame_equal(df, exp_single_row)
126131

127132
# - assign a complete row (mixed values) not in categories set
128-
with pytest.raises(ValueError):
133+
with pytest.raises(ValueError, match=msg1):
129134
df = orig.copy()
130135
df.iloc[2, :] = ["c", 2]
131136

@@ -134,7 +139,7 @@ def test_assigning_ops(self):
134139
df.iloc[2:4, :] = [["b", 2], ["b", 2]]
135140
tm.assert_frame_equal(df, exp_multi_row)
136141

137-
with pytest.raises(ValueError):
142+
with pytest.raises(ValueError, match=msg1):
138143
df = orig.copy()
139144
df.iloc[2:4, :] = [["c", 2], ["c", 2]]
140145

@@ -144,12 +149,12 @@ def test_assigning_ops(self):
144149
df.iloc[2:4, 0] = Categorical(["b", "b"], categories=["a", "b"])
145150
tm.assert_frame_equal(df, exp_parts_cats_col)
146151

147-
with pytest.raises(ValueError):
152+
with pytest.raises(ValueError, match=msg2):
148153
# different categories -> not sure if this should fail or pass
149154
df = orig.copy()
150155
df.iloc[2:4, 0] = Categorical(list("bb"), categories=list("abc"))
151156

152-
with pytest.raises(ValueError):
157+
with pytest.raises(ValueError, match=msg2):
153158
# different values
154159
df = orig.copy()
155160
df.iloc[2:4, 0] = Categorical(list("cc"), categories=list("abc"))
@@ -160,7 +165,7 @@ def test_assigning_ops(self):
160165
df.iloc[2:4, 0] = ["b", "b"]
161166
tm.assert_frame_equal(df, exp_parts_cats_col)
162167

163-
with pytest.raises(ValueError):
168+
with pytest.raises(ValueError, match=msg1):
164169
df.iloc[2:4, 0] = ["c", "c"]
165170

166171
# loc
@@ -175,7 +180,7 @@ def test_assigning_ops(self):
175180
tm.assert_frame_equal(df, exp_single_cats_value)
176181

177182
# - assign a single value not in the current categories set
178-
with pytest.raises(ValueError):
183+
with pytest.raises(ValueError, match=msg1):
179184
df = orig.copy()
180185
df.loc["j", "cats"] = "c"
181186

@@ -185,7 +190,7 @@ def test_assigning_ops(self):
185190
tm.assert_frame_equal(df, exp_single_row)
186191

187192
# - assign a complete row (mixed values) not in categories set
188-
with pytest.raises(ValueError):
193+
with pytest.raises(ValueError, match=msg1):
189194
df = orig.copy()
190195
df.loc["j", :] = ["c", 2]
191196

@@ -194,7 +199,7 @@ def test_assigning_ops(self):
194199
df.loc["j":"k", :] = [["b", 2], ["b", 2]]
195200
tm.assert_frame_equal(df, exp_multi_row)
196201

197-
with pytest.raises(ValueError):
202+
with pytest.raises(ValueError, match=msg1):
198203
df = orig.copy()
199204
df.loc["j":"k", :] = [["c", 2], ["c", 2]]
200205

@@ -204,14 +209,14 @@ def test_assigning_ops(self):
204209
df.loc["j":"k", "cats"] = Categorical(["b", "b"], categories=["a", "b"])
205210
tm.assert_frame_equal(df, exp_parts_cats_col)
206211

207-
with pytest.raises(ValueError):
212+
with pytest.raises(ValueError, match=msg2):
208213
# different categories -> not sure if this should fail or pass
209214
df = orig.copy()
210215
df.loc["j":"k", "cats"] = Categorical(
211216
["b", "b"], categories=["a", "b", "c"]
212217
)
213218

214-
with pytest.raises(ValueError):
219+
with pytest.raises(ValueError, match=msg2):
215220
# different values
216221
df = orig.copy()
217222
df.loc["j":"k", "cats"] = Categorical(
@@ -224,7 +229,7 @@ def test_assigning_ops(self):
224229
df.loc["j":"k", "cats"] = ["b", "b"]
225230
tm.assert_frame_equal(df, exp_parts_cats_col)
226231

227-
with pytest.raises(ValueError):
232+
with pytest.raises(ValueError, match=msg1):
228233
df.loc["j":"k", "cats"] = ["c", "c"]
229234

230235
# loc
@@ -239,7 +244,7 @@ def test_assigning_ops(self):
239244
tm.assert_frame_equal(df, exp_single_cats_value)
240245

241246
# - assign a single value not in the current categories set
242-
with pytest.raises(ValueError):
247+
with pytest.raises(ValueError, match=msg1):
243248
df = orig.copy()
244249
df.loc["j", df.columns[0]] = "c"
245250

@@ -249,7 +254,7 @@ def test_assigning_ops(self):
249254
tm.assert_frame_equal(df, exp_single_row)
250255

251256
# - assign a complete row (mixed values) not in categories set
252-
with pytest.raises(ValueError):
257+
with pytest.raises(ValueError, match=msg1):
253258
df = orig.copy()
254259
df.loc["j", :] = ["c", 2]
255260

@@ -258,7 +263,7 @@ def test_assigning_ops(self):
258263
df.loc["j":"k", :] = [["b", 2], ["b", 2]]
259264
tm.assert_frame_equal(df, exp_multi_row)
260265

261-
with pytest.raises(ValueError):
266+
with pytest.raises(ValueError, match=msg1):
262267
df = orig.copy()
263268
df.loc["j":"k", :] = [["c", 2], ["c", 2]]
264269

@@ -268,14 +273,14 @@ def test_assigning_ops(self):
268273
df.loc["j":"k", df.columns[0]] = Categorical(["b", "b"], categories=["a", "b"])
269274
tm.assert_frame_equal(df, exp_parts_cats_col)
270275

271-
with pytest.raises(ValueError):
276+
with pytest.raises(ValueError, match=msg2):
272277
# different categories -> not sure if this should fail or pass
273278
df = orig.copy()
274279
df.loc["j":"k", df.columns[0]] = Categorical(
275280
["b", "b"], categories=["a", "b", "c"]
276281
)
277282

278-
with pytest.raises(ValueError):
283+
with pytest.raises(ValueError, match=msg2):
279284
# different values
280285
df = orig.copy()
281286
df.loc["j":"k", df.columns[0]] = Categorical(
@@ -288,7 +293,7 @@ def test_assigning_ops(self):
288293
df.loc["j":"k", df.columns[0]] = ["b", "b"]
289294
tm.assert_frame_equal(df, exp_parts_cats_col)
290295

291-
with pytest.raises(ValueError):
296+
with pytest.raises(ValueError, match=msg1):
292297
df.loc["j":"k", df.columns[0]] = ["c", "c"]
293298

294299
# iat
@@ -297,7 +302,7 @@ def test_assigning_ops(self):
297302
tm.assert_frame_equal(df, exp_single_cats_value)
298303

299304
# - assign a single value not in the current categories set
300-
with pytest.raises(ValueError):
305+
with pytest.raises(ValueError, match=msg1):
301306
df = orig.copy()
302307
df.iat[2, 0] = "c"
303308

@@ -308,7 +313,7 @@ def test_assigning_ops(self):
308313
tm.assert_frame_equal(df, exp_single_cats_value)
309314

310315
# - assign a single value not in the current categories set
311-
with pytest.raises(ValueError):
316+
with pytest.raises(ValueError, match=msg1):
312317
df = orig.copy()
313318
df.at["j", "cats"] = "c"
314319

@@ -332,7 +337,7 @@ def test_assigning_ops(self):
332337
df.at["j", "cats"] = "b"
333338
tm.assert_frame_equal(df, exp_single_cats_value)
334339

335-
with pytest.raises(ValueError):
340+
with pytest.raises(ValueError, match=msg1):
336341
df = orig.copy()
337342
df.at["j", "cats"] = "c"
338343

pandas/tests/frame/indexing/test_indexing.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ def test_setitem(self, float_frame):
481481
# so raise/warn
482482
smaller = float_frame[:2]
483483

484-
with pytest.raises(com.SettingWithCopyError):
484+
msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame"
485+
with pytest.raises(com.SettingWithCopyError, match=msg):
485486
smaller["col10"] = ["1", "2"]
486487

487488
assert smaller["col10"].dtype == np.object_
@@ -865,7 +866,8 @@ def test_fancy_getitem_slice_mixed(self, float_frame, float_string_frame):
865866
# setting it triggers setting with copy
866867
sliced = float_frame.iloc[:, -3:]
867868

868-
with pytest.raises(com.SettingWithCopyError):
869+
msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame"
870+
with pytest.raises(com.SettingWithCopyError, match=msg):
869871
sliced["C"] = 4.0
870872

871873
assert (float_frame["C"] == 4).all()
@@ -992,7 +994,7 @@ def test_getitem_setitem_fancy_exceptions(self, float_frame):
992994
with pytest.raises(IndexingError, match="Too many indexers"):
993995
ix[:, :, :]
994996

995-
with pytest.raises(IndexingError):
997+
with pytest.raises(IndexingError, match="Too many indexers"):
996998
ix[:, :, :] = 1
997999

9981000
def test_getitem_setitem_boolean_misaligned(self, float_frame):
@@ -1071,10 +1073,10 @@ def test_getitem_setitem_float_labels(self):
10711073

10721074
cp = df.copy()
10731075

1074-
with pytest.raises(TypeError):
1076+
with pytest.raises(TypeError, match=msg):
10751077
cp.iloc[1.0:5] = 0
10761078

1077-
with pytest.raises(TypeError):
1079+
with pytest.raises(TypeError, match=msg):
10781080
result = cp.iloc[1.0:5] == 0 # noqa
10791081

10801082
assert result.values.all()
@@ -1470,7 +1472,8 @@ def test_iloc_row(self):
14701472

14711473
# verify slice is view
14721474
# setting it makes it raise/warn
1473-
with pytest.raises(com.SettingWithCopyError):
1475+
msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame"
1476+
with pytest.raises(com.SettingWithCopyError, match=msg):
14741477
result[2] = 0.0
14751478

14761479
exp_col = df[2].copy()
@@ -1501,7 +1504,8 @@ def test_iloc_col(self):
15011504

15021505
# verify slice is view
15031506
# and that we are setting a copy
1504-
with pytest.raises(com.SettingWithCopyError):
1507+
msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame"
1508+
with pytest.raises(com.SettingWithCopyError, match=msg):
15051509
result[8] = 0.0
15061510

15071511
assert (df[8] == 0).all()

pandas/tests/frame/indexing/test_where.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def _check_get(df, cond, check_dtypes=True):
5050
# check getting
5151
df = where_frame
5252
if df is float_string_frame:
53-
with pytest.raises(TypeError):
53+
msg = "'>' not supported between instances of 'str' and 'int'"
54+
with pytest.raises(TypeError, match=msg):
5455
df > 0
5556
return
5657
cond = df > 0
@@ -114,7 +115,8 @@ def _check_align(df, cond, other, check_dtypes=True):
114115

115116
df = where_frame
116117
if df is float_string_frame:
117-
with pytest.raises(TypeError):
118+
msg = "'>' not supported between instances of 'str' and 'int'"
119+
with pytest.raises(TypeError, match=msg):
118120
df > 0
119121
return
120122

@@ -172,7 +174,8 @@ def _check_set(df, cond, check_dtypes=True):
172174

173175
df = where_frame
174176
if df is float_string_frame:
175-
with pytest.raises(TypeError):
177+
msg = "'>' not supported between instances of 'str' and 'int'"
178+
with pytest.raises(TypeError, match=msg):
176179
df > 0
177180
return
178181

@@ -358,7 +361,8 @@ def test_where_datetime(self):
358361
)
359362

360363
stamp = datetime(2013, 1, 3)
361-
with pytest.raises(TypeError):
364+
msg = "'>' not supported between instances of 'float' and 'datetime.datetime'"
365+
with pytest.raises(TypeError, match=msg):
362366
df > stamp
363367

364368
result = df[df.iloc[:, :-1] > stamp]

pandas/tests/frame/methods/test_explode.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ def test_error():
99
df = pd.DataFrame(
1010
{"A": pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")), "B": 1}
1111
)
12-
with pytest.raises(ValueError):
12+
with pytest.raises(ValueError, match="column must be a scalar"):
1313
df.explode(list("AA"))
1414

1515
df.columns = list("AA")
16-
with pytest.raises(ValueError):
16+
with pytest.raises(ValueError, match="columns must be unique"):
1717
df.explode("A")
1818

1919

pandas/tests/frame/methods/test_isin.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ def test_isin_with_string_scalar(self):
6060
},
6161
index=["foo", "bar", "baz", "qux"],
6262
)
63-
with pytest.raises(TypeError):
63+
msg = (
64+
r"only list-like or dict-like objects are allowed "
65+
r"to be passed to DataFrame.isin\(\), you passed a 'str'"
66+
)
67+
with pytest.raises(TypeError, match=msg):
6468
df.isin("a")
6569

66-
with pytest.raises(TypeError):
70+
with pytest.raises(TypeError, match=msg):
6771
df.isin("aaa")
6872

6973
def test_isin_df(self):
@@ -92,7 +96,8 @@ def test_isin_df_dupe_values(self):
9296
df1 = DataFrame({"A": [1, 2, 3, 4], "B": [2, np.nan, 4, 4]})
9397
# just cols duped
9498
df2 = DataFrame([[0, 2], [12, 4], [2, np.nan], [4, 5]], columns=["B", "B"])
95-
with pytest.raises(ValueError):
99+
msg = r"cannot compute isin with a duplicate axis\."
100+
with pytest.raises(ValueError, match=msg):
96101
df1.isin(df2)
97102

98103
# just index duped
@@ -101,12 +106,12 @@ def test_isin_df_dupe_values(self):
101106
columns=["A", "B"],
102107
index=[0, 0, 1, 1],
103108
)
104-
with pytest.raises(ValueError):
109+
with pytest.raises(ValueError, match=msg):
105110
df1.isin(df2)
106111

107112
# cols and index:
108113
df2.columns = ["B", "B"]
109-
with pytest.raises(ValueError):
114+
with pytest.raises(ValueError, match=msg):
110115
df1.isin(df2)
111116

112117
def test_isin_dupe_self(self):

0 commit comments

Comments
 (0)