-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: loc with empty multiindex raises exception #38711
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
Changes from 2 commits
4b8e90d
45982fd
4e03099
c1931f7
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,22 @@ def test_loc_getitem_index_differently_ordered_slice_none(): | |
columns=["a", "b"], | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_loc_empty_multiindex(): | ||
# GH#36936 | ||
arrays = [["a", "a", "b", "a"], ["a", "a", "b", "b"]] | ||
index = MultiIndex.from_arrays(arrays, names=("idx1", "idx2")) | ||
df = DataFrame([1, 2, 3, 4], index=index, columns=["value"]) | ||
|
||
# loc on empty multiindex == loc with False mask | ||
empty_multiindex = df.loc[df.loc[:, "value"] == 0, :].index | ||
result = df.loc[empty_multiindex, :] | ||
expected = df.loc[[False] * len(df.index), :] | ||
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. can you construct the expected frame explicityly 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. Constructing an empty DataFrame named I used the following code to create an empty dataframe with multiindex: def test_loc_empty_multiindex():
# GH#36936
arrays = [["a", "a", "b", "a"], ["a", "a", "b", "b"]]
index = MultiIndex.from_arrays(arrays, names=("idx1", "idx2"))
df = DataFrame([1, 2, 3, 4], index=index, columns=["value"])
# loc on empty multiindex == loc with False mask
empty_multiindex = df.loc[df.loc[:, "value"] == 0, :].index
result = df.loc[empty_multiindex, :]
index2 = MultiIndex(levels=[[], []], codes=[[], []], names=["idx1", "idx2"])
expected = DataFrame([], index=index2, columns=["value"], dtype="string")
expected = expected.astype({"value": "int"})
tm.assert_frame_equal(result, expected) # this test fails
# replacing value with loc on empty multiindex
df.loc[df.loc[df.loc[:, "value"] == 0].index, "value"] = 5
result = df
expected = DataFrame([1, 2, 3, 4], index=index, columns=["value"])
tm.assert_frame_equal(result, expected) If the |
||
tm.assert_equal(result, expected) | ||
|
||
# replacing value with loc on empty multiindex | ||
df.loc[df.loc[df.loc[:, "value"] == 0].index, "value"] = 5 | ||
result = df | ||
expected = DataFrame([1, 2, 3, 4], index=index, columns=["value"]) | ||
tm.assert_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. does 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. The assertion method is changed to |
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.
can you co-locate this with similar tests
IOW there are 2 tests in the test_getitem.py that are testing basically the same, so locate there.
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.
Moved the test to pandas/tests/indexing/multiindex/test_getitem.py