-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: DataFrame.loc silently drops non-existent elements when using MultiIndex #10549
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
xref to #6699 I am not sure how you can know when to raise an error. This comes back to the is this a reindex or a lookup issue. IMHO this would be unexpected to the user if they had a long list of values that they are looking up, to have a As discussed we are currently in a consistent state (meaning for getting & setting). So this comes down to are Further though I think this could be communicated to the users, how disruptive would this be. We certainly don't want. |
This came out of our discussions at the SciPy sprints. IMO this is different than reindexing and filling with NaN. For reindexing, we don't fail silently -- we insert NaN. If we can't do that, it is better to raise. I do agree that this is part of a larger discussion about how to handle indexing fallbacks. I think removing indexing fallbacks should be a top priority for pandas 1.0. Currently the indexing code is nigh unmaintainable. |
I don't think this is a bug. The behaviour is undocumented, true, but it is coherent with the behaviour on lists of labels, which instead is clearly documented ("raise only if no label is found"). Moreover, there is not specific "shape of the dataframe that is returned" that you would expect without knowing what you're indexing on. You seem to be implying that, for instance, pd.Series(range(5), index=pd.MultiIndex.from_arrays([[1,1,2,2,2], ['a', 'b', 'a', 'b', 'c']])).loc[[1, 2], ['c']] should be returning 1 c NaN
2 c 4
dtype: int64 rather than the current 2 c 4
dtype: int64 (all labels are present in the index, but not all their combinations), but this would be really unexpected - why should I get a The desired behaviour can easily be obtained with |
This is still not raising. Was this done on purpose, that
|
Yes, it is intended - although other cases are still waiting for a fix: #20916 (comment) By the way: #20916 is probably a duplicate of this - that is explicitly about partial indexing, but this one also has partial indexing as examples, and #20770 already solved non-partial indexing. |
Closed by #42351. |
So here's my setup (using pandas 0.16.2):
I recently found that I can select multiple levels by indexing with a tuple of tuples
Or even select at multiple depths of levels
The issue is this: if I add any levels to the index tuple that don't exist in the dataframe, pandas drops them silently
It seems to me like this should raise an exception since
The text was updated successfully, but these errors were encountered: