|
4 | 4 |
|
5 | 5 | import pandas.util.testing as tm
|
6 | 6 | from pandas.core.indexes.api import Index, CategoricalIndex
|
| 7 | +from pandas.core.dtypes.dtypes import CategoricalDtype |
7 | 8 | from .common import Base
|
8 | 9 |
|
9 | 10 | from pandas.compat import range, PY3
|
@@ -95,6 +96,11 @@ def test_construction(self):
|
95 | 96 | 1, -1, 0], dtype='int8'))
|
96 | 97 | assert result.ordered
|
97 | 98 |
|
| 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 | + |
98 | 104 | # turn me to an Index
|
99 | 105 | result = Index(np.array(ci))
|
100 | 106 | assert isinstance(result, Index)
|
@@ -125,6 +131,23 @@ def test_construction_with_dtype(self):
|
125 | 131 | result = CategoricalIndex(idx, categories=idx, ordered=True)
|
126 | 132 | tm.assert_index_equal(result, expected, exact=True)
|
127 | 133 |
|
| 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): |
| 147 | + pd.CategoricalIndex(data, categories=cats, dtype=dtype) |
| 148 | + with pytest.raises(ValueError): |
| 149 | + pd.CategoricalIndex(data, ordered=ordered, dtype=dtype) |
| 150 | + |
128 | 151 | def test_create_categorical(self):
|
129 | 152 | # https://github.com/pandas-dev/pandas/pull/17513
|
130 | 153 | # The public CI constructor doesn't hit this code path with
|
|
0 commit comments