@@ -69,16 +69,7 @@ def concat_compat(
6969 -------
7070 a single array, preserving the combined dtypes
7171 """
72- # Special handling for categorical arrays solves #51362
73- if (
74- len (to_concat )
75- and all (isinstance (arr .dtype , CategoricalDtype ) for arr in to_concat )
76- and axis == 0
77- ):
78- return union_categoricals (
79- to_concat , sort_categories = True
80- ) # Performance cost, but necessary to keep tests passing.
81- # see pandas/tests/reshape/concat/test_append_common.py:498
72+
8273 if len (to_concat ) and lib .dtypes_all_equal ([obj .dtype for obj in to_concat ]):
8374 # fastpath!
8475 obj = to_concat [0 ]
@@ -102,6 +93,27 @@ def concat_compat(
10293 to_concat_eas ,
10394 axis = axis , # type: ignore[call-arg]
10495 )
96+ # Special handling for categorical arrays solves #51362
97+ if (
98+ len (to_concat )
99+ and all (isinstance (arr .dtype , CategoricalDtype ) for arr in to_concat )
100+ and axis == 0
101+ ):
102+ # Filter out empty arrays before union, similar to non_empties logic
103+ non_empty_categoricals = [x for x in to_concat if _is_nonempty (x , axis )]
104+
105+ if len (non_empty_categoricals ) == 0 :
106+ # All arrays are empty, return the first one (they're all categorical)
107+ return to_concat [0 ]
108+ elif len (non_empty_categoricals ) == 1 :
109+ # Only one non-empty array, return it directly
110+ return non_empty_categoricals [0 ]
111+ else :
112+ # Multiple non-empty arrays, use union_categoricals
113+ return union_categoricals (
114+ non_empty_categoricals , sort_categories = True
115+ ) # Performance cost, but necessary to keep tests passing.
116+ # see pandas/tests/reshape/concat/test_append_common.py:498
105117
106118 # If all arrays are empty, there's nothing to convert, just short-cut to
107119 # the concatenation, #3121.
0 commit comments