Skip to content

Commit

Permalink
CLN: reuse helper (pandas-dev#45401)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Jan 17, 2022
1 parent 07732d7 commit 23f12a1
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.astype import astype_array
from pandas.core.dtypes.cast import find_common_type
from pandas.core.dtypes.cast import (
common_dtype_categorical_compat,
find_common_type,
)
from pandas.core.dtypes.common import (
is_categorical_dtype,
is_dtype_equal,
is_sparse,
)
Expand All @@ -30,7 +32,6 @@
)

if TYPE_CHECKING:
from pandas import Categorical
from pandas.core.arrays.sparse import SparseArray


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

if isinstance(dtype, np.dtype) and dtype.kind in ["i", "u"]:

if is_categorical_dtype(arr.dtype) and cast("Categorical", arr)._hasnans:
# problem case: categorical of int -> gives int as result dtype,
# but categorical can contain NAs -> float64 instead
# GH#45359
dtype = np.dtype(np.float64)

if is_sparse(arr) and not is_sparse(dtype):
# problem case: SparseArray.astype(dtype) doesn't follow the specified
# dtype exactly, but converts this to Sparse[dtype] -> first manually
Expand Down Expand Up @@ -121,6 +114,7 @@ def is_nonempty(x) -> bool:
# for axis=0
if not single_dtype:
target_dtype = find_common_type([x.dtype for x in to_concat])
target_dtype = common_dtype_categorical_compat(to_concat, target_dtype)
to_concat = [cast_to_common_type(arr, target_dtype) for arr in to_concat]

if isinstance(to_concat[0], ABCExtensionArray):
Expand Down

0 comments on commit 23f12a1

Please sign in to comment.