-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Bug in loc raised Error when non-integer slice was given for MultiIndex #37707
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
Changes from 13 commits
c8df1c8
9e62c50
8377a79
e6acaac
37fe677
550ac88
d38c9e7
72345a4
04056ae
41dbbcc
07f13f3
adaa27b
061468c
d59e7f7
d9562c4
c382af0
694e469
d78d8ce
d9a4054
c13da44
0a6cab7
4ffe46e
3e9baa6
c579c88
0be97cd
923610e
fdd170e
3f3c133
4c17aeb
a766f98
48424fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2676,10 +2676,15 @@ def _partial_tup_index(self, tup, side="left"): | |
return start + section.searchsorted(loc, side=side) | ||
|
||
idx = self._get_loc_single_level_index(lev, lab) | ||
if k < n - 1: | ||
if isinstance(idx, slice) and k < n - 1: | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
start = idx.start | ||
end = idx.stop | ||
elif k < n - 1: | ||
end = start + section.searchsorted(idx, side="right") | ||
start = start + section.searchsorted(idx, side="left") | ||
else: | ||
if isinstance(idx, slice): | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you make this an if/else and then an else case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This way? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. umm sort of, L2688 should be an elif on L2687 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, should be ok now |
||
idx = idx.start | ||
return start + section.searchsorted(idx, side=side) | ||
|
||
def _get_loc_single_level_index(self, level_index: Index, key: Hashable) -> int: | ||
|
@@ -3048,22 +3053,25 @@ def convert_indexer(start, stop, step, indexer=indexer, codes=level_codes): | |
|
||
else: | ||
|
||
code = self._get_loc_single_level_index(level_index, key) | ||
idx = self._get_loc_single_level_index(level_index, key) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. side-note: _get_loc_single_level_index looks sketchy. if level_index contains NA values, we should probably be doing get_loc and not returning -1. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will look into this as a follow up There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have looked into this. Problem is, we are taking an index level from the MultiIndex, which does not contain the nans, so we will always get a KeyError, even if the original MultiIndex had nan in this level. |
||
|
||
if level > 0 or self.lexsort_depth == 0: | ||
# Desired level is not sorted | ||
locs = np.array(level_codes == code, dtype=bool, copy=False) | ||
locs = np.array(level_codes == idx, dtype=bool, copy=False) | ||
if not locs.any(): | ||
# The label is present in self.levels[level] but unused: | ||
raise KeyError(key) | ||
return locs | ||
|
||
i = level_codes.searchsorted(code, side="left") | ||
j = level_codes.searchsorted(code, side="right") | ||
if i == j: | ||
if isinstance(idx, slice): | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
start = idx.start | ||
end = idx.stop | ||
else: | ||
start = level_codes.searchsorted(idx, side="left") | ||
end = level_codes.searchsorted(idx, side="right") | ||
if start == end: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blank line here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
# The label is present in self.levels[level] but unused: | ||
raise KeyError(key) | ||
return slice(i, j) | ||
return slice(start, end) | ||
|
||
def get_locs(self, seq): | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"non-integer :class:`Interval`"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx, changed