Skip to content

Commit 2a679dc

Browse files
jbrockmendelnoatamir
authored andcommitted
DEPR: Index.reindex with non-unique Index (pandas-dev#49485)
1 parent dc4eed2 commit 2a679dc

File tree

5 files changed

+17
-35
lines changed

5 files changed

+17
-35
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ Removal of prior version deprecations/changes
250250
- Disallow passing non-keyword arguments to :meth:`DataFrame.where` and :meth:`Series.where` except for ``cond`` and ``other`` (:issue:`41523`)
251251
- Disallow passing non-keyword arguments to :meth:`Series.set_axis` and :meth:`DataFrame.set_axis` except for ``labels`` (:issue:`41491`)
252252
- Disallow passing non-keyword arguments to :meth:`Series.rename_axis` and :meth:`DataFrame.rename_axis` except for ``mapper`` (:issue:`47587`)
253+
- Disallow :meth:`Index.reindex` with non-unique :class:`Index` objects (:issue:`42568`)
253254
- Disallow passing non-keyword arguments to :meth:`Series.clip` and :meth:`DataFrame.clip` (:issue:`41511`)
254255
- Disallow passing non-keyword arguments to :meth:`Series.bfill`, :meth:`Series.ffill`, :meth:`DataFrame.bfill` and :meth:`DataFrame.ffill` (:issue:`41508`)
255256
- Disallow passing non-keyword arguments to :meth:`DataFrame.replace`, :meth:`Series.replace` except for ``to_replace`` and ``value`` (:issue:`47587`)

pandas/core/indexes/base.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -4339,12 +4339,7 @@ def reindex(
43394339

43404340
if not self.is_unique:
43414341
# GH#42568
4342-
warnings.warn(
4343-
"reindexing with a non-unique Index is deprecated and "
4344-
"will raise in a future version.",
4345-
FutureWarning,
4346-
stacklevel=find_stack_level(),
4347-
)
4342+
raise ValueError("cannot reindex on an axis with duplicate labels")
43484343

43494344
target = self._wrap_reindex_result(target, indexer, preserve_names)
43504345
return target, indexer

pandas/core/indexes/category.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,7 @@ def reindex(
414414
indexer, missing = self.get_indexer_non_unique(target)
415415
if not self.is_unique:
416416
# GH#42568
417-
warnings.warn(
418-
"reindexing with a non-unique Index is deprecated and will "
419-
"raise in a future version.",
420-
FutureWarning,
421-
stacklevel=find_stack_level(),
422-
)
417+
raise ValueError("cannot reindex on an axis with duplicate labels")
423418

424419
new_target: Index
425420
if len(self) and indexer is not None:

pandas/tests/indexes/categorical/test_reindex.py

+13-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import pytest
23

34
from pandas import (
45
Categorical,
@@ -12,37 +13,28 @@
1213
class TestReindex:
1314
def test_reindex_list_non_unique(self):
1415
# GH#11586
16+
msg = "cannot reindex on an axis with duplicate labels"
1517
ci = CategoricalIndex(["a", "b", "c", "a"])
16-
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
17-
res, indexer = ci.reindex(["a", "c"])
18-
19-
tm.assert_index_equal(res, Index(["a", "a", "c"]), exact=True)
20-
tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2], dtype=np.intp))
18+
with pytest.raises(ValueError, match=msg):
19+
ci.reindex(["a", "c"])
2120

2221
def test_reindex_categorical_non_unique(self):
22+
msg = "cannot reindex on an axis with duplicate labels"
2323
ci = CategoricalIndex(["a", "b", "c", "a"])
24-
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
25-
res, indexer = ci.reindex(Categorical(["a", "c"]))
26-
27-
exp = CategoricalIndex(["a", "a", "c"], categories=["a", "c"])
28-
tm.assert_index_equal(res, exp, exact=True)
29-
tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2], dtype=np.intp))
24+
with pytest.raises(ValueError, match=msg):
25+
ci.reindex(Categorical(["a", "c"]))
3026

3127
def test_reindex_list_non_unique_unused_category(self):
28+
msg = "cannot reindex on an axis with duplicate labels"
3229
ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
33-
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
34-
res, indexer = ci.reindex(["a", "c"])
35-
exp = Index(["a", "a", "c"], dtype="object")
36-
tm.assert_index_equal(res, exp, exact=True)
37-
tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2], dtype=np.intp))
30+
with pytest.raises(ValueError, match=msg):
31+
ci.reindex(["a", "c"])
3832

3933
def test_reindex_categorical_non_unique_unused_category(self):
34+
msg = "cannot reindex on an axis with duplicate labels"
4035
ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
41-
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
42-
res, indexer = ci.reindex(Categorical(["a", "c"]))
43-
exp = CategoricalIndex(["a", "a", "c"], categories=["a", "c"])
44-
tm.assert_index_equal(res, exp, exact=True)
45-
tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2], dtype=np.intp))
36+
with pytest.raises(ValueError, match=msg):
37+
ci.reindex(Categorical(["a", "c"]))
4638

4739
def test_reindex_duplicate_target(self):
4840
# See GH25459

pandas/tests/resample/test_datetime_index.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,7 @@ def test_asfreq_non_unique():
700700

701701
msg = "cannot reindex on an axis with duplicate labels"
702702
with pytest.raises(ValueError, match=msg):
703-
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
704-
ts.asfreq("B")
703+
ts.asfreq("B")
705704

706705

707706
def test_resample_axis1():

0 commit comments

Comments
 (0)