diff --git a/pandas/tests/generic/test_duplicate_labels.py b/pandas/tests/generic/test_duplicate_labels.py index 97468e1f10a8b..42745d2a69375 100644 --- a/pandas/tests/generic/test_duplicate_labels.py +++ b/pandas/tests/generic/test_duplicate_labels.py @@ -275,7 +275,8 @@ def test_set_flags_with_duplicates(self, cls, axes): result = cls(**axes) assert result.flags.allows_duplicate_labels is True - with pytest.raises(pd.errors.DuplicateLabelError): + msg = "Index has duplicates." + with pytest.raises(pd.errors.DuplicateLabelError, match=msg): cls(**axes).set_flags(allows_duplicate_labels=False) @pytest.mark.parametrize( @@ -287,7 +288,8 @@ def test_set_flags_with_duplicates(self, cls, axes): ], ) def test_setting_allows_duplicate_labels_raises(self, data): - with pytest.raises(pd.errors.DuplicateLabelError): + msg = "Index has duplicates." + with pytest.raises(pd.errors.DuplicateLabelError, match=msg): data.flags.allows_duplicate_labels = False assert data.flags.allows_duplicate_labels is True @@ -297,7 +299,8 @@ def test_setting_allows_duplicate_labels_raises(self, data): ) def test_series_raises(self, func): s = pd.Series([0, 1], index=["a", "b"]).set_flags(allows_duplicate_labels=False) - with pytest.raises(pd.errors.DuplicateLabelError): + msg = "Index has duplicates." + with pytest.raises(pd.errors.DuplicateLabelError, match=msg): func(s) @pytest.mark.parametrize( @@ -332,7 +335,8 @@ def test_getitem_raises(self, getter, target): else: target = df - with pytest.raises(pd.errors.DuplicateLabelError): + msg = "Index has duplicates." + with pytest.raises(pd.errors.DuplicateLabelError, match=msg): getter(target) @pytest.mark.parametrize( @@ -352,7 +356,8 @@ def test_getitem_raises(self, getter, target): ], ) def test_concat_raises(self, objs, kwargs): - with pytest.raises(pd.errors.DuplicateLabelError): + msg = "Index has duplicates." + with pytest.raises(pd.errors.DuplicateLabelError, match=msg): pd.concat(objs, **kwargs) @not_implemented @@ -361,7 +366,8 @@ def test_merge_raises(self): allows_duplicate_labels=False ) b = pd.DataFrame({"B": [0, 1, 2]}, index=["a", "b", "b"]) - with pytest.raises(pd.errors.DuplicateLabelError): + msg = "Index has duplicates." + with pytest.raises(pd.errors.DuplicateLabelError, match=msg): pd.merge(a, b, left_index=True, right_index=True) @@ -381,13 +387,14 @@ def test_merge_raises(self): ids=lambda x: type(x).__name__, ) def test_raises_basic(idx): - with pytest.raises(pd.errors.DuplicateLabelError): + msg = "Index has duplicates." + with pytest.raises(pd.errors.DuplicateLabelError, match=msg): pd.Series(1, index=idx).set_flags(allows_duplicate_labels=False) - with pytest.raises(pd.errors.DuplicateLabelError): + with pytest.raises(pd.errors.DuplicateLabelError, match=msg): pd.DataFrame({"A": [1, 1]}, index=idx).set_flags(allows_duplicate_labels=False) - with pytest.raises(pd.errors.DuplicateLabelError): + with pytest.raises(pd.errors.DuplicateLabelError, match=msg): pd.DataFrame([[1, 2]], columns=idx).set_flags(allows_duplicate_labels=False) @@ -412,7 +419,8 @@ def test_format_duplicate_labels_message_multi(): def test_dataframe_insert_raises(): df = pd.DataFrame({"A": [1, 2]}).set_flags(allows_duplicate_labels=False) - with pytest.raises(ValueError, match="Cannot specify"): + msg = "Cannot specify" + with pytest.raises(ValueError, match=msg): df.insert(0, "A", [3, 4], allow_duplicates=True)