Skip to content

Commit ce7b518

Browse files
author
adrien pacifico
committed
- change test from numpy to categorical_equal assertion
- change concat function to handle empty elements
1 parent 5bdc4e0 commit ce7b518

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

pandas/core/dtypes/concat.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

pandas/tests/dtypes/test_concat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_concat_mismatched_categoricals_with_empty():
1717

1818
result = _concat.concat_compat([ser1._values, ser2._values])
1919
expected = pd.concat([ser1, ser2])._values
20-
tm.assert_numpy_array_equal(result, expected)
20+
tm.assert_categorical_equal(result, expected)
2121

2222

2323
def test_concat_single_dataframe_tz_aware():

0 commit comments

Comments
 (0)