From 1d17e40dbdd0bde52db33c1ad69d4f136a1c28c3 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 2 Nov 2022 15:19:31 -0700 Subject: [PATCH] DEPR: Index.reindex with non-unique Indx --- doc/source/whatsnew/v2.0.0.rst | 1 + pandas/core/indexes/base.py | 7 +--- pandas/core/indexes/category.py | 7 +--- .../tests/indexes/categorical/test_reindex.py | 34 +++++++------------ pandas/tests/resample/test_datetime_index.py | 3 +- 5 files changed, 17 insertions(+), 35 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index d71160cdbc369..ac3a1ca95fbb4 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -250,6 +250,7 @@ Removal of prior version deprecations/changes - Disallow passing non-keyword arguments to :meth:`DataFrame.where` and :meth:`Series.where` except for ``cond`` and ``other`` (:issue:`41523`) - Disallow passing non-keyword arguments to :meth:`Series.set_axis` and :meth:`DataFrame.set_axis` except for ``labels`` (:issue:`41491`) - Disallow passing non-keyword arguments to :meth:`Series.rename_axis` and :meth:`DataFrame.rename_axis` except for ``mapper`` (:issue:`47587`) +- Disallow :meth:`Index.reindex` with non-unique :class:`Index` objects (:issue:`42568`) - Disallow passing non-keyword arguments to :meth:`Series.clip` and :meth:`DataFrame.clip` (:issue:`41511`) - Disallow passing non-keyword arguments to :meth:`Series.bfill`, :meth:`Series.ffill`, :meth:`DataFrame.bfill` and :meth:`DataFrame.ffill` (:issue:`41508`) - Disallow passing non-keyword arguments to :meth:`DataFrame.replace`, :meth:`Series.replace` except for ``to_replace`` and ``value`` (:issue:`47587`) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index b2ea46ce1399e..b5854c77d3709 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4339,12 +4339,7 @@ def reindex( if not self.is_unique: # GH#42568 - warnings.warn( - "reindexing with a non-unique Index is deprecated and " - "will raise in a future version.", - FutureWarning, - stacklevel=find_stack_level(), - ) + raise ValueError("cannot reindex on an axis with duplicate labels") target = self._wrap_reindex_result(target, indexer, preserve_names) return target, indexer diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 58b533cb576d9..e435dfbc6ab40 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -414,12 +414,7 @@ def reindex( indexer, missing = self.get_indexer_non_unique(target) if not self.is_unique: # GH#42568 - warnings.warn( - "reindexing with a non-unique Index is deprecated and will " - "raise in a future version.", - FutureWarning, - stacklevel=find_stack_level(), - ) + raise ValueError("cannot reindex on an axis with duplicate labels") new_target: Index if len(self) and indexer is not None: diff --git a/pandas/tests/indexes/categorical/test_reindex.py b/pandas/tests/indexes/categorical/test_reindex.py index 1337eff1f1c2f..8ca5c6099b4e7 100644 --- a/pandas/tests/indexes/categorical/test_reindex.py +++ b/pandas/tests/indexes/categorical/test_reindex.py @@ -1,4 +1,5 @@ import numpy as np +import pytest from pandas import ( Categorical, @@ -12,37 +13,28 @@ class TestReindex: def test_reindex_list_non_unique(self): # GH#11586 + msg = "cannot reindex on an axis with duplicate labels" ci = CategoricalIndex(["a", "b", "c", "a"]) - with tm.assert_produces_warning(FutureWarning, match="non-unique"): - res, indexer = ci.reindex(["a", "c"]) - - tm.assert_index_equal(res, Index(["a", "a", "c"]), exact=True) - tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2], dtype=np.intp)) + with pytest.raises(ValueError, match=msg): + ci.reindex(["a", "c"]) def test_reindex_categorical_non_unique(self): + msg = "cannot reindex on an axis with duplicate labels" ci = CategoricalIndex(["a", "b", "c", "a"]) - with tm.assert_produces_warning(FutureWarning, match="non-unique"): - res, indexer = ci.reindex(Categorical(["a", "c"])) - - exp = CategoricalIndex(["a", "a", "c"], categories=["a", "c"]) - tm.assert_index_equal(res, exp, exact=True) - tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2], dtype=np.intp)) + with pytest.raises(ValueError, match=msg): + ci.reindex(Categorical(["a", "c"])) def test_reindex_list_non_unique_unused_category(self): + msg = "cannot reindex on an axis with duplicate labels" ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"]) - with tm.assert_produces_warning(FutureWarning, match="non-unique"): - res, indexer = ci.reindex(["a", "c"]) - exp = Index(["a", "a", "c"], dtype="object") - tm.assert_index_equal(res, exp, exact=True) - tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2], dtype=np.intp)) + with pytest.raises(ValueError, match=msg): + ci.reindex(["a", "c"]) def test_reindex_categorical_non_unique_unused_category(self): + msg = "cannot reindex on an axis with duplicate labels" ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"]) - with tm.assert_produces_warning(FutureWarning, match="non-unique"): - res, indexer = ci.reindex(Categorical(["a", "c"])) - exp = CategoricalIndex(["a", "a", "c"], categories=["a", "c"]) - tm.assert_index_equal(res, exp, exact=True) - tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2], dtype=np.intp)) + with pytest.raises(ValueError, match=msg): + ci.reindex(Categorical(["a", "c"])) def test_reindex_duplicate_target(self): # See GH25459 diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index 769fd2d4a05eb..0c264c107d3d6 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -700,8 +700,7 @@ def test_asfreq_non_unique(): msg = "cannot reindex on an axis with duplicate labels" with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(FutureWarning, match="non-unique"): - ts.asfreq("B") + ts.asfreq("B") def test_resample_axis1():