Skip to content

Commit 86f5431

Browse files
krajatclRajat Bishnoi
and
Rajat Bishnoi
authored
TST: insert 'match' to bare pytest raises in pandas/tests/indexing/test_chaining_and_caching.py (#36762)
Co-authored-by: Rajat Bishnoi <[email protected]>
1 parent 7a9307b commit 86f5431

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

pandas/tests/indexing/test_chaining_and_caching.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ def test_detect_chained_assignment(self):
155155
)
156156
assert df._is_copy is None
157157

158-
with pytest.raises(com.SettingWithCopyError):
158+
msg = "A value is trying to be set on a copy of a slice from a DataFrame"
159+
with pytest.raises(com.SettingWithCopyError, match=msg):
159160
df["A"][0] = -5
160161

161-
with pytest.raises(com.SettingWithCopyError):
162+
with pytest.raises(com.SettingWithCopyError, match=msg):
162163
df["A"][1] = np.nan
163164

164165
assert df["A"]._is_copy is None
@@ -171,7 +172,7 @@ def test_detect_chained_assignment(self):
171172
}
172173
)
173174

174-
with pytest.raises(com.SettingWithCopyError):
175+
with pytest.raises(com.SettingWithCopyError, match=msg):
175176
df.loc[0]["A"] = -5
176177

177178
# Doc example
@@ -183,17 +184,17 @@ def test_detect_chained_assignment(self):
183184
)
184185
assert df._is_copy is None
185186

186-
with pytest.raises(com.SettingWithCopyError):
187+
with pytest.raises(com.SettingWithCopyError, match=msg):
187188
indexer = df.a.str.startswith("o")
188189
df[indexer]["c"] = 42
189190

190191
expected = DataFrame({"A": [111, "bbb", "ccc"], "B": [1, 2, 3]})
191192
df = DataFrame({"A": ["aaa", "bbb", "ccc"], "B": [1, 2, 3]})
192193

193-
with pytest.raises(com.SettingWithCopyError):
194+
with pytest.raises(com.SettingWithCopyError, match=msg):
194195
df["A"][0] = 111
195196

196-
with pytest.raises(com.SettingWithCopyError):
197+
with pytest.raises(com.SettingWithCopyError, match=msg):
197198
df.loc[0]["A"] = 111
198199

199200
df.loc[0, "A"] = 111
@@ -293,7 +294,7 @@ def random_text(nobs=100):
293294
df = DataFrame(np.arange(0, 9), columns=["count"])
294295
df["group"] = "b"
295296

296-
with pytest.raises(com.SettingWithCopyError):
297+
with pytest.raises(com.SettingWithCopyError, match=msg):
297298
df.iloc[0:5]["group"] = "a"
298299

299300
# Mixed type setting but same dtype & changing dtype
@@ -306,13 +307,13 @@ def random_text(nobs=100):
306307
)
307308
)
308309

309-
with pytest.raises(com.SettingWithCopyError):
310+
with pytest.raises(com.SettingWithCopyError, match=msg):
310311
df.loc[2]["D"] = "foo"
311312

312-
with pytest.raises(com.SettingWithCopyError):
313+
with pytest.raises(com.SettingWithCopyError, match=msg):
313314
df.loc[2]["C"] = "foo"
314315

315-
with pytest.raises(com.SettingWithCopyError):
316+
with pytest.raises(com.SettingWithCopyError, match=msg):
316317
df["C"][2] = "foo"
317318

318319
def test_setting_with_copy_bug(self):
@@ -340,8 +341,10 @@ def test_detect_chained_assignment_warnings_errors(self):
340341
with option_context("chained_assignment", "warn"):
341342
with tm.assert_produces_warning(com.SettingWithCopyWarning):
342343
df.loc[0]["A"] = 111
344+
345+
msg = "A value is trying to be set on a copy of a slice from a DataFrame"
343346
with option_context("chained_assignment", "raise"):
344-
with pytest.raises(com.SettingWithCopyError):
347+
with pytest.raises(com.SettingWithCopyError, match=msg):
345348
df.loc[0]["A"] = 111
346349

347350
def test_detect_chained_assignment_warnings_filter_and_dupe_cols(self):

0 commit comments

Comments
 (0)