7
7
import pandas as pd
8
8
from pandas import (
9
9
Categorical ,
10
- CategoricalIndex ,
11
10
DataFrame ,
12
11
Series ,
13
12
)
@@ -76,13 +75,13 @@ def test_concat_categoricalindex(self):
76
75
# GH 16111, categories that aren't lexsorted
77
76
categories = [9 , 0 , 1 , 2 , 3 ]
78
77
79
- a = Series (1 , index = CategoricalIndex ([9 , 0 ], categories = categories ))
80
- b = Series (2 , index = CategoricalIndex ([0 , 1 ], categories = categories ))
81
- c = Series (3 , index = CategoricalIndex ([1 , 2 ], categories = categories ))
78
+ a = Series (1 , index = pd . CategoricalIndex ([9 , 0 ], categories = categories ))
79
+ b = Series (2 , index = pd . CategoricalIndex ([0 , 1 ], categories = categories ))
80
+ c = Series (3 , index = pd . CategoricalIndex ([1 , 2 ], categories = categories ))
82
81
83
82
result = pd .concat ([a , b , c ], axis = 1 )
84
83
85
- exp_idx = CategoricalIndex ([9 , 0 , 1 , 2 ], categories = categories )
84
+ exp_idx = pd . CategoricalIndex ([9 , 0 , 1 , 2 ], categories = categories )
86
85
exp = DataFrame (
87
86
{
88
87
0 : [1 , 1 , np .nan , np .nan ],
@@ -100,7 +99,7 @@ def test_categorical_concat_preserve(self):
100
99
s = Series (list ("abc" ), dtype = "category" )
101
100
s2 = Series (list ("abd" ), dtype = "category" )
102
101
103
- exp = Series (list ("abcabd" ), dtype = "category" )
102
+ exp = Series (list ("abcabd" ))
104
103
res = pd .concat ([s , s2 ], ignore_index = True )
105
104
tm .assert_series_equal (res , exp )
106
105
@@ -148,8 +147,8 @@ def test_categorical_index_preserver(self):
148
147
result = pd .concat ([df2 , df3 ])
149
148
expected = pd .concat (
150
149
[
151
- df2 .set_axis (df2 .index .astype ("category" ), axis = 0 ),
152
- df3 .set_axis (df3 .index .astype ("category" ), axis = 0 ),
150
+ df2 .set_axis (df2 .index .astype (object ), axis = 0 ),
151
+ df3 .set_axis (df3 .index .astype (object ), axis = 0 ),
153
152
]
154
153
)
155
154
tm .assert_frame_equal (result , expected )
@@ -180,8 +179,7 @@ def test_concat_categorical_datetime(self):
180
179
181
180
result = pd .concat ([df1 , df2 ])
182
181
expected = DataFrame (
183
- {"x" : Series ([datetime (2021 , 1 , 1 ), datetime (2021 , 1 , 2 )])},
184
- dtype = "category" ,
182
+ {"x" : Series ([datetime (2021 , 1 , 1 ), datetime (2021 , 1 , 2 )])}
185
183
)
186
184
187
185
tm .assert_equal (result , expected )
@@ -229,17 +227,15 @@ def test_categorical_index_upcast(self):
229
227
b = DataFrame ({"foo" : [4 , 3 ]}, index = Categorical (["baz" , "bar" ]))
230
228
231
229
res = pd .concat ([a , b ])
232
- exp = DataFrame (
233
- {"foo" : [1 , 2 , 4 , 3 ]}, index = Categorical (["foo" , "bar" , "baz" , "bar" ])
234
- )
230
+ exp = DataFrame ({"foo" : [1 , 2 , 4 , 3 ]}, index = ["foo" , "bar" , "baz" , "bar" ])
235
231
236
232
tm .assert_equal (res , exp )
237
233
238
234
a = Series ([1 , 2 ], index = Categorical (["foo" , "bar" ]))
239
235
b = Series ([4 , 3 ], index = Categorical (["baz" , "bar" ]))
240
236
241
237
res = pd .concat ([a , b ])
242
- exp = Series ([1 , 2 , 4 , 3 ], index = Categorical ( ["foo" , "bar" , "baz" , "bar" ]) )
238
+ exp = Series ([1 , 2 , 4 , 3 ], index = ["foo" , "bar" , "baz" , "bar" ])
243
239
244
240
tm .assert_equal (res , exp )
245
241
@@ -261,9 +257,9 @@ def test_categorical_missing_from_one_frame(self):
261
257
def test_concat_categorical_same_categories_different_order (self ):
262
258
# https://github.com/pandas-dev/pandas/issues/24845
263
259
264
- c1 = CategoricalIndex (["a" , "a" ], categories = ["a" , "b" ], ordered = False )
265
- c2 = CategoricalIndex (["b" , "b" ], categories = ["b" , "a" ], ordered = False )
266
- c3 = CategoricalIndex (
260
+ c1 = pd . CategoricalIndex (["a" , "a" ], categories = ["a" , "b" ], ordered = False )
261
+ c2 = pd . CategoricalIndex (["b" , "b" ], categories = ["b" , "a" ], ordered = False )
262
+ c3 = pd . CategoricalIndex (
267
263
["a" , "a" , "b" , "b" ], categories = ["a" , "b" ], ordered = False
268
264
)
269
265
0 commit comments