Skip to content

Bug in loc did not raise KeyError when missing combination with slice(None) was given #37764

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 9 commits into from
Nov 21, 2020

Conversation

phofl
Copy link
Member

@phofl phofl commented Nov 11, 2020

Additional question: When an non existent key is given for get_locs it raises a KeyError. When just a missing combination of keys is given ((b,1) in the new test), while the individual keys exist, an empty array is returned. Is this the expected behavior?

@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Nov 11, 2020
@@ -1107,6 +1107,8 @@ def _getitem_axis(self, key, axis: int):
# nested tuple slicing
if is_nested_tuple(key, labels):
locs = labels.get_locs(key)
if not len(locs):
raise KeyError(key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is going to raise (not 100% sure, but leaning towards yes), i think it should happen in MultiIndex.get_locs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could not find any information in the docs, if get_locs should raise when the combination of both keys is not found in MultiIndex. Would prefer that too, but did not want to break anything, hence my question above. Will try to introduce this there

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are raising in get_locs now. This should be much more stable

@phofl
Copy link
Member Author

phofl commented Nov 12, 2020

...

Edit: Did not think this through.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm ping on green.

)
df = DataFrame(np.random.rand(4, 3), index=mi)
msg = r"\('b', '1', slice\(None, None, None\)\)"
with pytest.raises(KeyError, match=msg):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add the case where the 3rd level is level out (should also raise)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jreback jreback added this to the 1.2 milestone Nov 14, 2020
@jreback
Copy link
Contributor

jreback commented Nov 14, 2020

lgtm @jbrockmendel if any comments.

@jbrockmendel
Copy link
Member

LGTM

Thanks for tackling all these indexing issues @phofl, they can be a real doozy. FWIW we've done a lot of semi-recent cleanup of the indexing code but MultiIndex has received relatively little attention. Probably some relatively low-hanging fruit there.

@phofl
Copy link
Member Author

phofl commented Nov 14, 2020

Thanks,

I will have a look for the MultiIndex code paths

@jbrockmendel
Copy link
Member

@phofl can you merge master, the CI should be fixed

� Conflicts:
�	doc/source/whatsnew/v1.2.0.rst
� Conflicts:
�	doc/source/whatsnew/v1.2.0.rst
@phofl
Copy link
Member Author

phofl commented Nov 20, 2020

Merged master

@jreback jreback merged commit b617bee into pandas-dev:master Nov 21, 2020
@jreback
Copy link
Contributor

jreback commented Nov 21, 2020

thanks @phofl

@phofl phofl deleted the 19556 branch November 21, 2020 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent MultiIndex behaviour when indexing selects no rows
3 participants