Skip to content

Commit 4080bc0

Browse files
committed
BUG: Index constructor does not maintain CategoricalDtype
1 parent ab000a9 commit 4080bc0

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ Conversion
300300
- Bug in :class:`FY5253` where ``datetime`` addition and subtraction incremented incorrectly for dates on the year-end but not normalized to midnight (:issue:`18854`)
301301
- Bug in :class:`DatetimeIndex` where adding or subtracting an array-like of ``DateOffset`` objects either raised (``np.array``, ``pd.Index``) or broadcast incorrectly (``pd.Series``) (:issue:`18849`)
302302
- Bug in :class:`Series` floor-division where operating on a scalar ``timedelta`` raises an exception (:issue:`18846`)
303+
- Bug in :class:`Index` constructor with ``dtype=CategoricalDtype(...)`` where ``categories`` and ``ordered`` are not maintained (issue:`19032`)
303304

304305

305306
Indexing

pandas/core/indexes/base.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,13 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None,
197197
# categorical
198198
if is_categorical_dtype(data) or is_categorical_dtype(dtype):
199199
from .category import CategoricalIndex
200-
return CategoricalIndex(data, copy=copy, name=name, **kwargs)
200+
return CategoricalIndex(data, dtype=dtype, copy=copy, name=name,
201+
**kwargs)
201202

202203
# interval
203-
if is_interval_dtype(data):
204+
if is_interval_dtype(data) or is_interval_dtype(dtype):
204205
from .interval import IntervalIndex
205-
return IntervalIndex.from_intervals(data, name=name,
206-
copy=copy)
206+
return IntervalIndex(data, dtype=dtype, name=name, copy=copy)
207207

208208
# index-like
209209
elif isinstance(data, (np.ndarray, Index, ABCSeries)):

pandas/tests/indexes/test_category.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,27 @@ def test_construction_with_categorical_dtype(self):
137137
data, cats, ordered = 'a a b b'.split(), 'c b a'.split(), True
138138
dtype = CategoricalDtype(categories=cats, ordered=ordered)
139139

140-
result = pd.CategoricalIndex(data, dtype=dtype)
141-
expected = pd.CategoricalIndex(data, categories=cats,
142-
ordered=ordered)
140+
result = CategoricalIndex(data, dtype=dtype)
141+
expected = CategoricalIndex(data, categories=cats, ordered=ordered)
143142
tm.assert_index_equal(result, expected, exact=True)
144143

145-
# error to combine categories or ordered and dtype keywords args
146-
with pytest.raises(ValueError, match="Cannot specify both `dtype` and "
147-
"`categories` or `ordered`."):
148-
pd.CategoricalIndex(data, categories=cats, dtype=dtype)
149-
with pytest.raises(ValueError, match="Cannot specify both `dtype` and "
150-
"`categories` or `ordered`."):
151-
pd.CategoricalIndex(data, ordered=ordered, dtype=dtype)
144+
# GH 19032
145+
result = Index(data, dtype=dtype)
146+
tm.assert_index_equal(result, expected, exact=True)
147+
148+
# error when combining categories/ordered and dtype kwargs
149+
msg = 'Cannot specify both `dtype` and `categories` or `ordered`.'
150+
with pytest.raises(ValueError, match=msg):
151+
CategoricalIndex(data, categories=cats, dtype=dtype)
152+
153+
with pytest.raises(ValueError, match=msg):
154+
Index(data, categories=cats, dtype=dtype)
155+
156+
with pytest.raises(ValueError, match=msg):
157+
CategoricalIndex(data, ordered=ordered, dtype=dtype)
158+
159+
with pytest.raises(ValueError, match=msg):
160+
Index(data, ordered=ordered, dtype=dtype)
152161

153162
def test_create_categorical(self):
154163
# https://github.com/pandas-dev/pandas/pull/17513

0 commit comments

Comments
 (0)