From cb2b14c10fa8d2af3d0d08786d86cf2edadb4c7a Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 21 Feb 2020 16:17:41 -0800 Subject: [PATCH 1/4] BUG: Index(categorical, dtype=object) not returning object dtype --- pandas/core/indexes/base.py | 3 +++ pandas/tests/indexes/test_base.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 14ee21ea5614c..1d8d7049ada65 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -304,6 +304,9 @@ def __new__( # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 from pandas.core.indexes.category import CategoricalIndex + if is_dtype_equal(_o_dtype, dtype): + ci = CategoricalIndex(data, copy=False, name=name, **kwargs) + return ci.astype(object) return CategoricalIndex(data, dtype=dtype, copy=copy, name=name, **kwargs) # interval diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 3b4b6b09dcda5..912c85a2acbd1 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -387,6 +387,12 @@ def test_constructor_dtypes_to_object(self, cast_index, vals): assert isinstance(index, Index) assert index.dtype == object + def test_constructor_categorical_to_object(self): + # Categorical data and dtype=object should return object-dtype + ci = CategoricalIndex(range(5)) + result = Index(ci, dtype=object) + assert not isinstance(result, CategoricalIndex) + @pytest.mark.parametrize( "vals", [ From 45537e6e70accf9b86e3d2aaab0fcf83209016ca Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 21 Feb 2020 17:19:06 -0800 Subject: [PATCH 2/4] whatsnew --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 0e2b15974dbbd..134ce393c1e61 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -101,7 +101,7 @@ Bug fixes Categorical ^^^^^^^^^^^ - +- Bug when passing categorical data to :class:`Index` constructor along with ``dtype=object`` incorrectly returning a :class:`CategoricalIndex` instead of object-dtype :class:`Index` (:issue:`32167`) - - From 28b6561b06b65d1c5d63decfcc182194b8e85afb Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 22 Feb 2020 07:29:19 -0800 Subject: [PATCH 3/4] GH ref --- pandas/tests/indexes/test_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 912c85a2acbd1..3d4437244d019 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -388,7 +388,7 @@ def test_constructor_dtypes_to_object(self, cast_index, vals): assert index.dtype == object def test_constructor_categorical_to_object(self): - # Categorical data and dtype=object should return object-dtype + # GH#32167 Categorical data and dtype=object should return object-dtype ci = CategoricalIndex(range(5)) result = Index(ci, dtype=object) assert not isinstance(result, CategoricalIndex) From 0bcdabb5bb3cc7da7b480c773b64361335e39075 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 22 Feb 2020 20:04:11 -0800 Subject: [PATCH 4/4] use maybe_asobject --- pandas/core/indexes/base.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 9f1b93f008682..53c4dfde2775b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -304,10 +304,7 @@ def __new__( # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 from pandas.core.indexes.category import CategoricalIndex - if is_dtype_equal(_o_dtype, dtype): - ci = CategoricalIndex(data, copy=False, name=name, **kwargs) - return ci.astype(object) - return CategoricalIndex(data, dtype=dtype, copy=copy, name=name, **kwargs) + return _maybe_asobject(dtype, CategoricalIndex, data, copy, name, **kwargs) # interval elif is_interval_dtype(data) or is_interval_dtype(dtype):