diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst index dc66303a44f53..862aef8ceab34 100644 --- a/doc/source/user_guide/indexing.rst +++ b/doc/source/user_guide/indexing.rst @@ -701,7 +701,7 @@ Having a duplicated index will raise for a ``.reindex()``: .. code-block:: ipython In [17]: s.reindex(labels) - ValueError: cannot reindex from a duplicate axis + ValueError: cannot reindex on an axis with duplicate labels Generally, you can intersect the desired labels with the current axis, and then reindex. @@ -717,7 +717,7 @@ However, this would *still* raise if your resulting index is duplicated. In [41]: labels = ['a', 'd'] In [42]: s.loc[s.index.intersection(labels)].reindex(labels) - ValueError: cannot reindex from a duplicate axis + ValueError: cannot reindex on an axis with duplicate labels .. _indexing.basics.partial_setting: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 4de95079f6480..ec58a0487c5f6 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3715,7 +3715,7 @@ def _validate_can_reindex(self, indexer: np.ndarray) -> None: """ # trying to reindex on an axis with duplicates if not self._index_as_unique and len(indexer): - raise ValueError("cannot reindex from a duplicate axis") + raise ValueError("cannot reindex on an axis with duplicate labels") def reindex( self, target, method=None, level=None, limit=None, tolerance=None diff --git a/pandas/tests/frame/indexing/test_getitem.py b/pandas/tests/frame/indexing/test_getitem.py index 073e7b0357124..71e8f84b4ad01 100644 --- a/pandas/tests/frame/indexing/test_getitem.py +++ b/pandas/tests/frame/indexing/test_getitem.py @@ -299,7 +299,7 @@ def test_getitem_boolean_frame_unaligned_with_duplicate_columns(self, df_dup_col # boolean with the duplicate raises df = df_dup_cols - msg = "cannot reindex from a duplicate axis" + msg = "cannot reindex on an axis with duplicate labels" with pytest.raises(ValueError, match=msg): df[df.A > 6] diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index 62d7535159f13..25682330fe19a 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -68,7 +68,7 @@ def test_setitem_error_msmgs(self): index=Index(["a", "b", "c", "a"], name="foo"), name="fiz", ) - msg = "cannot reindex from a duplicate axis" + msg = "cannot reindex on an axis with duplicate labels" with pytest.raises(ValueError, match=msg): df["newcol"] = ser diff --git a/pandas/tests/frame/methods/test_reindex.py b/pandas/tests/frame/methods/test_reindex.py index 84992982a104a..d0765084adfa9 100644 --- a/pandas/tests/frame/methods/test_reindex.py +++ b/pandas/tests/frame/methods/test_reindex.py @@ -658,7 +658,7 @@ def test_reindex_dups(self): tm.assert_frame_equal(result, expected) # reindex fails - msg = "cannot reindex from a duplicate axis" + msg = "cannot reindex on an axis with duplicate labels" with pytest.raises(ValueError, match=msg): df.reindex(index=list(range(len(df)))) @@ -668,7 +668,7 @@ def test_reindex_with_duplicate_columns(self): df = DataFrame( [[1, 5, 7.0], [1, 5, 7.0], [1, 5, 7.0]], columns=["bar", "a", "a"] ) - msg = "cannot reindex from a duplicate axis" + msg = "cannot reindex on an axis with duplicate labels" with pytest.raises(ValueError, match=msg): df.reindex(columns=["bar"]) with pytest.raises(ValueError, match=msg): @@ -942,7 +942,7 @@ def test_reindex_with_categoricalindex(self): index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cabe")), name="B"), ) # passed duplicate indexers are not allowed - msg = "cannot reindex from a duplicate axis" + msg = "cannot reindex on an axis with duplicate labels" with pytest.raises(ValueError, match=msg): df2.reindex(["a", "b"]) diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index 5594659fb4b03..318289a51f781 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -692,7 +692,7 @@ def test_asfreq_non_unique(): rng2 = rng.repeat(2).values ts = Series(np.random.randn(len(rng2)), index=rng2) - msg = "cannot reindex from a duplicate axis" + msg = "cannot reindex on an axis with duplicate labels" with pytest.raises(ValueError, match=msg): ts.asfreq("B")