diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index da8327f64e26f..a9d219504e809 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -609,7 +609,8 @@ def test_bins_unequal_len(): bins = pd.cut(series.dropna().values, 4) # len(bins) != len(series) here - with pytest.raises(ValueError): + msg = r"Length of grouper \(8\) and axis \(10\) must be same length" + with pytest.raises(ValueError, match=msg): series.groupby(bins).mean() diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 964cf320a422b..fd23e95106ab0 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -349,7 +349,8 @@ def test_take(self, indices): if not isinstance(indices, (DatetimeIndex, PeriodIndex, TimedeltaIndex)): # GH 10791 - with pytest.raises(AttributeError): + msg = r"'(.*Index)' object has no attribute 'freq'" + with pytest.raises(AttributeError, match=msg): indices.freq def test_take_invalid_kwargs(self): @@ -537,9 +538,10 @@ def test_delete_base(self, indices): assert result.equals(expected) assert result.name == expected.name - with pytest.raises((IndexError, ValueError)): - # either depending on numpy version - indices.delete(len(indices)) + length = len(indices) + msg = f"index {length} is out of bounds for axis 0 with size {length}" + with pytest.raises(IndexError, match=msg): + indices.delete(length) def test_equals(self, indices): if isinstance(indices, IntervalIndex): @@ -787,13 +789,14 @@ def test_putmask_with_wrong_mask(self): # GH18368 index = self.create_index() - with pytest.raises(ValueError): + msg = "putmask: mask and data must be the same size" + with pytest.raises(ValueError, match=msg): index.putmask(np.ones(len(index) + 1, np.bool), 1) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): index.putmask(np.ones(len(index) - 1, np.bool), 1) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): index.putmask("foo", 1) @pytest.mark.parametrize("copy", [True, False]) @@ -861,10 +864,21 @@ def test_getitem_2d_deprecated(self): def test_contains_requires_hashable_raises(self): idx = self.create_index() - with pytest.raises(TypeError, match="unhashable type"): + + msg = "unhashable type: 'list'" + with pytest.raises(TypeError, match=msg): [] in idx - with pytest.raises(TypeError): + msg = "|".join( + [ + r"unhashable type: 'dict'", + r"must be real number, not dict", + r"an integer is required", + r"\{\}", + r"pandas\._libs\.interval\.IntervalTree' is not iterable", + ] + ) + with pytest.raises(TypeError, match=msg): {} in idx._engine def test_copy_copies_cache(self): diff --git a/pandas/tests/indexes/datetimelike.py b/pandas/tests/indexes/datetimelike.py index ba10976a67e9a..85d670e9dbffa 100644 --- a/pandas/tests/indexes/datetimelike.py +++ b/pandas/tests/indexes/datetimelike.py @@ -11,14 +11,15 @@ class DatetimeLike(Base): def test_argmax_axis_invalid(self): # GH#23081 + msg = r"`axis` must be fewer than the number of dimensions \(1\)" rng = self.create_index() - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): rng.argmax(axis=1) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): rng.argmin(axis=2) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): rng.min(axis=-2) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): rng.max(axis=-3) def test_can_hold_identifiers(self): diff --git a/pandas/tests/indexes/multi/test_compat.py b/pandas/tests/indexes/multi/test_compat.py index 9273de9c20412..d1f66af4a8e83 100644 --- a/pandas/tests/indexes/multi/test_compat.py +++ b/pandas/tests/indexes/multi/test_compat.py @@ -53,7 +53,11 @@ def test_boolean_context_compat2(): i2 = MultiIndex.from_tuples([("A", 1), ("A", 3)]) common = i1.intersection(i2) - with pytest.raises(ValueError): + msg = ( + r"The truth value of a MultiIndex is ambiguous\. " + r"Use a\.empty, a\.bool\(\), a\.item\(\), a\.any\(\) or a\.all\(\)\." + ) + with pytest.raises(ValueError, match=msg): bool(common) diff --git a/pandas/tests/indexes/multi/test_reshape.py b/pandas/tests/indexes/multi/test_reshape.py index de32bd94be491..6d8a396119ef3 100644 --- a/pandas/tests/indexes/multi/test_reshape.py +++ b/pandas/tests/indexes/multi/test_reshape.py @@ -175,6 +175,6 @@ def test_delete_base(idx): assert result.equals(expected) assert result.name == expected.name - with pytest.raises((IndexError, ValueError)): - # Exception raised depends on NumPy version. + msg = "index 6 is out of bounds for axis 0 with size 6" + with pytest.raises(IndexError, match=msg): idx.delete(len(idx)) diff --git a/pandas/tests/indexes/multi/test_sorting.py b/pandas/tests/indexes/multi/test_sorting.py index bb40612b9a55a..423bbed831b87 100644 --- a/pandas/tests/indexes/multi/test_sorting.py +++ b/pandas/tests/indexes/multi/test_sorting.py @@ -105,7 +105,11 @@ def test_unsortedindex(): expected = df.iloc[0] tm.assert_series_equal(result, expected) - with pytest.raises(UnsortedIndexError): + msg = ( + "MultiIndex slicing requires the index to be lexsorted: " + r"slicing on levels \[1\], lexsort depth 0" + ) + with pytest.raises(UnsortedIndexError, match=msg): df.loc(axis=0)["z", slice("a")] df.sort_index(inplace=True) assert len(df.loc(axis=0)["z", :]) == 2 @@ -124,7 +128,8 @@ def test_unsortedindex_doc_examples(): with tm.assert_produces_warning(PerformanceWarning): dfm.loc[(1, "z")] - with pytest.raises(UnsortedIndexError): + msg = r"Key length \(2\) was greater than MultiIndex lexsort depth \(1\)" + with pytest.raises(UnsortedIndexError, match=msg): dfm.loc[(0, "y"):(1, "z")] assert not dfm.index.is_lexsorted()