Skip to content

REF: disambiguate get_loc_level k variable #42378

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 2 commits into from
Jul 7, 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
18 changes: 6 additions & 12 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3765,18 +3765,12 @@ class animal locomotion
self._consolidate_inplace()

if isinstance(index, MultiIndex):
try:
loc, new_index = index._get_loc_level(key, level=0)
except TypeError as err:
raise TypeError(
f"Expected label or tuple of labels, got {key}"
) from err
else:
if not drop_level:
if lib.is_integer(loc):
new_index = index[loc : loc + 1]
else:
new_index = index[loc]
loc, new_index = index._get_loc_level(key, level=0)
if not drop_level:
if lib.is_integer(loc):
new_index = index[loc : loc + 1]
else:
new_index = index[loc]
else:
loc = index.get_loc(key)

Expand Down
29 changes: 18 additions & 11 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2988,6 +2988,7 @@ def maybe_mi_droplevels(indexer, levels):
if isinstance(key, tuple) and level == 0:

try:
# Check if this tuple is a single key in our first level
if key in self.levels[0]:
indexer = self._get_level_indexer(key, level=level)
new_index = maybe_mi_droplevels(indexer, [0])
Expand Down Expand Up @@ -3021,19 +3022,25 @@ def maybe_mi_droplevels(indexer, levels):
indexer = None
for i, k in enumerate(key):
if not isinstance(k, slice):
k = self._get_level_indexer(k, level=i)
if isinstance(k, slice):
# everything
if k.start == 0 and k.stop == len(self):
k = slice(None, None)
loc_level = self._get_level_indexer(k, level=i)
if isinstance(loc_level, slice):
if com.is_null_slice(loc_level) or com.is_full_slice(
loc_level, len(self)
):
# everything
continue
else:
raise TypeError(
f"Expected label or tuple of labels, got {key}"
)
else:
k_index = k
k_index = loc_level

if isinstance(k, slice):
if k == slice(None, None):
continue
else:
raise TypeError(key)
elif com.is_null_slice(k):
# taking everything, does not affect `indexer` below
continue
else:
raise TypeError(f"Expected label or tuple of labels, got {key}")

if indexer is None:
indexer = k_index
Expand Down