diff --git a/pandas/tests/arrays/boolean/test_function.py b/pandas/tests/arrays/boolean/test_function.py index c2987dc37b960..49a832f8dda20 100644 --- a/pandas/tests/arrays/boolean/test_function.py +++ b/pandas/tests/arrays/boolean/test_function.py @@ -54,7 +54,8 @@ def test_ufuncs_binary(ufunc): tm.assert_extension_array_equal(result, expected) # not handled types - with pytest.raises(TypeError): + msg = r"operand type\(s\) all returned NotImplemented from __array_ufunc__" + with pytest.raises(TypeError, match=msg): ufunc(a, "test") @@ -76,7 +77,8 @@ def test_ufuncs_unary(ufunc): @pytest.mark.parametrize("values", [[True, False], [True, None]]) def test_ufunc_reduce_raises(values): a = pd.array(values, dtype="boolean") - with pytest.raises(NotImplementedError): + msg = "The 'reduce' method is not supported" + with pytest.raises(NotImplementedError, match=msg): np.add.reduce(a) diff --git a/pandas/tests/indexes/ranges/test_constructors.py b/pandas/tests/indexes/ranges/test_constructors.py index b7f673428ae38..f573da44e99b3 100644 --- a/pandas/tests/indexes/ranges/test_constructors.py +++ b/pandas/tests/indexes/ranges/test_constructors.py @@ -149,9 +149,9 @@ def test_constructor_corner(self): tm.assert_index_equal(index, Index(arr)) # non-int raise Exception - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=r"Wrong type \"): RangeIndex("1", "10", "1") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=r"Wrong type \"): RangeIndex(1.1, 10.2, 1.3) # invalid passed type diff --git a/pandas/tests/indexes/ranges/test_range.py b/pandas/tests/indexes/ranges/test_range.py index 05422e7b4419f..2438cd352f86f 100644 --- a/pandas/tests/indexes/ranges/test_range.py +++ b/pandas/tests/indexes/ranges/test_range.py @@ -117,7 +117,8 @@ def test_delete(self): tm.assert_index_equal(result, expected) assert result.name == expected.name - with pytest.raises((IndexError, ValueError)): + msg = "index 5 is out of bounds for axis 0 with size 5" + with pytest.raises((IndexError, ValueError), match=msg): # either depending on numpy version result = idx.delete(len(idx)) diff --git a/pandas/tests/indexing/multiindex/test_chaining_and_caching.py b/pandas/tests/indexing/multiindex/test_chaining_and_caching.py index 8bfba8c12e934..d3b13336e2a44 100644 --- a/pandas/tests/indexing/multiindex/test_chaining_and_caching.py +++ b/pandas/tests/indexing/multiindex/test_chaining_and_caching.py @@ -23,7 +23,8 @@ def test_detect_chained_assignment(): multiind = MultiIndex.from_tuples(tuples, names=["part", "side"]) zed = DataFrame(events, index=["a", "b"], columns=multiind) - with pytest.raises(com.SettingWithCopyError): + msg = "A value is trying to be set on a copy of a slice from a DataFrame" + with pytest.raises(com.SettingWithCopyError, match=msg): zed["eyes"]["right"].fillna(value=555, inplace=True) diff --git a/pandas/tests/indexing/test_partial.py b/pandas/tests/indexing/test_partial.py index 2ce07ec41758f..2e691c6fd76d8 100644 --- a/pandas/tests/indexing/test_partial.py +++ b/pandas/tests/indexing/test_partial.py @@ -43,10 +43,12 @@ def test_partial_setting(self): # iloc/iat raise s = s_orig.copy() - with pytest.raises(IndexError): + msg = "iloc cannot enlarge its target object" + with pytest.raises(IndexError, match=msg): s.iloc[3] = 5.0 - with pytest.raises(IndexError): + msg = "index 3 is out of bounds for axis 0 with size 3" + with pytest.raises(IndexError, match=msg): s.iat[3] = 5.0 # ## frame ## @@ -58,10 +60,12 @@ def test_partial_setting(self): # iloc/iat raise df = df_orig.copy() - with pytest.raises(IndexError): + msg = "iloc cannot enlarge its target object" + with pytest.raises(IndexError, match=msg): df.iloc[4, 2] = 5.0 - with pytest.raises(IndexError): + msg = "index 2 is out of bounds for axis 0 with size 2" + with pytest.raises(IndexError, match=msg): df.iat[4, 2] = 5.0 # row setting where it exists @@ -162,7 +166,8 @@ def test_partial_setting_mixed_dtype(self): # list-like must conform df = DataFrame(columns=["A", "B"]) - with pytest.raises(ValueError): + msg = "cannot set a row with mismatched columns" + with pytest.raises(ValueError, match=msg): df.loc[0] = [1, 2, 3] # TODO: #15657, these are left as object and not coerced @@ -330,10 +335,12 @@ def test_partial_set_invalid(self): df = orig.copy() # don't allow not string inserts - with pytest.raises(TypeError): + msg = "cannot insert DatetimeIndex with incompatible label" + + with pytest.raises(TypeError, match=msg): df.loc[100.0, :] = df.iloc[0] - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): df.loc[100, :] = df.iloc[0] # allow object conversion here @@ -375,13 +382,16 @@ def test_partial_set_empty_frame(self): # frame df = DataFrame() - with pytest.raises(ValueError): + msg = "cannot set a frame with no defined columns" + + with pytest.raises(ValueError, match=msg): df.loc[1] = 1 - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): df.loc[1] = Series([1], index=["foo"]) - with pytest.raises(ValueError): + msg = "cannot set a frame with no defined index and a scalar" + with pytest.raises(ValueError, match=msg): df.loc[:, 1] = 1 # these work as they don't really change diff --git a/pandas/tests/series/indexing/test_alter_index.py b/pandas/tests/series/indexing/test_alter_index.py index f2969e15fad8a..558f10d967df6 100644 --- a/pandas/tests/series/indexing/test_alter_index.py +++ b/pandas/tests/series/indexing/test_alter_index.py @@ -283,7 +283,8 @@ def test_reindex_datetimeindexes_tz_naive_and_aware(): idx = date_range("20131101", tz="America/Chicago", periods=7) newidx = date_range("20131103", periods=10, freq="H") s = Series(range(7), index=idx) - with pytest.raises(TypeError): + msg = "Cannot compare tz-naive and tz-aware timestamps" + with pytest.raises(TypeError, match=msg): s.reindex(newidx, method="ffill")