Skip to content

ENH: Clarify error message when reindexing fails (#42000) #42007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/source/user_guide/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/frame/methods/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))))

Expand All @@ -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):
Expand Down Expand Up @@ -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"])

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down