Skip to content

Commit 58c2f09

Browse files
topper-123jreback
authored andcommitted
Let CategoricalIndex take CategoricalDtype as dtype (#18116)
1 parent de299f6 commit 58c2f09

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

doc/source/whatsnew/v0.21.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Categorical
122122
- Bug in :meth:`DataFrame.astype` where casting to 'category' on an empty ``DataFrame`` causes a segmentation fault (:issue:`18004`)
123123
- Error messages in the testing module have been improved when items have
124124
different ``CategoricalDtype`` (:issue:`18069`)
125-
-
125+
- ``CategoricalIndex`` can now correctly take a ``pd.api.types.CategoricalDtype`` as its dtype (:issue:`18116`)
126126

127127
Other
128128
^^^^^

pandas/core/indexes/category.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def __new__(cls, data=None, categories=None, ordered=None, dtype=None,
7979
if data is not None or categories is None:
8080
cls._scalar_data_error(data)
8181
data = []
82-
data = cls._create_categorical(cls, data, categories, ordered)
82+
data = cls._create_categorical(cls, data, categories, ordered,
83+
dtype)
8384

8485
if copy:
8586
data = data.copy()

pandas/tests/indexes/test_category.py

+25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pandas.util.testing as tm
66
from pandas.core.indexes.api import Index, CategoricalIndex
7+
from pandas.core.dtypes.dtypes import CategoricalDtype
78
from .common import Base
89

910
from pandas.compat import range, PY3
@@ -95,6 +96,11 @@ def test_construction(self):
9596
1, -1, 0], dtype='int8'))
9697
assert result.ordered
9798

99+
result = pd.CategoricalIndex(ci, categories=list('ab'), ordered=True)
100+
expected = pd.CategoricalIndex(ci, categories=list('ab'), ordered=True,
101+
dtype='category')
102+
tm.assert_index_equal(result, expected, exact=True)
103+
98104
# turn me to an Index
99105
result = Index(np.array(ci))
100106
assert isinstance(result, Index)
@@ -125,6 +131,25 @@ def test_construction_with_dtype(self):
125131
result = CategoricalIndex(idx, categories=idx, ordered=True)
126132
tm.assert_index_equal(result, expected, exact=True)
127133

134+
def test_construction_with_categorical_dtype(self):
135+
# construction with CategoricalDtype
136+
# GH18109
137+
data, cats, ordered = 'a a b b'.split(), 'c b a'.split(), True
138+
dtype = CategoricalDtype(categories=cats, ordered=ordered)
139+
140+
result = pd.CategoricalIndex(data, dtype=dtype)
141+
expected = pd.CategoricalIndex(data, categories=cats,
142+
ordered=ordered)
143+
tm.assert_index_equal(result, expected, exact=True)
144+
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)
152+
128153
def test_create_categorical(self):
129154
# https://github.com/pandas-dev/pandas/pull/17513
130155
# The public CI constructor doesn't hit this code path with

0 commit comments

Comments
 (0)