Skip to content

CLN: reuse common_dtype_categorical_compat #45401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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