12
12
from pandas .core .indexes .api import CategoricalIndex , Index
13
13
import pandas .util .testing as tm
14
14
15
- from .common import Base
15
+ from .. common import Base
16
16
17
17
18
18
class TestCategoricalIndex (Base ):
@@ -32,147 +32,6 @@ def test_can_hold_identifiers(self):
32
32
key = idx [0 ]
33
33
assert idx ._can_hold_identifiers_and_holds_name (key ) is True
34
34
35
- def test_construction (self ):
36
-
37
- ci = self .create_index (categories = list ("abcd" ))
38
- categories = ci .categories
39
-
40
- result = Index (ci )
41
- tm .assert_index_equal (result , ci , exact = True )
42
- assert not result .ordered
43
-
44
- result = Index (ci .values )
45
- tm .assert_index_equal (result , ci , exact = True )
46
- assert not result .ordered
47
-
48
- # empty
49
- result = CategoricalIndex (categories = categories )
50
- tm .assert_index_equal (result .categories , Index (categories ))
51
- tm .assert_numpy_array_equal (result .codes , np .array ([], dtype = "int8" ))
52
- assert not result .ordered
53
-
54
- # passing categories
55
- result = CategoricalIndex (list ("aabbca" ), categories = categories )
56
- tm .assert_index_equal (result .categories , Index (categories ))
57
- tm .assert_numpy_array_equal (
58
- result .codes , np .array ([0 , 0 , 1 , 1 , 2 , 0 ], dtype = "int8" )
59
- )
60
-
61
- c = pd .Categorical (list ("aabbca" ))
62
- result = CategoricalIndex (c )
63
- tm .assert_index_equal (result .categories , Index (list ("abc" )))
64
- tm .assert_numpy_array_equal (
65
- result .codes , np .array ([0 , 0 , 1 , 1 , 2 , 0 ], dtype = "int8" )
66
- )
67
- assert not result .ordered
68
-
69
- result = CategoricalIndex (c , categories = categories )
70
- tm .assert_index_equal (result .categories , Index (categories ))
71
- tm .assert_numpy_array_equal (
72
- result .codes , np .array ([0 , 0 , 1 , 1 , 2 , 0 ], dtype = "int8" )
73
- )
74
- assert not result .ordered
75
-
76
- ci = CategoricalIndex (c , categories = list ("abcd" ))
77
- result = CategoricalIndex (ci )
78
- tm .assert_index_equal (result .categories , Index (categories ))
79
- tm .assert_numpy_array_equal (
80
- result .codes , np .array ([0 , 0 , 1 , 1 , 2 , 0 ], dtype = "int8" )
81
- )
82
- assert not result .ordered
83
-
84
- result = CategoricalIndex (ci , categories = list ("ab" ))
85
- tm .assert_index_equal (result .categories , Index (list ("ab" )))
86
- tm .assert_numpy_array_equal (
87
- result .codes , np .array ([0 , 0 , 1 , 1 , - 1 , 0 ], dtype = "int8" )
88
- )
89
- assert not result .ordered
90
-
91
- result = CategoricalIndex (ci , categories = list ("ab" ), ordered = True )
92
- tm .assert_index_equal (result .categories , Index (list ("ab" )))
93
- tm .assert_numpy_array_equal (
94
- result .codes , np .array ([0 , 0 , 1 , 1 , - 1 , 0 ], dtype = "int8" )
95
- )
96
- assert result .ordered
97
-
98
- result = pd .CategoricalIndex (ci , categories = list ("ab" ), ordered = True )
99
- expected = pd .CategoricalIndex (
100
- ci , categories = list ("ab" ), ordered = True , dtype = "category"
101
- )
102
- tm .assert_index_equal (result , expected , exact = True )
103
-
104
- # turn me to an Index
105
- result = Index (np .array (ci ))
106
- assert isinstance (result , Index )
107
- assert not isinstance (result , CategoricalIndex )
108
-
109
- def test_construction_with_dtype (self ):
110
-
111
- # specify dtype
112
- ci = self .create_index (categories = list ("abc" ))
113
-
114
- result = Index (np .array (ci ), dtype = "category" )
115
- tm .assert_index_equal (result , ci , exact = True )
116
-
117
- result = Index (np .array (ci ).tolist (), dtype = "category" )
118
- tm .assert_index_equal (result , ci , exact = True )
119
-
120
- # these are generally only equal when the categories are reordered
121
- ci = self .create_index ()
122
-
123
- result = Index (np .array (ci ), dtype = "category" ).reorder_categories (ci .categories )
124
- tm .assert_index_equal (result , ci , exact = True )
125
-
126
- # make sure indexes are handled
127
- expected = CategoricalIndex ([0 , 1 , 2 ], categories = [0 , 1 , 2 ], ordered = True )
128
- idx = Index (range (3 ))
129
- result = CategoricalIndex (idx , categories = idx , ordered = True )
130
- tm .assert_index_equal (result , expected , exact = True )
131
-
132
- def test_construction_empty_with_bool_categories (self ):
133
- # see gh-22702
134
- cat = pd .CategoricalIndex ([], categories = [True , False ])
135
- categories = sorted (cat .categories .tolist ())
136
- assert categories == [False , True ]
137
-
138
- def test_construction_with_categorical_dtype (self ):
139
- # construction with CategoricalDtype
140
- # GH18109
141
- data , cats , ordered = "a a b b" .split (), "c b a" .split (), True
142
- dtype = CategoricalDtype (categories = cats , ordered = ordered )
143
-
144
- result = CategoricalIndex (data , dtype = dtype )
145
- expected = CategoricalIndex (data , categories = cats , ordered = ordered )
146
- tm .assert_index_equal (result , expected , exact = True )
147
-
148
- # GH 19032
149
- result = Index (data , dtype = dtype )
150
- tm .assert_index_equal (result , expected , exact = True )
151
-
152
- # error when combining categories/ordered and dtype kwargs
153
- msg = "Cannot specify `categories` or `ordered` together with `dtype`."
154
- with pytest .raises (ValueError , match = msg ):
155
- CategoricalIndex (data , categories = cats , dtype = dtype )
156
-
157
- with pytest .raises (ValueError , match = msg ):
158
- Index (data , categories = cats , dtype = dtype )
159
-
160
- with pytest .raises (ValueError , match = msg ):
161
- CategoricalIndex (data , ordered = ordered , dtype = dtype )
162
-
163
- with pytest .raises (ValueError , match = msg ):
164
- Index (data , ordered = ordered , dtype = dtype )
165
-
166
- def test_create_categorical (self ):
167
- # https://github.com/pandas-dev/pandas/pull/17513
168
- # The public CI constructor doesn't hit this code path with
169
- # instances of CategoricalIndex, but we still want to test the code
170
- ci = CategoricalIndex (["a" , "b" , "c" ])
171
- # First ci is self, second ci is data.
172
- result = CategoricalIndex ._create_categorical (ci , ci )
173
- expected = Categorical (["a" , "b" , "c" ])
174
- tm .assert_categorical_equal (result , expected )
175
-
176
35
@pytest .mark .parametrize (
177
36
"func,op_name" ,
178
37
[
0 commit comments