Skip to content

Commit a0f30f6

Browse files
Change find_result_type to take a dtype instead of values
1 parent 6d07b0c commit a0f30f6

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
@@ -367,6 +367,7 @@ Performance improvements
367367
- Performance improvement in various :class:`MultiIndex` set and indexing operations (:issue:`53955`)
368368
- Performance improvement when doing various reshaping operations on :class:`arrays.IntegerArrays` & :class:`arrays.FloatingArray` by avoiding doing unnecessary validation (:issue:`53013`)
369369
- Performance improvement when indexing with pyarrow timestamp and duration dtypes (:issue:`53368`)
370+
- 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`)
370371
-
371372

372373
.. ---------------------------------------------------------------------------

pandas/core/dtypes/cast.py

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

12621262
# TODO: other value-dependent functions to standardize here include
12631263
# Index._find_common_type_compat
1264-
def find_result_type(left: ArrayLike, right: Any) -> DtypeObj:
1264+
def find_result_type(left_dtype: DtypeObj, right: Any) -> DtypeObj:
12651265
"""
1266-
Find the type/dtype for a the result of an operation between these objects.
1266+
Find the type/dtype for the result of an operation between objects.
12671267
1268-
This is similar to find_common_type, but looks at the objects instead
1269-
of just their dtypes. This can be useful in particular when one of the
1270-
objects does not have a `dtype`.
1268+
This is similar to find_common_type, but looks at the right object instead
1269+
of just its dtype. This can be useful in particular when the right
1270+
object does not have a `dtype`.
12711271
12721272
Parameters
12731273
----------
1274-
left : np.ndarray or ExtensionArray
1274+
left_dtype : np.dtype or ExtensionDtype
12751275
right : Any
12761276
12771277
Returns
@@ -1286,26 +1286,24 @@ def find_result_type(left: ArrayLike, right: Any) -> DtypeObj:
12861286
new_dtype: DtypeObj
12871287

12881288
if (
1289-
isinstance(left, np.ndarray)
1290-
and left.dtype.kind in "iuc"
1289+
isinstance(left_dtype, np.dtype)
1290+
and left_dtype.kind in "iuc"
12911291
and (lib.is_integer(right) or lib.is_float(right))
12921292
):
12931293
# e.g. with int8 dtype and right=512, we want to end up with
12941294
# np.int16, whereas infer_dtype_from(512) gives np.int64,
12951295
# which will make us upcast too far.
1296-
if lib.is_float(right) and right.is_integer() and left.dtype.kind != "f":
1296+
if lib.is_float(right) and right.is_integer() and left_dtype.kind != "f":
12971297
right = int(right)
1298+
new_dtype = np.result_type(left_dtype, right)
12981299

1299-
new_dtype = np.result_type(left, right)
1300-
1301-
elif is_valid_na_for_dtype(right, left.dtype):
1300+
elif is_valid_na_for_dtype(right, left_dtype):
13021301
# e.g. IntervalDtype[int] and None/np.nan
1303-
new_dtype = ensure_dtype_can_hold_na(left.dtype)
1302+
new_dtype = ensure_dtype_can_hold_na(left_dtype)
13041303

13051304
else:
13061305
dtype, _ = infer_dtype_from(right)
1307-
1308-
new_dtype = find_common_type([left.dtype, dtype])
1306+
new_dtype = find_common_type([left_dtype, dtype])
13091307

13101308
return new_dtype
13111309

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6293,7 +6293,7 @@ def _find_common_type_compat(self, target) -> DtypeObj:
62936293
):
62946294
return _dtype_obj
62956295

6296-
dtype = find_result_type(self._values, target)
6296+
dtype = find_result_type(self.dtype, target)
62976297
dtype = common_dtype_categorical_compat([self, target], dtype)
62986298
return dtype
62996299

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ def coerce_to_target_dtype(self, other) -> Block:
450450
we can also safely try to coerce to the same dtype
451451
and will receive the same block
452452
"""
453-
new_dtype = find_result_type(self.values, other)
453+
new_dtype = find_result_type(self.values.dtype, other)
454454

455455
return self.astype(new_dtype, copy=False)
456456

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)