-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
s.loc[[]] raises error (only) if index is not unique #13691
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
s.loc[[]]
raises error (only) if index is not unique
ok, will mark it, though non-unique multi-indexes are not very well tested, nor should they really be allowed (but they are) :< |
I have just recently run into this issue as well. I do this very often in fact, and now a lot of my code is broken. I would probably do a lot of differently now, as I've learned more about pandas, but it is what it is. (FYI, merging with df1_good with [left_index=True, right_index=True, how='inner'] produces an equivalent result) df1 = pd.DataFrame(data=dict(A=[1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2], df2 = df1.groupby(level=[0, 2])[['V']].max() df2_good = df2[df2.V > 9] ...this apparently works and returns an empty dataframe with the index labels and columns intact. ...this fails with TypeError: Cannot infer number of levels from empty list Anyhow, it's perfectly acceptable in my analysis that initial query returns an empty DataFrame. This used to work just fine. Now, any time I want to do anything remotely like this I need to always check and make sure that the result of the initial query isn't empty. This works fine in pandas 0.16, and does not in 0.17 or 0.18. I don't know exactly where it failed, but I get the same error above. |
Code Sample, a copy-pastable example if possible
Expected Output
Out[5]: Series([], dtype: int64)
By the way, on top of the inconsistency above,
In [6]: s.loc[s.iloc[0:0]]
results in the same error, which is even more unexpected because
s.iloc[0:0].index
is a perfectly validMultiIndex
, and so it is certainly possible to infer the number of levels from it.Might be related to #13490 (the error is the same, but I ignore the level at which the open PR applies).
output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: