Skip to content

REF: move casting from Index._get_indexer to Index.get_indexer #42308

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 1 commit into from
Jul 1, 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
21 changes: 12 additions & 9 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3462,6 +3462,18 @@ def get_indexer(
# Only call equals if we have same dtype to avoid inference/casting
return np.arange(len(target), dtype=np.intp)

if not is_dtype_equal(self.dtype, target.dtype) and not is_interval_dtype(
self.dtype
):
# IntervalIndex gets special treatment for partial-indexing
dtype = self._find_common_type_compat(target)

this = self.astype(dtype, copy=False)
target = target.astype(dtype, copy=False)
return this._get_indexer(
target, method=method, limit=limit, tolerance=tolerance
)

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

def _get_indexer(
Expand All @@ -3474,15 +3486,6 @@ def _get_indexer(
if tolerance is not None:
tolerance = self._convert_tolerance(tolerance, target)

if not is_dtype_equal(self.dtype, target.dtype):
dtype = self._find_common_type_compat(target)

this = self.astype(dtype, copy=False)
target = target.astype(dtype, copy=False)
return this.get_indexer(
target, method=method, limit=limit, tolerance=tolerance
)

if method in ["pad", "backfill"]:
indexer = self._get_fill_indexer(target, method, limit, tolerance)
elif method == "nearest":
Expand Down
4 changes: 0 additions & 4 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,6 @@ def _get_indexer(
reverse = self._range[::-1]
start, stop, step = reverse.start, reverse.stop, reverse.step

if not is_signed_integer_dtype(target):
# checks/conversions/roundings are delegated to general method
return super()._get_indexer(target, method=method, tolerance=tolerance)

target_array = np.asarray(target)
locs = target_array - start
valid = (locs % step == 0) & (locs >= 0) & (target_array < stop)
Expand Down