Skip to content

Commit a95ce63

Browse files
[Backport #14551] PERF: casting loc to labels dtype before searchsorted (#14551)
(cherry picked from commit 1d95179)
1 parent 4c42422 commit a95ce63

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

doc/source/whatsnew/v0.19.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Performance Improvements
2424
- Improved performance in ``.to_json()`` when ``lines=True`` (:issue:`14408`)
2525
- Improved performance in ``Series.asof(where)`` when ``where`` is a scalar (:issue:`14461)
2626
- Improved performance in ``DataFrame.asof(where)`` when ``where`` is a scalar (:issue:`14461)
27-
27+
- Improved performance in certain types of `loc` indexing with a MultiIndex (:issue:`14551`).
2828

2929

3030

pandas/indexes/multi.py

+7
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,13 @@ def convert_indexer(start, stop, step, indexer=indexer, labels=labels):
19071907
return np.array(labels == loc, dtype=bool)
19081908
else:
19091909
# sorted, so can return slice object -> view
1910+
try:
1911+
loc = labels.dtype.type(loc)
1912+
except TypeError:
1913+
# this occurs when loc is a slice (partial string indexing)
1914+
# but the TypeError raised by searchsorted in this case
1915+
# is catched in Index._has_valid_type()
1916+
pass
19101917
i = labels.searchsorted(loc, side='left')
19111918
j = labels.searchsorted(loc, side='right')
19121919
return slice(i, j)

0 commit comments

Comments
 (0)