Skip to content

Commit 034fab5

Browse files
authored
BUG: Index(categorical, dtype=object) not returning object dtype (#32167)
1 parent 0edd2d9 commit 034fab5

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Bug fixes
103103

104104
Categorical
105105
^^^^^^^^^^^
106-
106+
- 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`)
107107
-
108108
-
109109

pandas/core/indexes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def __new__(
304304
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
305305
from pandas.core.indexes.category import CategoricalIndex
306306

307-
return CategoricalIndex(data, dtype=dtype, copy=copy, name=name, **kwargs)
307+
return _maybe_asobject(dtype, CategoricalIndex, data, copy, name, **kwargs)
308308

309309
# interval
310310
elif is_interval_dtype(data) or is_interval_dtype(dtype):

pandas/tests/indexes/test_index_new.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from pandas.core.dtypes.common import is_unsigned_integer_dtype
88

9-
from pandas import Index, Int64Index, MultiIndex, UInt64Index
9+
from pandas import CategoricalIndex, Index, Int64Index, MultiIndex, UInt64Index
1010
import pandas._testing as tm
1111

1212

@@ -47,3 +47,9 @@ def test_constructor_dtypes_to_object(self, cast_index, vals):
4747

4848
assert type(index) is Index
4949
assert index.dtype == object
50+
51+
def test_constructor_categorical_to_object(self):
52+
# GH#32167 Categorical data and dtype=object should return object-dtype
53+
ci = CategoricalIndex(range(5))
54+
result = Index(ci, dtype=object)
55+
assert not isinstance(result, CategoricalIndex)

0 commit comments

Comments
 (0)