Skip to content

REF: implement MultiIndex._get_indexer_level_0 #42480

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 12, 2021
Merged
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
28 changes: 16 additions & 12 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
from pandas.core.arrays import Categorical
from pandas.core.arrays.categorical import factorize_from_iterables
import pandas.core.common as com
from pandas.core.indexers import is_empty_indexer
import pandas.core.indexes.base as ibase
from pandas.core.indexes.base import (
Index,
Expand Down Expand Up @@ -2559,24 +2558,29 @@ def _convert_listlike_indexer(self, keyarr) -> np.ndarray | None:

# are we indexing a specific level
if len(keyarr) and not isinstance(keyarr[0], tuple):
_, indexer = self.reindex(keyarr, level=0)

# take all
if indexer is None:
indexer = np.arange(len(self), dtype=np.intp)
return indexer

check = self.levels[0].get_indexer(keyarr)
mask = check == -1
indexer = self._get_indexer_level_0(keyarr)
mask = indexer == -1
if mask.any():
raise KeyError(f"{keyarr[mask]} not in index")
elif is_empty_indexer(indexer, keyarr):
check = self.levels[0].get_indexer(keyarr)
cmask = check == -1
if cmask.any():
raise KeyError(f"{keyarr[cmask]} not in index")
# We get here when levels still contain values which are not
# actually in Index anymore
raise KeyError(f"{keyarr} not in index")

return indexer

def _get_indexer_level_0(self, target) -> np.ndarray:
"""
Optimized equivalent to `self.get_level_values(0).get_indexer_for(target)`.
"""
lev = self.levels[0]
codes = self._codes[0]
cat = Categorical.from_codes(codes=codes, categories=lev)
ci = Index(cat)
return ci.get_indexer_for(target)

def _get_partial_string_timestamp_match_key(self, key):
"""
Translate any partial string timestamp matches in key, returning the
Expand Down