Skip to content

Commit c71f2ab

Browse files
mroeschkeim-vinicius
authored and
im-vinicius
committed
REF: Use np.result_type instead of np.find_common_type (pandas-dev#53343)
* REF: Use np.result_type instead of np.find_common_type * Fall back to object * Create a np_find_common_dtype * Address circular import * Use generator * Typing
1 parent 3f4c494 commit c71f2ab

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

pandas/core/algorithms.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
from pandas.util._decorators import doc
3333
from pandas.util._exceptions import find_stack_level
3434

35-
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
35+
from pandas.core.dtypes.cast import (
36+
construct_1d_object_array_from_listlike,
37+
np_find_common_type,
38+
)
3639
from pandas.core.dtypes.common import (
3740
ensure_float64,
3841
ensure_object,
@@ -518,7 +521,7 @@ def f(c, v):
518521
f = np.in1d
519522

520523
else:
521-
common = np.find_common_type([values.dtype, comps_array.dtype], [])
524+
common = np_find_common_type(values.dtype, comps_array.dtype)
522525
values = values.astype(common, copy=False)
523526
comps_array = comps_array.astype(common, copy=False)
524527
f = htable.ismember

pandas/core/dtypes/cast.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,32 @@ def common_dtype_categorical_compat(
13281328
return dtype
13291329

13301330

1331+
def np_find_common_type(*dtypes: np.dtype) -> np.dtype:
1332+
"""
1333+
np.find_common_type implementation pre-1.25 deprecation using np.result_type
1334+
https://github.com/pandas-dev/pandas/pull/49569#issuecomment-1308300065
1335+
1336+
Parameters
1337+
----------
1338+
dtypes : np.dtypes
1339+
1340+
Returns
1341+
-------
1342+
np.dtype
1343+
"""
1344+
try:
1345+
common_dtype = np.result_type(*dtypes)
1346+
if common_dtype.kind in "mMSU":
1347+
# NumPy promotion currently (1.25) misbehaves for for times and strings,
1348+
# so fall back to object (find_common_dtype did unless there
1349+
# was only one dtype)
1350+
common_dtype = np.dtype("O")
1351+
1352+
except TypeError:
1353+
common_dtype = np.dtype("O")
1354+
return common_dtype
1355+
1356+
13311357
@overload
13321358
def find_common_type(types: list[np.dtype]) -> np.dtype:
13331359
...
@@ -1395,7 +1421,7 @@ def find_common_type(types):
13951421
if t.kind in "iufc":
13961422
return np.dtype("object")
13971423

1398-
return np.find_common_type(types, [])
1424+
return np_find_common_type(*types)
13991425

14001426

14011427
def construct_2d_arraylike_from_scalar(

pandas/core/dtypes/concat.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pandas.core.dtypes.cast import (
1818
common_dtype_categorical_compat,
1919
find_common_type,
20+
np_find_common_type,
2021
)
2122
from pandas.core.dtypes.dtypes import CategoricalDtype
2223
from pandas.core.dtypes.generic import (
@@ -156,11 +157,9 @@ def _get_result_dtype(
156157
target_dtype = np.dtype(object)
157158
kinds = {"o"}
158159
else:
159-
# Argument 1 to "list" has incompatible type "Set[Union[ExtensionDtype,
160-
# Any]]"; expected "Iterable[Union[dtype[Any], None, Type[Any],
161-
# _SupportsDType[dtype[Any]], str, Tuple[Any, Union[SupportsIndex,
162-
# Sequence[SupportsIndex]]], List[Any], _DTypeDict, Tuple[Any, Any]]]"
163-
target_dtype = np.find_common_type(list(dtypes), []) # type: ignore[arg-type]
160+
# error: Argument 1 to "np_find_common_type" has incompatible type
161+
# "*Set[Union[ExtensionDtype, Any]]"; expected "dtype[Any]"
162+
target_dtype = np_find_common_type(*dtypes) # type: ignore[arg-type]
164163

165164
return any_ea, kinds, target_dtype
166165

pandas/core/dtypes/dtypes.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,8 @@ def _subtype_with_str(self):
19211921
def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
19221922
# TODO for now only handle SparseDtypes and numpy dtypes => extend
19231923
# with other compatible extension dtypes
1924+
from pandas.core.dtypes.cast import np_find_common_type
1925+
19241926
if any(
19251927
isinstance(x, ExtensionDtype) and not isinstance(x, SparseDtype)
19261928
for x in dtypes
@@ -1943,8 +1945,8 @@ def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
19431945
stacklevel=find_stack_level(),
19441946
)
19451947

1946-
np_dtypes = [x.subtype if isinstance(x, SparseDtype) else x for x in dtypes]
1947-
return SparseDtype(np.find_common_type(np_dtypes, []), fill_value=fill_value)
1948+
np_dtypes = (x.subtype if isinstance(x, SparseDtype) else x for x in dtypes)
1949+
return SparseDtype(np_find_common_type(*np_dtypes), fill_value=fill_value)
19481950

19491951

19501952
@register_extension_dtype

pandas/core/internals/array_manager.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
ensure_dtype_can_hold_na,
3030
find_common_type,
3131
infer_dtype_from_scalar,
32+
np_find_common_type,
3233
)
3334
from pandas.core.dtypes.common import (
3435
ensure_platform_int,
@@ -1409,7 +1410,7 @@ def concat_arrays(to_concat: list) -> ArrayLike:
14091410
target_dtype = to_concat_no_proxy[0].dtype
14101411
elif all(x.kind in "iub" and isinstance(x, np.dtype) for x in dtypes):
14111412
# GH#42092
1412-
target_dtype = np.find_common_type(list(dtypes), [])
1413+
target_dtype = np_find_common_type(*dtypes)
14131414
else:
14141415
target_dtype = find_common_type([arr.dtype for arr in to_concat_no_proxy])
14151416

pandas/tests/dtypes/test_inference.py

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

1000-
common_kind = np.find_common_type(
1001-
[type(data0), type(data1)], scalar_types=[]
1002-
).kind
1000+
common_kind = np.result_type(type(data0), type(data1)).kind
10031001
kind0 = "python" if not hasattr(data0, "dtype") else data0.dtype.kind
10041002
kind1 = "python" if not hasattr(data1, "dtype") else data1.dtype.kind
10051003
if kind0 != "python" and kind1 != "python":

0 commit comments

Comments
 (0)