Skip to content

Commit 23f12a1

Browse files
authored
CLN: reuse helper (#45401)
1 parent 07732d7 commit 23f12a1

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

pandas/core/dtypes/concat.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
from pandas.util._exceptions import find_stack_level
1717

1818
from pandas.core.dtypes.astype import astype_array
19-
from pandas.core.dtypes.cast import find_common_type
19+
from pandas.core.dtypes.cast import (
20+
common_dtype_categorical_compat,
21+
find_common_type,
22+
)
2023
from pandas.core.dtypes.common import (
21-
is_categorical_dtype,
2224
is_dtype_equal,
2325
is_sparse,
2426
)
@@ -30,7 +32,6 @@
3032
)
3133

3234
if TYPE_CHECKING:
33-
from pandas import Categorical
3435
from pandas.core.arrays.sparse import SparseArray
3536

3637

@@ -42,14 +43,6 @@ def cast_to_common_type(arr: ArrayLike, dtype: DtypeObj) -> ArrayLike:
4243
if is_dtype_equal(arr.dtype, dtype):
4344
return arr
4445

45-
if isinstance(dtype, np.dtype) and dtype.kind in ["i", "u"]:
46-
47-
if is_categorical_dtype(arr.dtype) and cast("Categorical", arr)._hasnans:
48-
# problem case: categorical of int -> gives int as result dtype,
49-
# but categorical can contain NAs -> float64 instead
50-
# GH#45359
51-
dtype = np.dtype(np.float64)
52-
5346
if is_sparse(arr) and not is_sparse(dtype):
5447
# problem case: SparseArray.astype(dtype) doesn't follow the specified
5548
# dtype exactly, but converts this to Sparse[dtype] -> first manually
@@ -121,6 +114,7 @@ def is_nonempty(x) -> bool:
121114
# for axis=0
122115
if not single_dtype:
123116
target_dtype = find_common_type([x.dtype for x in to_concat])
117+
target_dtype = common_dtype_categorical_compat(to_concat, target_dtype)
124118
to_concat = [cast_to_common_type(arr, target_dtype) for arr in to_concat]
125119

126120
if isinstance(to_concat[0], ABCExtensionArray):

0 commit comments

Comments
 (0)