diff --git a/doc/source/whatsnew/v0.19.1.txt b/doc/source/whatsnew/v0.19.1.txt index a81ab6ed0311c..eda67bc7088f2 100644 --- a/doc/source/whatsnew/v0.19.1.txt +++ b/doc/source/whatsnew/v0.19.1.txt @@ -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`). diff --git a/pandas/indexes/multi.py b/pandas/indexes/multi.py index a9f452db69659..f9576d92d8a49 100644 --- a/pandas/indexes/multi.py +++ b/pandas/indexes/multi.py @@ -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)