-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
.loc sometimes raises KeyError without an error message when called on an unsorted MultiIndex DataFrame #12660
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
Comments
hmm, yeah ideally this could be done with a helpful message. pull-requests are welcome! its actually not that deep in the code. just keep stepping thru. |
prob can start this with #11897 to make a useful exception (it still inherits from |
don't wait for the other change as its orthogonal |
Ok, I'm on it, will send a PR soon |
This seems to be fixed (the example works fine). |
can u put up this as a validation test |
Hello,
I know it is well documented that MultiIndex DataFrames need to be sorted to use slicing, and that is fine. Even if you forget this, in most cases (for example when using .loc with a slicer) Pandas gives a helpful error message when you try to call it on an unsorted DataFrame, which makes it easy to spot the mistake and add the necessary sorting. However when simply using .loc without a slicer, the same KeyError exception is raised without an error message, which looks like as if it was a legit key error.
Code Sample, a copy-pastable example if possible
Create a test DataFrame
Try to call .loc without a slicer
Make the same call after setting the sortlevel
Expected Output
The same helpful error message, regardless of using or not using an explicit slicer in the .loc query.
output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: