33"""
44from __future__ import annotations
55
6- from typing import TYPE_CHECKING
6+ from typing import (
7+ TYPE_CHECKING ,
8+ Sequence ,
9+ cast ,
10+ )
711
812import numpy as np
913
2428)
2529
2630if TYPE_CHECKING :
27- from pandas ._typing import AxisInt
31+ from pandas ._typing import (
32+ ArrayLike ,
33+ AxisInt ,
34+ )
2835
29- from pandas .core .arrays import Categorical
36+ from pandas .core .arrays import (
37+ Categorical ,
38+ ExtensionArray ,
39+ )
3040
3141
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 :
3345 """
3446 provide concatenation of an array of arrays each of which is a single
3547 '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):
3850
3951 Parameters
4052 ----------
41- to_concat : array of arrays
53+ to_concat : sequence of arrays
4254 axis : axis to provide concatenation
4355 ea_compat_axis : bool, default False
4456 For ExtensionArray compat, behave as if axis == 1 when determining
@@ -93,10 +105,12 @@ def is_nonempty(x) -> bool:
93105
94106 if isinstance (to_concat [0 ], ABCExtensionArray ):
95107 # TODO: what about EA-backed Index?
108+ to_concat_eas = cast ("Sequence[ExtensionArray]" , to_concat )
96109 cls = type (to_concat [0 ])
97- return cls ._concat_same_type (to_concat )
110+ return cls ._concat_same_type (to_concat_eas )
98111 else :
99- return np .concatenate (to_concat )
112+ to_concat_arrs = cast ("Sequence[np.ndarray]" , to_concat )
113+ return np .concatenate (to_concat_arrs )
100114
101115 elif all_empty :
102116 # we have all empties, but may need to coerce the result dtype to
@@ -111,7 +125,10 @@ def is_nonempty(x) -> bool:
111125 to_concat = [x .astype ("object" ) for x in to_concat ]
112126 kinds = {"o" }
113127
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]
115132 if "b" in kinds and result .dtype .kind in ["i" , "u" , "f" ]:
116133 # GH#39817 cast to object instead of casting bools to numeric
117134 result = result .astype (object , copy = False )
@@ -283,21 +300,21 @@ def _maybe_unwrap(x):
283300 return Categorical (new_codes , categories = categories , ordered = ordered , fastpath = True )
284301
285302
286- def _concatenate_2d (to_concat , axis : AxisInt ):
303+ def _concatenate_2d (to_concat : Sequence [ np . ndarray ] , axis : AxisInt ) -> np . ndarray :
287304 # coerce to 2d if needed & concatenate
288305 if axis == 1 :
289306 to_concat = [np .atleast_2d (x ) for x in to_concat ]
290307 return np .concatenate (to_concat , axis = axis )
291308
292309
293- def _concat_datetime (to_concat , axis : AxisInt = 0 ):
310+ def _concat_datetime (to_concat : Sequence [ ArrayLike ] , axis : AxisInt = 0 ) -> ArrayLike :
294311 """
295312 provide concatenation of an datetimelike array of arrays each of which is a
296313 single M8[ns], datetime64[ns, tz] or m8[ns] dtype
297314
298315 Parameters
299316 ----------
300- to_concat : array of arrays
317+ to_concat : sequence of arrays
301318 axis : axis to provide concatenation
302319
303320 Returns
@@ -316,5 +333,10 @@ def _concat_datetime(to_concat, axis: AxisInt = 0):
316333 # in Timestamp/Timedelta
317334 return _concatenate_2d ([x .astype (object ) for x in to_concat ], axis = axis )
318335
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+ )
320342 return result
0 commit comments