From 439e7075fe16e1081d4eff1078f125281f1c18a6 Mon Sep 17 00:00:00 2001 From: GYHHAHA <1801214626@qq.com> Date: Tue, 15 Dec 2020 13:58:27 +0800 Subject: [PATCH 1/5] `reindex` of empty CategoricalIndex sometimes fails if target is not a `CategoricalIndex` --- doc/source/whatsnew/v1.3.0.rst | 2 +- pandas/core/indexes/category.py | 2 +- pandas/tests/indexes/categorical/test_reindex.py | 11 ++++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 57dd1d05a274e..719b5b5433f9c 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -170,7 +170,7 @@ Bug fixes Categorical ^^^^^^^^^^^ -- +- Bug in ``CategoricalIndex.reindex`` failed when ``Index`` passed with elements all in category (:issue:`28690`) - Datetimelike diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index e2a7752cf3f0d..bd9224d340ec8 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -441,7 +441,7 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None): if len(missing): cats = self.categories.get_indexer(target) - if (cats == -1).any(): + if not isinstance(cats, CategoricalIndex) or (cats == -1).any(): # coerce to a regular index here! result = Index(np.array(self), name=self.name) new_target, indexer, _ = result._reindex_non_unique(np.array(target)) diff --git a/pandas/tests/indexes/categorical/test_reindex.py b/pandas/tests/indexes/categorical/test_reindex.py index 668c559abd08e..dca67e2711db3 100644 --- a/pandas/tests/indexes/categorical/test_reindex.py +++ b/pandas/tests/indexes/categorical/test_reindex.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from pandas import Categorical, CategoricalIndex, Index, Series +from pandas import Categorical, CategoricalIndex, Index, Series, DataFrame import pandas._testing as tm @@ -59,3 +59,12 @@ def test_reindex_missing_category(self): msg = "'fill_value=-1' is not present in this Categorical's categories" with pytest.raises(TypeError, match=msg): ser.reindex([1, 2, 3, 4, 5], fill_value=-1) + + def test_reindex_not_category(self): + # GH: 28690 + df = DataFrame( + index=CategoricalIndex([], categories=["A"]) + ) + result = df.reindex(index=Index(["A"])) + expected = DataFrame(index=Index(["A"])) + tm.assert_frame_equal(result, expected) From ed24f1040ad68044049412ed9ee3717657eb5b5d Mon Sep 17 00:00:00 2001 From: GYHHAHA <1801214626@qq.com> Date: Tue, 15 Dec 2020 14:08:29 +0800 Subject: [PATCH 2/5] Update test_reindex.py --- pandas/tests/indexes/categorical/test_reindex.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexes/categorical/test_reindex.py b/pandas/tests/indexes/categorical/test_reindex.py index dca67e2711db3..28c48d21e66fd 100644 --- a/pandas/tests/indexes/categorical/test_reindex.py +++ b/pandas/tests/indexes/categorical/test_reindex.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from pandas import Categorical, CategoricalIndex, Index, Series, DataFrame +from pandas import Categorical, CategoricalIndex, DataFrame, Index, Series import pandas._testing as tm @@ -62,9 +62,7 @@ def test_reindex_missing_category(self): def test_reindex_not_category(self): # GH: 28690 - df = DataFrame( - index=CategoricalIndex([], categories=["A"]) - ) + df = DataFrame(index=CategoricalIndex([], categories=["A"])) result = df.reindex(index=Index(["A"])) expected = DataFrame(index=Index(["A"])) tm.assert_frame_equal(result, expected) From 5fe6ae85ac1b3cea583ca2bedf4e54d0f637c515 Mon Sep 17 00:00:00 2001 From: GYHHAHA <1801214626@qq.com> Date: Wed, 16 Dec 2020 13:12:48 +0800 Subject: [PATCH 3/5] Update test_reindex.py --- .../tests/indexes/categorical/test_reindex.py | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexes/categorical/test_reindex.py b/pandas/tests/indexes/categorical/test_reindex.py index 28c48d21e66fd..8228c5139ccdd 100644 --- a/pandas/tests/indexes/categorical/test_reindex.py +++ b/pandas/tests/indexes/categorical/test_reindex.py @@ -60,9 +60,34 @@ def test_reindex_missing_category(self): with pytest.raises(TypeError, match=msg): ser.reindex([1, 2, 3, 4, 5], fill_value=-1) - def test_reindex_not_category(self): + @pytest.mark.parametrize( + "index_df,index_res,index_exp", + [ + ( + CategoricalIndex([], categories=["A"]), + Index(["A"]), + Index(["A"]), + ), + ( + CategoricalIndex([], categories=["A"]), + Index(["B"]), + Index(["B"]), + ), + ( + CategoricalIndex([], categories=["A"]), + CategoricalIndex(["A"]), + CategoricalIndex(["A"]), + ), + ( + CategoricalIndex([], categories=["A"]), + CategoricalIndex(["B"]), + CategoricalIndex(["B"]), + ), + ], + ) + def test_reindex_not_category(self, index_df, index_res, index_exp): # GH: 28690 - df = DataFrame(index=CategoricalIndex([], categories=["A"])) - result = df.reindex(index=Index(["A"])) - expected = DataFrame(index=Index(["A"])) + df = DataFrame(index=index_df) + result = df.reindex(index=index_res) + expected = DataFrame(index=index_exp) tm.assert_frame_equal(result, expected) From 3f76985934401d9e7a2b22974b4285bdd924874a Mon Sep 17 00:00:00 2001 From: GYHHAHA <1801214626@qq.com> Date: Wed, 16 Dec 2020 19:52:42 +0800 Subject: [PATCH 4/5] Update v1.3.0.rst --- doc/source/whatsnew/v1.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 719b5b5433f9c..b98a5aef8bcf8 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -170,7 +170,7 @@ Bug fixes Categorical ^^^^^^^^^^^ -- Bug in ``CategoricalIndex.reindex`` failed when ``Index`` passed with elements all in category (:issue:`28690`) +- Bug in ``CategoricalIndex.reindex`` failed when ``Index`` passed with elements all in the category (:issue:`28690`) - Datetimelike From 99feb4eaf688335151ffd4677a3a2373488c4a3a Mon Sep 17 00:00:00 2001 From: GYHHAHA <1801214626@qq.com> Date: Thu, 17 Dec 2020 10:50:32 +0800 Subject: [PATCH 5/5] Update v1.3.0.rst --- doc/source/whatsnew/v1.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index b98a5aef8bcf8..719b5b5433f9c 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -170,7 +170,7 @@ Bug fixes Categorical ^^^^^^^^^^^ -- Bug in ``CategoricalIndex.reindex`` failed when ``Index`` passed with elements all in the category (:issue:`28690`) +- Bug in ``CategoricalIndex.reindex`` failed when ``Index`` passed with elements all in category (:issue:`28690`) - Datetimelike