Skip to content

BUG: Index(categorical, dtype=object) not returning object dtype #32167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,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`)
-
-

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def __new__(
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
from pandas.core.indexes.category import CategoricalIndex

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):
Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/indexes/test_index_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pandas.core.dtypes.common import is_unsigned_integer_dtype

from pandas import Index, Int64Index, MultiIndex, UInt64Index
from pandas import CategoricalIndex, Index, Int64Index, MultiIndex, UInt64Index
import pandas._testing as tm


Expand Down Expand Up @@ -47,3 +47,9 @@ def test_constructor_dtypes_to_object(self, cast_index, vals):

assert type(index) is Index
assert index.dtype == object

def test_constructor_categorical_to_object(self):
# 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)