Skip to content

Commit af1b3c1

Browse files
aberresiynehz
authored andcommitted
ENH: Clarify error message when reindexing fails (pandas-dev#42000) (pandas-dev#42007)
1 parent b44491b commit af1b3c1

File tree

6 files changed

+9
-9
lines changed

6 files changed

+9
-9
lines changed

doc/source/user_guide/indexing.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ Having a duplicated index will raise for a ``.reindex()``:
701701
.. code-block:: ipython
702702
703703
In [17]: s.reindex(labels)
704-
ValueError: cannot reindex from a duplicate axis
704+
ValueError: cannot reindex on an axis with duplicate labels
705705
706706
Generally, you can intersect the desired labels with the current
707707
axis, and then reindex.
@@ -717,7 +717,7 @@ However, this would *still* raise if your resulting index is duplicated.
717717
In [41]: labels = ['a', 'd']
718718
719719
In [42]: s.loc[s.index.intersection(labels)].reindex(labels)
720-
ValueError: cannot reindex from a duplicate axis
720+
ValueError: cannot reindex on an axis with duplicate labels
721721
722722
723723
.. _indexing.basics.partial_setting:

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3737,7 +3737,7 @@ def _validate_can_reindex(self, indexer: np.ndarray) -> None:
37373737
"""
37383738
# trying to reindex on an axis with duplicates
37393739
if not self._index_as_unique and len(indexer):
3740-
raise ValueError("cannot reindex from a duplicate axis")
3740+
raise ValueError("cannot reindex on an axis with duplicate labels")
37413741

37423742
def reindex(
37433743
self, target, method=None, level=None, limit=None, tolerance=None

pandas/tests/frame/indexing/test_getitem.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def test_getitem_boolean_frame_unaligned_with_duplicate_columns(self, df_dup_col
299299

300300
# boolean with the duplicate raises
301301
df = df_dup_cols
302-
msg = "cannot reindex from a duplicate axis"
302+
msg = "cannot reindex on an axis with duplicate labels"
303303
with pytest.raises(ValueError, match=msg):
304304
df[df.A > 6]
305305

pandas/tests/frame/indexing/test_setitem.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_setitem_error_msmgs(self):
6868
index=Index(["a", "b", "c", "a"], name="foo"),
6969
name="fiz",
7070
)
71-
msg = "cannot reindex from a duplicate axis"
71+
msg = "cannot reindex on an axis with duplicate labels"
7272
with pytest.raises(ValueError, match=msg):
7373
df["newcol"] = ser
7474

pandas/tests/frame/methods/test_reindex.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ def test_reindex_dups(self):
658658
tm.assert_frame_equal(result, expected)
659659

660660
# reindex fails
661-
msg = "cannot reindex from a duplicate axis"
661+
msg = "cannot reindex on an axis with duplicate labels"
662662
with pytest.raises(ValueError, match=msg):
663663
df.reindex(index=list(range(len(df))))
664664

@@ -668,7 +668,7 @@ def test_reindex_with_duplicate_columns(self):
668668
df = DataFrame(
669669
[[1, 5, 7.0], [1, 5, 7.0], [1, 5, 7.0]], columns=["bar", "a", "a"]
670670
)
671-
msg = "cannot reindex from a duplicate axis"
671+
msg = "cannot reindex on an axis with duplicate labels"
672672
with pytest.raises(ValueError, match=msg):
673673
df.reindex(columns=["bar"])
674674
with pytest.raises(ValueError, match=msg):
@@ -942,7 +942,7 @@ def test_reindex_with_categoricalindex(self):
942942
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cabe")), name="B"),
943943
)
944944
# passed duplicate indexers are not allowed
945-
msg = "cannot reindex from a duplicate axis"
945+
msg = "cannot reindex on an axis with duplicate labels"
946946
with pytest.raises(ValueError, match=msg):
947947
df2.reindex(["a", "b"])
948948

pandas/tests/resample/test_datetime_index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ def test_asfreq_non_unique():
692692
rng2 = rng.repeat(2).values
693693
ts = Series(np.random.randn(len(rng2)), index=rng2)
694694

695-
msg = "cannot reindex from a duplicate axis"
695+
msg = "cannot reindex on an axis with duplicate labels"
696696
with pytest.raises(ValueError, match=msg):
697697
ts.asfreq("B")
698698

0 commit comments

Comments
 (0)