@@ -95,7 +95,15 @@ def is_nonempty(x) -> bool:
95
95
_contains_datetime = any (typ .startswith ("datetime" ) for typ in typs )
96
96
_contains_period = any (typ .startswith ("period" ) for typ in typs )
97
97
98
- if "category" in typs :
98
+ all_empty = not len (non_empties )
99
+ single_dtype = len ({x .dtype for x in to_concat }) == 1
100
+ any_ea = any (is_extension_array_dtype (x .dtype ) for x in to_concat )
101
+
102
+ if any_ea and single_dtype and axis == 0 :
103
+ cls = type (to_concat [0 ])
104
+ return cls ._concat_same_type (to_concat )
105
+
106
+ elif "category" in typs :
99
107
# this must be prior to concat_datetime,
100
108
# to support Categorical + datetime-like
101
109
return concat_categorical (to_concat , axis = axis )
@@ -107,18 +115,11 @@ def is_nonempty(x) -> bool:
107
115
elif "sparse" in typs :
108
116
return _concat_sparse (to_concat , axis = axis , typs = typs )
109
117
110
- all_empty = not len (non_empties )
111
- single_dtype = len ({x .dtype for x in to_concat }) == 1
112
- any_ea = any (is_extension_array_dtype (x .dtype ) for x in to_concat )
113
-
114
- if any_ea and axis == 1 :
118
+ elif any_ea and axis == 1 :
115
119
to_concat = [np .atleast_2d (x .astype ("object" )) for x in to_concat ]
120
+ return np .concatenate (to_concat , axis = axis )
116
121
117
- elif any_ea and single_dtype and axis == 0 :
118
- cls = type (to_concat [0 ])
119
- return cls ._concat_same_type (to_concat )
120
-
121
- if all_empty :
122
+ elif all_empty :
122
123
# we have all empties, but may need to coerce the result dtype to
123
124
# object if we have non-numeric type operands (numpy would otherwise
124
125
# cast this to float)
@@ -282,15 +283,15 @@ def union_categoricals(
282
283
[b, c, a, b]
283
284
Categories (3, object): [b, c, a]
284
285
"""
285
- from pandas import Index , Categorical
286
+ from pandas import Categorical
286
287
from pandas .core .arrays .categorical import recode_for_categories
287
288
288
289
if len (to_union ) == 0 :
289
290
raise ValueError ("No Categoricals to union" )
290
291
291
292
def _maybe_unwrap (x ):
292
293
if isinstance (x , (ABCCategoricalIndex , ABCSeries )):
293
- return x .values
294
+ return x ._values
294
295
elif isinstance (x , Categorical ):
295
296
return x
296
297
else :
@@ -333,7 +334,7 @@ def _maybe_unwrap(x):
333
334
elif ignore_order or all (not c .ordered for c in to_union ):
334
335
# different categories - union and recode
335
336
cats = first .categories .append ([c .categories for c in to_union [1 :]])
336
- categories = Index ( cats .unique () )
337
+ categories = cats .unique ()
337
338
if sort_categories :
338
339
categories = categories .sort_values ()
339
340
0 commit comments