|
3 | 3 | """
|
4 | 4 | from __future__ import annotations
|
5 | 5 |
|
6 |
| -from typing import ( |
7 |
| - TYPE_CHECKING, |
8 |
| - cast, |
9 |
| -) |
| 6 | +from typing import TYPE_CHECKING |
10 | 7 | import warnings
|
11 | 8 |
|
12 | 9 | import numpy as np
|
13 | 10 |
|
14 |
| -from pandas._typing import ( |
15 |
| - ArrayLike, |
16 |
| - AxisInt, |
17 |
| - DtypeObj, |
18 |
| -) |
| 11 | +from pandas._typing import AxisInt |
19 | 12 | from pandas.util._exceptions import find_stack_level
|
20 | 13 |
|
21 | 14 | from pandas.core.dtypes.astype import astype_array
|
22 | 15 | from pandas.core.dtypes.cast import (
|
23 | 16 | common_dtype_categorical_compat,
|
24 | 17 | find_common_type,
|
25 | 18 | )
|
26 |
| -from pandas.core.dtypes.common import ( |
27 |
| - is_dtype_equal, |
28 |
| - is_sparse, |
29 |
| -) |
| 19 | +from pandas.core.dtypes.common import is_dtype_equal |
30 | 20 | from pandas.core.dtypes.dtypes import (
|
31 | 21 | DatetimeTZDtype,
|
32 | 22 | ExtensionDtype,
|
|
39 | 29 |
|
40 | 30 | if TYPE_CHECKING:
|
41 | 31 | from pandas.core.arrays import Categorical
|
42 |
| - from pandas.core.arrays.sparse import SparseArray |
43 |
| - |
44 |
| - |
45 |
| -def cast_to_common_type(arr: ArrayLike, dtype: DtypeObj) -> ArrayLike: |
46 |
| - """ |
47 |
| - Helper function for `arr.astype(common_dtype)` but handling all special |
48 |
| - cases. |
49 |
| - """ |
50 |
| - if is_dtype_equal(arr.dtype, dtype): |
51 |
| - return arr |
52 |
| - |
53 |
| - if is_sparse(arr) and not is_sparse(dtype): |
54 |
| - # TODO(2.0): remove special case once SparseArray.astype deprecation |
55 |
| - # is enforced. |
56 |
| - # problem case: SparseArray.astype(dtype) doesn't follow the specified |
57 |
| - # dtype exactly, but converts this to Sparse[dtype] -> first manually |
58 |
| - # convert to dense array |
59 |
| - |
60 |
| - # error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has incompatible type |
61 |
| - # "Union[dtype[Any], ExtensionDtype]"; expected "Union[dtype[Any], None, type, _ |
62 |
| - # SupportsDType[dtype[Any]], str, Union[Tuple[Any, int], Tuple[Any, |
63 |
| - # Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any], _DTypeDict, |
64 |
| - # Tuple[Any, Any]]]" [arg-type] |
65 |
| - arr = cast("SparseArray", arr) |
66 |
| - return arr.to_dense().astype(dtype, copy=False) # type: ignore[arg-type] |
67 |
| - |
68 |
| - # astype_array includes ensure_wrapped_if_datetimelike |
69 |
| - return astype_array(arr, dtype=dtype, copy=False) |
70 | 32 |
|
71 | 33 |
|
72 | 34 | def concat_compat(to_concat, axis: AxisInt = 0, ea_compat_axis: bool = False):
|
@@ -126,7 +88,9 @@ def is_nonempty(x) -> bool:
|
126 | 88 | if not single_dtype:
|
127 | 89 | target_dtype = find_common_type([x.dtype for x in to_concat])
|
128 | 90 | target_dtype = common_dtype_categorical_compat(to_concat, target_dtype)
|
129 |
| - to_concat = [cast_to_common_type(arr, target_dtype) for arr in to_concat] |
| 91 | + to_concat = [ |
| 92 | + astype_array(arr, target_dtype, copy=False) for arr in to_concat |
| 93 | + ] |
130 | 94 |
|
131 | 95 | if isinstance(to_concat[0], ABCExtensionArray):
|
132 | 96 | # TODO: what about EA-backed Index?
|
|
0 commit comments