|
41 | 41 | )
|
42 | 42 |
|
43 | 43 |
|
| 44 | +def _is_nonempty(x, axis) -> bool: |
| 45 | + # filter empty arrays |
| 46 | + # 1-d dtypes always are included here |
| 47 | + if x.ndim <= axis: |
| 48 | + return True |
| 49 | + return x.shape[axis] > 0 |
| 50 | + |
| 51 | + |
44 | 52 | def concat_compat(
|
45 | 53 | to_concat: Sequence[ArrayLike], axis: AxisInt = 0, ea_compat_axis: bool = False
|
46 | 54 | ) -> ArrayLike:
|
@@ -79,36 +87,34 @@ def concat_compat(
|
79 | 87 | # e.g. DatetimeArray
|
80 | 88 | # NB: We are assuming here that ensure_wrapped_if_arraylike has
|
81 | 89 | # been called where relevant.
|
82 |
| - return obj._concat_same_type(to_concat_eas, axis=axis) |
83 |
| - |
84 |
| - # filter empty arrays |
85 |
| - # 1-d dtypes always are included here |
86 |
| - def is_nonempty(x) -> bool: |
87 |
| - if x.ndim <= axis: |
88 |
| - return True |
89 |
| - return x.shape[axis] > 0 |
| 90 | + return obj._concat_same_type( |
| 91 | + # error: Unexpected keyword argument "axis" for "_concat_same_type" |
| 92 | + # of "ExtensionArray" |
| 93 | + to_concat_eas, |
| 94 | + axis=axis, # type: ignore[call-arg] |
| 95 | + ) |
90 | 96 |
|
91 | 97 | # If all arrays are empty, there's nothing to convert, just short-cut to
|
92 | 98 | # the concatenation, #3121.
|
93 | 99 | #
|
94 | 100 | # Creating an empty array directly is tempting, but the winnings would be
|
95 | 101 | # marginal given that it would still require shape & dtype calculation and
|
96 | 102 | # np.concatenate which has them both implemented is compiled.
|
97 |
| - non_empties = [x for x in to_concat if is_nonempty(x)] |
| 103 | + non_empties = [x for x in to_concat if _is_nonempty(x, axis)] |
98 | 104 | if non_empties and axis == 0 and not ea_compat_axis:
|
99 | 105 | # ea_compat_axis see GH#39574
|
100 | 106 | to_concat = non_empties
|
101 | 107 |
|
102 | 108 | dtypes = {obj.dtype for obj in to_concat}
|
103 | 109 | kinds = {obj.dtype.kind for obj in to_concat}
|
104 | 110 | contains_datetime = any(
|
105 |
| - isinstance(dtype, (np.dtype, DatetimeTZDtype)) and dtype.kind in ["m", "M"] |
| 111 | + isinstance(dtype, (np.dtype, DatetimeTZDtype)) and dtype.kind in "mM" |
106 | 112 | for dtype in dtypes
|
107 | 113 | ) or any(isinstance(obj, ABCExtensionArray) and obj.ndim > 1 for obj in to_concat)
|
108 | 114 |
|
109 | 115 | all_empty = not len(non_empties)
|
110 |
| - single_dtype = len({x.dtype for x in to_concat}) == 1 |
111 |
| - any_ea = any(isinstance(x.dtype, ExtensionDtype) for x in to_concat) |
| 116 | + single_dtype = len(dtypes) == 1 |
| 117 | + any_ea = any(isinstance(x, ExtensionDtype) for x in dtypes) |
112 | 118 |
|
113 | 119 | if contains_datetime:
|
114 | 120 | return _concat_datetime(to_concat, axis=axis)
|
@@ -345,7 +351,7 @@ def _concat_datetime(to_concat: Sequence[ArrayLike], axis: AxisInt = 0) -> Array
|
345 | 351 |
|
346 | 352 | to_concat = [ensure_wrapped_if_datetimelike(x) for x in to_concat]
|
347 | 353 |
|
348 |
| - single_dtype = len({x.dtype for x in to_concat}) == 1 |
| 354 | + single_dtype = lib.dtypes_all_equal([x.dtype for x in to_concat]) |
349 | 355 |
|
350 | 356 | # multiple types, need to coerce to object
|
351 | 357 | if not single_dtype:
|
|
0 commit comments