Skip to content

Commit e821e1b

Browse files
authored
Backport PR #53343: REF: Use np.result_type instead of np.find_common_type (#53359)
* Backport PR #53343: REF: Use np.result_type instead of np.find_common_type * Update pandas/core/internals/concat.py * Update pandas/core/arrays/sparse/dtype.py * Update pandas/core/dtypes/concat.py
1 parent 2b012f0 commit e821e1b

File tree

6 files changed

+39
-8
lines changed

6 files changed

+39
-8
lines changed

pandas/core/algorithms.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from pandas.core.dtypes.cast import (
3636
construct_1d_object_array_from_listlike,
3737
infer_dtype_from_array,
38+
np_find_common_type,
3839
)
3940
from pandas.core.dtypes.common import (
4041
ensure_float64,
@@ -522,7 +523,7 @@ def f(c, v):
522523
f = np.in1d
523524

524525
else:
525-
common = np.find_common_type([values.dtype, comps_array.dtype], [])
526+
common = np_find_common_type(values.dtype, comps_array.dtype)
526527
values = values.astype(common, copy=False)
527528
comps_array = comps_array.astype(common, copy=False)
528529
f = htable.ismember

pandas/core/arrays/sparse/dtype.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ def _subtype_with_str(self):
400400
def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
401401
# TODO for now only handle SparseDtypes and numpy dtypes => extend
402402
# with other compatible extension dtypes
403+
from pandas.core.dtypes.cast import np_find_common_type
404+
403405
if any(
404406
isinstance(x, ExtensionDtype) and not isinstance(x, SparseDtype)
405407
for x in dtypes
@@ -420,5 +422,5 @@ def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
420422
stacklevel=find_stack_level(),
421423
)
422424

423-
np_dtypes = [x.subtype if isinstance(x, SparseDtype) else x for x in dtypes]
424-
return SparseDtype(np.find_common_type(np_dtypes, []), fill_value=fill_value)
425+
np_dtypes = (x.subtype if isinstance(x, SparseDtype) else x for x in dtypes)
426+
return SparseDtype(np_find_common_type(*np_dtypes), fill_value=fill_value)

pandas/core/dtypes/cast.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,32 @@ def common_dtype_categorical_compat(
13681368
return dtype
13691369

13701370

1371+
def np_find_common_type(*dtypes: np.dtype) -> np.dtype:
1372+
"""
1373+
np.find_common_type implementation pre-1.25 deprecation using np.result_type
1374+
https://github.com/pandas-dev/pandas/pull/49569#issuecomment-1308300065
1375+
1376+
Parameters
1377+
----------
1378+
dtypes : np.dtypes
1379+
1380+
Returns
1381+
-------
1382+
np.dtype
1383+
"""
1384+
try:
1385+
common_dtype = np.result_type(*dtypes)
1386+
if common_dtype.kind in "mMSU":
1387+
# NumPy promotion currently (1.25) misbehaves for for times and strings,
1388+
# so fall back to object (find_common_dtype did unless there
1389+
# was only one dtype)
1390+
common_dtype = np.dtype("O")
1391+
1392+
except TypeError:
1393+
common_dtype = np.dtype("O")
1394+
return common_dtype
1395+
1396+
13711397
@overload
13721398
def find_common_type(types: list[np.dtype]) -> np.dtype:
13731399
...
@@ -1435,7 +1461,7 @@ def find_common_type(types):
14351461
if is_integer_dtype(t) or is_float_dtype(t) or is_complex_dtype(t):
14361462
return np.dtype("object")
14371463

1438-
return np.find_common_type(types, [])
1464+
return np_find_common_type(*types)
14391465

14401466

14411467
def construct_2d_arraylike_from_scalar(

pandas/core/dtypes/concat.py

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pandas.core.dtypes.cast import (
1414
common_dtype_categorical_compat,
1515
find_common_type,
16+
np_find_common_type,
1617
)
1718
from pandas.core.dtypes.common import is_dtype_equal
1819
from pandas.core.dtypes.dtypes import (
@@ -110,6 +111,8 @@ def is_nonempty(x) -> bool:
110111
# coerce to object
111112
to_concat = [x.astype("object") for x in to_concat]
112113
kinds = {"o"}
114+
else:
115+
target_dtype = np_find_common_type(*dtypes)
113116

114117
result = np.concatenate(to_concat, axis=axis)
115118
if "b" in kinds and result.dtype.kind in ["i", "u", "f"]:

pandas/core/internals/concat.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from pandas.core.dtypes.cast import (
2929
ensure_dtype_can_hold_na,
3030
find_common_type,
31+
np_find_common_type,
3132
)
3233
from pandas.core.dtypes.common import (
3334
is_1d_only_ea_dtype,
@@ -144,7 +145,7 @@ def concat_arrays(to_concat: list) -> ArrayLike:
144145
target_dtype = to_concat_no_proxy[0].dtype
145146
elif all(x.kind in ["i", "u", "b"] and isinstance(x, np.dtype) for x in dtypes):
146147
# GH#42092
147-
target_dtype = np.find_common_type(list(dtypes), [])
148+
target_dtype = np_find_common_type(*dtypes)
148149
else:
149150
target_dtype = find_common_type([arr.dtype for arr in to_concat_no_proxy])
150151

pandas/tests/dtypes/test_inference.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1012,9 +1012,7 @@ def test_maybe_convert_objects_itemsize(self, data0, data1):
10121012
data = [data0, data1]
10131013
arr = np.array(data, dtype="object")
10141014

1015-
common_kind = np.find_common_type(
1016-
[type(data0), type(data1)], scalar_types=[]
1017-
).kind
1015+
common_kind = np.result_type(type(data0), type(data1)).kind
10181016
kind0 = "python" if not hasattr(data0, "dtype") else data0.dtype.kind
10191017
kind1 = "python" if not hasattr(data1, "dtype") else data1.dtype.kind
10201018
if kind0 != "python" and kind1 != "python":

0 commit comments

Comments
 (0)