16
16
from pandas .util ._exceptions import find_stack_level
17
17
18
18
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
+ )
20
23
from pandas .core .dtypes .common import (
21
- is_categorical_dtype ,
22
24
is_dtype_equal ,
23
25
is_sparse ,
24
26
)
30
32
)
31
33
32
34
if TYPE_CHECKING :
33
- from pandas import Categorical
34
35
from pandas .core .arrays .sparse import SparseArray
35
36
36
37
@@ -42,14 +43,6 @@ def cast_to_common_type(arr: ArrayLike, dtype: DtypeObj) -> ArrayLike:
42
43
if is_dtype_equal (arr .dtype , dtype ):
43
44
return arr
44
45
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
-
53
46
if is_sparse (arr ) and not is_sparse (dtype ):
54
47
# problem case: SparseArray.astype(dtype) doesn't follow the specified
55
48
# dtype exactly, but converts this to Sparse[dtype] -> first manually
@@ -121,6 +114,7 @@ def is_nonempty(x) -> bool:
121
114
# for axis=0
122
115
if not single_dtype :
123
116
target_dtype = find_common_type ([x .dtype for x in to_concat ])
117
+ target_dtype = common_dtype_categorical_compat (to_concat , target_dtype )
124
118
to_concat = [cast_to_common_type (arr , target_dtype ) for arr in to_concat ]
125
119
126
120
if isinstance (to_concat [0 ], ABCExtensionArray ):
0 commit comments