Skip to content

TST: Add a test for multiindex df with na value and adding a new row #53010

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

Merged
merged 3 commits into from
May 3, 2023
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