Skip to content

Commit 2ab3b07

Browse files
author
wdyy20041223
committed
TST: Add regression tests for enlarging MultiIndex with None keys (GH#59153)
1 parent 00a7c41 commit 2ab3b07

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

pandas/tests/indexing/multiindex/test_setitem.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,24 @@ def test_frame_setitem_partial_multiindex():
533533
expected = df.copy()
534534
expected["d"] = 8
535535
tm.assert_frame_equal(result, expected)
536+
537+
def test_setitem_enlargement_multiindex_with_none(self):
538+
# GH#59153
539+
# test enlarge a DataFrame with a MultiIndex
540+
index = MultiIndex.from_tuples(
541+
[("A", "a1"), ("A", "a2"), ("B", "b1"), ("B", None)]
542+
)
543+
df = DataFrame([(0.0, 6.0), (1.0, 5.0), (2.0, 4.0), (3.0, 7.0)], index=index)
544+
df.loc[("A", None), :] = [12.0, 13.0]
545+
expected_index = MultiIndex.from_tuples(
546+
[("A", "a1"), ("A", "a2"), ("B", "b1"), ("B", None), ("A", None)]
547+
)
548+
expected = DataFrame(
549+
[[0.0, 6.0], [1.0, 5.0], [2.0, 4.0], [3.0, 7.0], [12.0, 13.0]],
550+
index=expected_index,
551+
columns=[0, 1],
552+
)
553+
tm.assert_frame_equal(df, expected, check_index_type=False)
554+
result = df.loc[("A", None), :]
555+
expected_row = Series([12.0, 13.0], index=[0, 1], name=("A", np.nan))
556+
tm.assert_series_equal(result, expected_row)

0 commit comments

Comments
 (0)