Skip to content

Commit 0428536

Browse files
authored
TYP: concat_compat (#52315)
1 parent 578275d commit 0428536

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

pandas/core/algorithms.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,11 @@ def union_with_duplicates(
16661666
lvals = lvals._values
16671667
if isinstance(rvals, ABCIndex):
16681668
rvals = rvals._values
1669-
unique_vals = unique(concat_compat([lvals, rvals]))
1669+
# error: List item 0 has incompatible type "Union[ExtensionArray,
1670+
# ndarray[Any, Any], Index]"; expected "Union[ExtensionArray,
1671+
# ndarray[Any, Any]]"
1672+
combined = concat_compat([lvals, rvals]) # type: ignore[list-item]
1673+
unique_vals = unique(combined)
16701674
unique_vals = ensure_wrapped_if_datetimelike(unique_vals)
16711675
repeats = final_count.reindex(unique_vals).values
16721676
return np.repeat(unique_vals, repeats)

pandas/core/dtypes/cast.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
TYPE_CHECKING,
1111
Any,
1212
Literal,
13+
Sequence,
1314
Sized,
1415
TypeVar,
1516
cast,
@@ -1317,7 +1318,7 @@ def find_result_type(left: ArrayLike, right: Any) -> DtypeObj:
13171318

13181319

13191320
def common_dtype_categorical_compat(
1320-
objs: list[Index | ArrayLike], dtype: DtypeObj
1321+
objs: Sequence[Index | ArrayLike], dtype: DtypeObj
13211322
) -> DtypeObj:
13221323
"""
13231324
Update the result of find_common_type to account for NAs in a Categorical.

pandas/core/dtypes/concat.py

+34-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
"""
44
from __future__ import annotations
55

6-
from typing import TYPE_CHECKING
6+
from typing import (
7+
TYPE_CHECKING,
8+
Sequence,
9+
cast,
10+
)
711

812
import numpy as np
913

@@ -24,12 +28,20 @@
2428
)
2529

2630
if 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

Comments
 (0)