3
3
"""
4
4
from __future__ import annotations
5
5
6
- from typing import TYPE_CHECKING
6
+ from typing import (
7
+ TYPE_CHECKING,
8
+ Sequence,
9
+ cast,
10
+ )
7
11
8
12
import numpy as np
9
13
24
28
)
25
29
26
30
if TYPE_CHECKING:
27
- from pandas._typing import AxisInt
31
+ from pandas._typing import (
32
+ ArrayLike,
33
+ AxisInt,
34
+ )
28
35
29
- from pandas.core.arrays import Categorical
36
+ from pandas.core.arrays import (
37
+ Categorical,
38
+ ExtensionArray,
39
+ )
30
40
31
41
32
- def concat_compat(to_concat, axis: AxisInt = 0, ea_compat_axis: bool = False):
42
+ def concat_compat(
43
+ to_concat: Sequence[ArrayLike], axis: AxisInt = 0, ea_compat_axis: bool = False
44
+ ) -> ArrayLike:
33
45
"""
34
46
provide concatenation of an array of arrays each of which is a single
35
47
'normalized' dtypes (in that for example, if it's object, then it is a
@@ -38,7 +50,7 @@ def concat_compat(to_concat, axis: AxisInt = 0, ea_compat_axis: bool = False):
38
50
39
51
Parameters
40
52
----------
41
- to_concat : array of arrays
53
+ to_concat : sequence of arrays
42
54
axis : axis to provide concatenation
43
55
ea_compat_axis : bool, default False
44
56
For ExtensionArray compat, behave as if axis == 1 when determining
@@ -93,10 +105,12 @@ def is_nonempty(x) -> bool:
93
105
94
106
if isinstance(to_concat[0], ABCExtensionArray):
95
107
# TODO: what about EA-backed Index?
108
+ to_concat_eas = cast("Sequence[ExtensionArray]", to_concat)
96
109
cls = type(to_concat[0])
97
- return cls._concat_same_type(to_concat )
110
+ return cls._concat_same_type(to_concat_eas )
98
111
else:
99
- return np.concatenate(to_concat)
112
+ to_concat_arrs = cast("Sequence[np.ndarray]", to_concat)
113
+ return np.concatenate(to_concat_arrs)
100
114
101
115
elif all_empty:
102
116
# we have all empties, but may need to coerce the result dtype to
@@ -111,7 +125,10 @@ def is_nonempty(x) -> bool:
111
125
to_concat = [x.astype("object") for x in to_concat]
112
126
kinds = {"o"}
113
127
114
- result = np.concatenate(to_concat, axis=axis)
128
+ # error: Argument 1 to "concatenate" has incompatible type
129
+ # "Sequence[Union[ExtensionArray, ndarray[Any, Any]]]"; expected
130
+ # "Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]]]"
131
+ result: np.ndarray = np.concatenate(to_concat, axis=axis) # type: ignore[arg-type]
115
132
if "b" in kinds and result.dtype.kind in ["i", "u", "f"]:
116
133
# GH#39817 cast to object instead of casting bools to numeric
117
134
result = result.astype(object, copy=False)
@@ -283,21 +300,21 @@ def _maybe_unwrap(x):
283
300
return Categorical(new_codes, categories=categories, ordered=ordered, fastpath=True)
284
301
285
302
286
- def _concatenate_2d(to_concat, axis: AxisInt):
303
+ def _concatenate_2d(to_concat: Sequence[np.ndarray] , axis: AxisInt) -> np.ndarray :
287
304
# coerce to 2d if needed & concatenate
288
305
if axis == 1:
289
306
to_concat = [np.atleast_2d(x) for x in to_concat]
290
307
return np.concatenate(to_concat, axis=axis)
291
308
292
309
293
- def _concat_datetime(to_concat, axis: AxisInt = 0):
310
+ def _concat_datetime(to_concat: Sequence[ArrayLike] , axis: AxisInt = 0) -> ArrayLike :
294
311
"""
295
312
provide concatenation of an datetimelike array of arrays each of which is a
296
313
single M8[ns], datetime64[ns, tz] or m8[ns] dtype
297
314
298
315
Parameters
299
316
----------
300
- to_concat : array of arrays
317
+ to_concat : sequence of arrays
301
318
axis : axis to provide concatenation
302
319
303
320
Returns
@@ -316,5 +333,10 @@ def _concat_datetime(to_concat, axis: AxisInt = 0):
316
333
# in Timestamp/Timedelta
317
334
return _concatenate_2d([x.astype(object) for x in to_concat], axis=axis)
318
335
319
- result = type(to_concat[0])._concat_same_type(to_concat, axis=axis)
336
+ # error: Unexpected keyword argument "axis" for "_concat_same_type" of
337
+ # "ExtensionArray"
338
+ to_concat_eas = cast("list[ExtensionArray]", to_concat)
339
+ result = type(to_concat_eas[0])._concat_same_type( # type: ignore[call-arg]
340
+ to_concat_eas, axis=axis
341
+ )
320
342
return result
0 commit comments