Skip to content

PERF: casting loc to labels dtype before searchsorted #14551

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
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Performance Improvements
- Improved performance in ``.to_json()`` when ``lines=True`` (:issue:`14408`)
- Improved performance in ``Series.asof(where)`` when ``where`` is a scalar (:issue:`14461)
- Improved performance in ``DataFrame.asof(where)`` when ``where`` is a scalar (:issue:`14461)

- Improved performance in certain types of `loc` indexing with a MultiIndex (:issue:`14551`).



Expand Down
7 changes: 7 additions & 0 deletions pandas/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,13 @@ def convert_indexer(start, stop, step, indexer=indexer, labels=labels):
return np.array(labels == loc, dtype=bool)
else:
# sorted, so can return slice object -> view
try:
loc = labels.dtype.type(loc)
except TypeError:
# this occurs when loc is a slice (partial string indexing)
# but the TypeError raised by searchsorted in this case
# is catched in Index._has_valid_type()
pass
i = labels.searchsorted(loc, side='left')
j = labels.searchsorted(loc, side='right')
return slice(i, j)
Expand Down