Skip to content

Commit 9262c70

Browse files
Raisa DzhamtyrovaRaisa Dzhamtyrova
authored andcommitted
add messages to tests
1 parent ebeb407 commit 9262c70

15 files changed

+92
-60
lines changed

pandas/tests/extension/decimal/test_decimal.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,10 @@ class TestMissing(BaseDecimal, base.BaseMissingTests):
146146

147147
class Reduce:
148148
def check_reduce(self, s, op_name, skipna):
149-
150149
if op_name in ["median", "skew", "kurt"]:
151-
with pytest.raises(NotImplementedError):
150+
# breakpoint()
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ 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, the \`-\` operator, is not supported.*"
116+
with pytest.raises(TypeError, match=msg):
116117
op(s, other)
117118

118119
return
@@ -139,7 +140,7 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
139140
expected[result.isna()] = np.nan
140141
self.assert_series_equal(result, expected)
141142
else:
142-
with pytest.raises(exc):
143+
with pytest.raises(exc, match=msg):
143144
op(s, other)
144145

145146
def _check_divmod_op(self, s, op, other, exc=None):

pandas/tests/extension/test_categorical.py

Lines changed: 2 additions & 1 deletion
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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ 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+
msg = (
119+
r"(:?Cannot setitem on a Categorical with a new category.*)"
120+
r"|(:?Cannot set a Categorical with another, without identical categories)"
121+
)
122+
with pytest.raises(ValueError, match=msg):
119123
df = orig.copy()
120124
df.iloc[2, 0] = "c"
121125

@@ -125,7 +129,7 @@ def test_assigning_ops(self):
125129
tm.assert_frame_equal(df, exp_single_row)
126130

127131
# - assign a complete row (mixed values) not in categories set
128-
with pytest.raises(ValueError):
132+
with pytest.raises(ValueError, match=msg):
129133
df = orig.copy()
130134
df.iloc[2, :] = ["c", 2]
131135

@@ -134,7 +138,7 @@ def test_assigning_ops(self):
134138
df.iloc[2:4, :] = [["b", 2], ["b", 2]]
135139
tm.assert_frame_equal(df, exp_multi_row)
136140

137-
with pytest.raises(ValueError):
141+
with pytest.raises(ValueError, match=msg):
138142
df = orig.copy()
139143
df.iloc[2:4, :] = [["c", 2], ["c", 2]]
140144

@@ -144,12 +148,12 @@ def test_assigning_ops(self):
144148
df.iloc[2:4, 0] = Categorical(["b", "b"], categories=["a", "b"])
145149
tm.assert_frame_equal(df, exp_parts_cats_col)
146150

147-
with pytest.raises(ValueError):
151+
with pytest.raises(ValueError, match=msg):
148152
# different categories -> not sure if this should fail or pass
149153
df = orig.copy()
150154
df.iloc[2:4, 0] = Categorical(list("bb"), categories=list("abc"))
151155

152-
with pytest.raises(ValueError):
156+
with pytest.raises(ValueError, match=msg):
153157
# different values
154158
df = orig.copy()
155159
df.iloc[2:4, 0] = Categorical(list("cc"), categories=list("abc"))
@@ -160,7 +164,7 @@ def test_assigning_ops(self):
160164
df.iloc[2:4, 0] = ["b", "b"]
161165
tm.assert_frame_equal(df, exp_parts_cats_col)
162166

163-
with pytest.raises(ValueError):
167+
with pytest.raises(ValueError, match=msg):
164168
df.iloc[2:4, 0] = ["c", "c"]
165169

166170
# loc
@@ -175,7 +179,7 @@ def test_assigning_ops(self):
175179
tm.assert_frame_equal(df, exp_single_cats_value)
176180

177181
# - assign a single value not in the current categories set
178-
with pytest.raises(ValueError):
182+
with pytest.raises(ValueError, match=msg):
179183
df = orig.copy()
180184
df.loc["j", "cats"] = "c"
181185

@@ -185,7 +189,7 @@ def test_assigning_ops(self):
185189
tm.assert_frame_equal(df, exp_single_row)
186190

187191
# - assign a complete row (mixed values) not in categories set
188-
with pytest.raises(ValueError):
192+
with pytest.raises(ValueError, match=msg):
189193
df = orig.copy()
190194
df.loc["j", :] = ["c", 2]
191195

@@ -194,7 +198,7 @@ def test_assigning_ops(self):
194198
df.loc["j":"k", :] = [["b", 2], ["b", 2]]
195199
tm.assert_frame_equal(df, exp_multi_row)
196200

197-
with pytest.raises(ValueError):
201+
with pytest.raises(ValueError, match=msg):
198202
df = orig.copy()
199203
df.loc["j":"k", :] = [["c", 2], ["c", 2]]
200204

@@ -204,14 +208,14 @@ def test_assigning_ops(self):
204208
df.loc["j":"k", "cats"] = Categorical(["b", "b"], categories=["a", "b"])
205209
tm.assert_frame_equal(df, exp_parts_cats_col)
206210

207-
with pytest.raises(ValueError):
211+
with pytest.raises(ValueError, match=msg):
208212
# different categories -> not sure if this should fail or pass
209213
df = orig.copy()
210214
df.loc["j":"k", "cats"] = Categorical(
211215
["b", "b"], categories=["a", "b", "c"]
212216
)
213217

214-
with pytest.raises(ValueError):
218+
with pytest.raises(ValueError, match=msg):
215219
# different values
216220
df = orig.copy()
217221
df.loc["j":"k", "cats"] = Categorical(
@@ -224,7 +228,7 @@ def test_assigning_ops(self):
224228
df.loc["j":"k", "cats"] = ["b", "b"]
225229
tm.assert_frame_equal(df, exp_parts_cats_col)
226230

227-
with pytest.raises(ValueError):
231+
with pytest.raises(ValueError, match=msg):
228232
df.loc["j":"k", "cats"] = ["c", "c"]
229233

230234
# loc
@@ -239,7 +243,7 @@ def test_assigning_ops(self):
239243
tm.assert_frame_equal(df, exp_single_cats_value)
240244

241245
# - assign a single value not in the current categories set
242-
with pytest.raises(ValueError):
246+
with pytest.raises(ValueError, match=msg):
243247
df = orig.copy()
244248
df.loc["j", df.columns[0]] = "c"
245249

@@ -249,7 +253,7 @@ def test_assigning_ops(self):
249253
tm.assert_frame_equal(df, exp_single_row)
250254

251255
# - assign a complete row (mixed values) not in categories set
252-
with pytest.raises(ValueError):
256+
with pytest.raises(ValueError, match=msg):
253257
df = orig.copy()
254258
df.loc["j", :] = ["c", 2]
255259

@@ -258,7 +262,7 @@ def test_assigning_ops(self):
258262
df.loc["j":"k", :] = [["b", 2], ["b", 2]]
259263
tm.assert_frame_equal(df, exp_multi_row)
260264

261-
with pytest.raises(ValueError):
265+
with pytest.raises(ValueError, match=msg):
262266
df = orig.copy()
263267
df.loc["j":"k", :] = [["c", 2], ["c", 2]]
264268

@@ -268,14 +272,14 @@ def test_assigning_ops(self):
268272
df.loc["j":"k", df.columns[0]] = Categorical(["b", "b"], categories=["a", "b"])
269273
tm.assert_frame_equal(df, exp_parts_cats_col)
270274

271-
with pytest.raises(ValueError):
275+
with pytest.raises(ValueError, match=msg):
272276
# different categories -> not sure if this should fail or pass
273277
df = orig.copy()
274278
df.loc["j":"k", df.columns[0]] = Categorical(
275279
["b", "b"], categories=["a", "b", "c"]
276280
)
277281

278-
with pytest.raises(ValueError):
282+
with pytest.raises(ValueError, match=msg):
279283
# different values
280284
df = orig.copy()
281285
df.loc["j":"k", df.columns[0]] = Categorical(
@@ -288,7 +292,7 @@ def test_assigning_ops(self):
288292
df.loc["j":"k", df.columns[0]] = ["b", "b"]
289293
tm.assert_frame_equal(df, exp_parts_cats_col)
290294

291-
with pytest.raises(ValueError):
295+
with pytest.raises(ValueError, match=msg):
292296
df.loc["j":"k", df.columns[0]] = ["c", "c"]
293297

294298
# iat
@@ -297,7 +301,7 @@ def test_assigning_ops(self):
297301
tm.assert_frame_equal(df, exp_single_cats_value)
298302

299303
# - assign a single value not in the current categories set
300-
with pytest.raises(ValueError):
304+
with pytest.raises(ValueError, match=msg):
301305
df = orig.copy()
302306
df.iat[2, 0] = "c"
303307

@@ -308,7 +312,7 @@ def test_assigning_ops(self):
308312
tm.assert_frame_equal(df, exp_single_cats_value)
309313

310314
# - assign a single value not in the current categories set
311-
with pytest.raises(ValueError):
315+
with pytest.raises(ValueError, match=msg):
312316
df = orig.copy()
313317
df.at["j", "cats"] = "c"
314318

@@ -332,7 +336,7 @@ def test_assigning_ops(self):
332336
df.at["j", "cats"] = "b"
333337
tm.assert_frame_equal(df, exp_single_cats_value)
334338

335-
with pytest.raises(ValueError):
339+
with pytest.raises(ValueError, match=msg):
336340
df = orig.copy()
337341
df.at["j", "cats"] = "c"
338342

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 11 additions & 7 deletions
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

Lines changed: 8 additions & 4 deletions
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_diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_diff_datetime_axis1(self, tz):
7474
)
7575
tm.assert_frame_equal(result, expected)
7676
else:
77-
with pytest.raises(NotImplementedError):
77+
with pytest.raises(NotImplementedError, match=""):
7878
result = df.diff(axis=1)
7979

8080
def test_diff_timedelta(self):

pandas/tests/frame/methods/test_explode.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ 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+
msg = r"(:?columns must be unique)|(:?column must be a scalar)"
13+
with pytest.raises(ValueError, match=msg):
1314
df.explode(list("AA"))
1415

1516
df.columns = list("AA")
16-
with pytest.raises(ValueError):
17+
with pytest.raises(ValueError, match=msg):
1718
df.explode("A")
1819

1920

0 commit comments

Comments
 (0)