-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
BUG: loc dropping levels when df has only one row #38150
Changes from 12 commits
bef2819
7408464
da86c93
4d9272d
3f54db9
d49e512
7a1c22a
8517274
ee106a9
693038f
e87865c
68987cd
04bfc9b
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 |
---|---|---|
|
@@ -695,3 +695,17 @@ def test_loc_getitem_index_differently_ordered_slice_none(): | |
columns=["a", "b"], | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_loc_getitem_drops_levels_for_one_row_dataframe(): | ||
# GH#10521 | ||
mi = MultiIndex.from_arrays([["x"], ["y"], ["z"]], names=["a", "b", "c"]) | ||
df = DataFrame({"d": [0]}, index=mi) | ||
expected = df.copy() | ||
result = df.loc["x", :, "z"] | ||
tm.assert_frame_equal(result, expected) | ||
|
||
ser = Series([0], index=mi) | ||
result = ser.loc["x", :, "z"] | ||
expected = Series([0], index=Index(["y"], name="b")) | ||
tm.assert_series_equal(result, expected) | ||
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. big picture, why isnt 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. Series drops levels of MultiIndex (for example #6022), while DataFrame should keep them. The current behavior is, that DataFrame keeps them all the times except when it has one row. 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. is #38150 (comment) saying that you plant to change the Series behavior to match DataFrame? 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. No, the comment was meant to clarify why we have to check for ndim==1 |
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.
self.ndim == 1
is faster than this isinstance check (41 ns vs 297 ns, so not a huge deal either way)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.
Is better to read too :)