From 7d84878b84dd7f1ce346af783987eaf721185647 Mon Sep 17 00:00:00 2001 From: shteken Date: Sun, 30 Apr 2023 11:03:31 +0200 Subject: [PATCH 1/2] add test_multiindex_with_na --- pandas/tests/test_multilevel.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index 199307dd138ca..7ae4411754d7b 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -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) + + df.at[("A", "F"), "col_2"] = 0.0 + + assert df.loc[("A", "F")]["col_2"] == 0.0 + assert pd.isna(df.loc[("A", "F")]["col_1"]) + class TestSorted: """everything you wanted to test about sorting""" From 7c2a5d1e888864258afef4e504d979c4bcc19005 Mon Sep 17 00:00:00 2001 From: shteken Date: Tue, 2 May 2023 18:29:48 +0200 Subject: [PATCH 2/2] address comments in pr --- pandas/tests/test_multilevel.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index 7ae4411754d7b..8c5f9a894f2f7 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -298,13 +298,21 @@ def test_multiindex_with_na(self): ["A", "D", 9.87, 10.54], ], columns=["pivot_0", "pivot_1", "col_1", "col_2"], - ) - df.set_index(["pivot_0", "pivot_1"], inplace=True) + ).set_index(["pivot_0", "pivot_1"]) df.at[("A", "F"), "col_2"] = 0.0 - assert df.loc[("A", "F")]["col_2"] == 0.0 - assert pd.isna(df.loc[("A", "F")]["col_1"]) + expected = DataFrame( + [ + ["A", np.nan, 1.23, 4.56], + ["A", "G", 1.23, 4.56], + ["A", "D", 9.87, 10.54], + ["A", "F", np.nan, 0.0], + ], + columns=["pivot_0", "pivot_1", "col_1", "col_2"], + ).set_index(["pivot_0", "pivot_1"]) + + tm.assert_frame_equal(df, expected) class TestSorted: