Skip to content

Commit 568b56b

Browse files
authored
REF: move short-circuiting up to get_indexer (#42230)
1 parent 77f15dc commit 568b56b

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

pandas/core/indexes/base.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -3077,7 +3077,7 @@ def intersection(self, other, sort=False):
30773077
return Index([], name=result_name)
30783078

30793079
elif not is_dtype_equal(self.dtype, other.dtype):
3080-
dtype = find_common_type([self.dtype, other.dtype])
3080+
dtype = self._find_common_type_compat(other)
30813081
this = self.astype(dtype, copy=False)
30823082
other = other.astype(dtype, copy=False)
30833083
return this.intersection(other, sort=sort)
@@ -3429,6 +3429,10 @@ def get_indexer(
34293429
ptarget, method=method, limit=limit, tolerance=tolerance
34303430
)
34313431

3432+
if is_dtype_equal(self.dtype, target.dtype) and self.equals(target):
3433+
# Only call equals if we have same dtype to avoid inference/casting
3434+
return np.arange(len(target), dtype=np.intp)
3435+
34323436
return self._get_indexer(target, method, limit, tolerance)
34333437

34343438
def _get_indexer(
@@ -3951,8 +3955,9 @@ def join(
39513955
return join_index, lidx, ridx
39523956

39533957
if not is_dtype_equal(self.dtype, other.dtype):
3954-
this = self.astype("O")
3955-
other = other.astype("O")
3958+
dtype = self._find_common_type_compat(other)
3959+
this = self.astype(dtype, copy=False)
3960+
other = other.astype(dtype, copy=False)
39563961
return this.join(other, how=how, return_indexers=True)
39573962

39583963
_validate_join_method(how)
@@ -5230,6 +5235,8 @@ def get_indexer_non_unique(self, target) -> tuple[np.ndarray, np.ndarray]:
52305235
that = target.astype(dtype, copy=False)
52315236
return this.get_indexer_non_unique(that)
52325237

5238+
# Note: _maybe_promote ensures we never get here with MultiIndex
5239+
# self and non-Multi target
52335240
tgt_values = target._get_engine_target()
52345241

52355242
indexer, missing = self._engine.get_indexer_non_unique(tgt_values)
@@ -5962,8 +5969,7 @@ def insert(self, loc: int, item) -> Index:
59625969
try:
59635970
item = self._validate_fill_value(item)
59645971
except TypeError:
5965-
inferred, _ = infer_dtype_from(item)
5966-
dtype = find_common_type([self.dtype, inferred])
5972+
dtype = self._find_common_type_compat(item)
59675973
return self.astype(dtype).insert(loc, item)
59685974

59695975
arr = np.asarray(self)

pandas/core/indexes/interval.py

-4
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,6 @@ def _get_indexer(
646646
# returned ndarray is np.intp
647647

648648
if isinstance(target, IntervalIndex):
649-
# equal indexes -> 1:1 positional match
650-
if self.equals(target):
651-
return np.arange(len(self), dtype="intp")
652-
653649
if not self._should_compare(target):
654650
return self._get_indexer_non_comparable(target, method, unique=True)
655651

pandas/tests/libs/test_hashtable.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,9 @@ def test_get_labels_groupby_for_Int64(writable):
252252
vals = np.array([1, 2, -1, 2, 1, -1], dtype=np.int64)
253253
vals.flags.writeable = writable
254254
arr, unique = table.get_labels_groupby(vals)
255-
expected_arr = np.array([0, 1, -1, 1, 0, -1], dtype=np.int64)
255+
expected_arr = np.array([0, 1, -1, 1, 0, -1], dtype=np.intp)
256256
expected_unique = np.array([1, 2], dtype=np.int64)
257-
tm.assert_numpy_array_equal(arr.astype(np.int64), expected_arr)
257+
tm.assert_numpy_array_equal(arr, expected_arr)
258258
tm.assert_numpy_array_equal(unique, expected_unique)
259259

260260

0 commit comments

Comments
 (0)