Skip to content

Commit 155e572

Browse files
Change find_result_type to take a dtype instead of values
1 parent 80ba8b5 commit 155e572

File tree

6 files changed

+27
-18
lines changed

6 files changed

+27
-18
lines changed

doc/source/whatsnew/v2.0.2.rst

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ Bug fixes
4646

4747
Other
4848
~~~~~
49-
- Improved performance when passing an array to :meth:`RangeIndex.take`, :meth:`DataFrame.loc`, or :meth:`DataFrame.iloc` and the DataFrame is using a RangeIndex (:issue:`53387`)
5049
- Raised a better error message when calling :func:`Series.dt.to_pydatetime` with :class:`ArrowDtype` with ``pyarrow.date32`` or ``pyarrow.date64`` type (:issue:`52812`)
5150

5251
.. ---------------------------------------------------------------------------

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ Performance improvements
351351
- Performance improvement in :meth:`~arrays.ArrowExtensionArray.to_numpy` (:issue:`52525`)
352352
- Performance improvement when doing various reshaping operations on :class:`arrays.IntegerArrays` & :class:`arrays.FloatingArray` by avoiding doing unnecessary validation (:issue:`53013`)
353353
- Performance improvement when indexing with pyarrow timestamp and duration dtypes (:issue:`53368`)
354+
- Performance improvement when passing an array to :meth:`RangeIndex.take`, :meth:`DataFrame.loc`, or :meth:`DataFrame.iloc` and the DataFrame is using a RangeIndex (:issue:`53387`)
354355
-
355356

356357
.. ---------------------------------------------------------------------------

pandas/core/dtypes/cast.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -1258,17 +1258,17 @@ def _ensure_nanosecond_dtype(dtype: DtypeObj) -> None:
12581258

12591259
# TODO: other value-dependent functions to standardize here include
12601260
# Index._find_common_type_compat
1261-
def find_result_type(left: ArrayLike, right: Any) -> DtypeObj:
1261+
def find_result_type(left_dtype: DtypeObj, right: Any) -> DtypeObj:
12621262
"""
1263-
Find the type/dtype for a the result of an operation between these objects.
1263+
Find the type/dtype for the result of an operation between objects.
12641264
1265-
This is similar to find_common_type, but looks at the objects instead
1266-
of just their dtypes. This can be useful in particular when one of the
1267-
objects does not have a `dtype`.
1265+
This is similar to find_common_type, but looks at the right object instead
1266+
of just its dtype. This can be useful in particular when the right
1267+
object does not have a `dtype`.
12681268
12691269
Parameters
12701270
----------
1271-
left : np.ndarray or ExtensionArray
1271+
left_dtype : np.dtype or ExtensionDtype
12721272
right : Any
12731273
12741274
Returns
@@ -1283,26 +1283,24 @@ def find_result_type(left: ArrayLike, right: Any) -> DtypeObj:
12831283
new_dtype: DtypeObj
12841284

12851285
if (
1286-
isinstance(left, np.ndarray)
1287-
and left.dtype.kind in "iuc"
1286+
isinstance(left_dtype, np.dtype)
1287+
and left_dtype.kind in "iuc"
12881288
and (lib.is_integer(right) or lib.is_float(right))
12891289
):
12901290
# e.g. with int8 dtype and right=512, we want to end up with
12911291
# np.int16, whereas infer_dtype_from(512) gives np.int64,
12921292
# which will make us upcast too far.
1293-
if lib.is_float(right) and right.is_integer() and left.dtype.kind != "f":
1293+
if lib.is_float(right) and right.is_integer() and left_dtype.kind != "f":
12941294
right = int(right)
1295+
new_dtype = np.result_type(left_dtype, right)
12951296

1296-
new_dtype = np.result_type(left, right)
1297-
1298-
elif is_valid_na_for_dtype(right, left.dtype):
1297+
elif is_valid_na_for_dtype(right, left_dtype):
12991298
# e.g. IntervalDtype[int] and None/np.nan
1300-
new_dtype = ensure_dtype_can_hold_na(left.dtype)
1299+
new_dtype = ensure_dtype_can_hold_na(left_dtype)
13011300

13021301
else:
13031302
dtype, _ = infer_dtype_from(right)
1304-
1305-
new_dtype = find_common_type([left.dtype, dtype])
1303+
new_dtype = find_common_type([left_dtype, dtype])
13061304

13071305
return new_dtype
13081306

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6276,7 +6276,7 @@ def _find_common_type_compat(self, target) -> DtypeObj:
62766276
):
62776277
return _dtype_obj
62786278

6279-
dtype = find_result_type(self._values, target)
6279+
dtype = find_result_type(self.dtype, target)
62806280
dtype = common_dtype_categorical_compat([self, target], dtype)
62816281
return dtype
62826282

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def coerce_to_target_dtype(self, other) -> Block:
423423
we can also safely try to coerce to the same dtype
424424
and will receive the same block
425425
"""
426-
new_dtype = find_result_type(self.values, other)
426+
new_dtype = find_result_type(self.values.dtype, other)
427427

428428
return self.astype(new_dtype, copy=False)
429429

pandas/tests/indexes/numeric/test_setops.py

+11
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ def test_range_float_union_dtype(self):
7474
result = other.union(index)
7575
tm.assert_index_equal(result, expected)
7676

77+
def test_range_uint64_union_dtype(self):
78+
# https://github.com/pandas-dev/pandas/issues/26778
79+
index = RangeIndex(start=0, stop=3)
80+
other = Index([0, 10], dtype=np.uint64)
81+
result = index.union(other)
82+
expected = Index([0, 1, 2, 10], dtype=object)
83+
tm.assert_index_equal(result, expected)
84+
85+
result = other.union(index)
86+
tm.assert_index_equal(result, expected)
87+
7788
def test_float64_index_difference(self):
7889
# https://github.com/pandas-dev/pandas/issues/35217
7990
float_index = Index([1.0, 2, 3])

0 commit comments

Comments
 (0)