Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,22 @@ def test_datetime_object_multiindex(self):

tm.assert_frame_equal(result, expected)

def test_multiindex_with_na(self):
df = DataFrame(
[
["A", np.nan, 1.23, 4.56],
["A", "G", 1.23, 4.56],
["A", "D", 9.87, 10.54],
],
columns=["pivot_0", "pivot_1", "col_1", "col_2"],
)
df.set_index(["pivot_0", "pivot_1"], inplace=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
)
df.set_index(["pivot_0", "pivot_1"], inplace=True)
).set_index(["pivot_0", "pivot_1"])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask what benefit do we gain from chaining the operations?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to avoid inplace=True where possible


df.at[("A", "F"), "col_2"] = 0.0

assert df.loc[("A", "F")]["col_2"] == 0.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of these asserts, could you construct expected = DataFrame(...) and then use tm.assert_frame_equal(df, expected)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I understand that with your suggestion it would be easier to understand the expected output.

assert pd.isna(df.loc[("A", "F")]["col_1"])


class TestSorted:
"""everything you wanted to test about sorting"""
Expand Down