Skip to content

REF: move short-circuiting up to get_indexer #42230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3077,7 +3077,7 @@ def intersection(self, other, sort=False):
return Index([], name=result_name)

elif not is_dtype_equal(self.dtype, other.dtype):
dtype = find_common_type([self.dtype, other.dtype])
dtype = self._find_common_type_compat(other)
this = self.astype(dtype, copy=False)
other = other.astype(dtype, copy=False)
return this.intersection(other, sort=sort)
Expand Down Expand Up @@ -3429,6 +3429,10 @@ def get_indexer(
ptarget, method=method, limit=limit, tolerance=tolerance
)

if is_dtype_equal(self.dtype, target.dtype) and self.equals(target):
# Only call equals if we have same dtype to avoid inference/casting
return np.arange(len(target), dtype=np.intp)

return self._get_indexer(target, method, limit, tolerance)

def _get_indexer(
Expand Down Expand Up @@ -3937,8 +3941,9 @@ def join(
return join_index, lidx, ridx

if not is_dtype_equal(self.dtype, other.dtype):
this = self.astype("O")
other = other.astype("O")
dtype = self._find_common_type_compat(other)
this = self.astype(dtype, copy=False)
other = other.astype(dtype, copy=False)
return this.join(other, how=how, return_indexers=True)

_validate_join_method(how)
Expand Down Expand Up @@ -5216,6 +5221,8 @@ def get_indexer_non_unique(self, target) -> tuple[np.ndarray, np.ndarray]:
that = target.astype(dtype, copy=False)
return this.get_indexer_non_unique(that)

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

indexer, missing = self._engine.get_indexer_non_unique(tgt_values)
Expand Down Expand Up @@ -5948,8 +5955,7 @@ def insert(self, loc: int, item) -> Index:
try:
item = self._validate_fill_value(item)
except TypeError:
inferred, _ = infer_dtype_from(item)
dtype = find_common_type([self.dtype, inferred])
dtype = self._find_common_type_compat(item)
return self.astype(dtype).insert(loc, item)

arr = np.asarray(self)
Expand Down
4 changes: 0 additions & 4 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,10 +646,6 @@ def _get_indexer(
# returned ndarray is np.intp

if isinstance(target, IntervalIndex):
# equal indexes -> 1:1 positional match
if self.equals(target):
return np.arange(len(self), dtype="intp")

if not self._should_compare(target):
return self._get_indexer_non_comparable(target, method, unique=True)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/libs/test_hashtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ def test_get_labels_groupby_for_Int64(writable):
vals = np.array([1, 2, -1, 2, 1, -1], dtype=np.int64)
vals.flags.writeable = writable
arr, unique = table.get_labels_groupby(vals)
expected_arr = np.array([0, 1, -1, 1, 0, -1], dtype=np.int64)
expected_arr = np.array([0, 1, -1, 1, 0, -1], dtype=np.intp)
expected_unique = np.array([1, 2], dtype=np.int64)
tm.assert_numpy_array_equal(arr.astype(np.int64), expected_arr)
tm.assert_numpy_array_equal(arr, expected_arr)
tm.assert_numpy_array_equal(unique, expected_unique)


Expand Down