1616from pandas .util ._exceptions import find_stack_level
1717
1818from 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+ )
2023from pandas .core .dtypes .common import (
21- is_categorical_dtype ,
2224 is_dtype_equal ,
2325 is_sparse ,
2426)
3032)
3133
3234if 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