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