From 9e6ffae145c180bb44c100f1d8273f56b6217ba2 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 15 Nov 2020 18:48:49 -0800 Subject: [PATCH] REF: make casting explicit in CategoricalIndex --- pandas/core/indexes/category.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 06df8f85cded7..d2ec2bcfeb470 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -491,6 +491,7 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None): # in which case we are going to conform to the passed Categorical new_target = np.asarray(new_target) if is_categorical_dtype(target): + new_target = Categorical(new_target, dtype=target.dtype) new_target = target._shallow_copy(new_target, name=self.name) else: new_target = Index(new_target, name=self.name) @@ -514,6 +515,7 @@ def _reindex_non_unique(self, target): if not (cats == -1).any(): # .reindex returns normal Index. Revert to CategoricalIndex if # all targets are included in my categories + new_target = Categorical(new_target, dtype=self.dtype) new_target = self._shallow_copy(new_target) return new_target, indexer, new_indexer