Skip to content

Commit 119bf9b

Browse files
author
wdyy20041223
committed
TST: Add regression tests for enlarging MultiIndex with None keys (GH#59153)
1 parent 0627903 commit 119bf9b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

pandas/tests/indexing/multiindex/test_setitem.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,50 @@ def test_setitem_enlargement_keep_index_names(self):
490490
)
491491
tm.assert_frame_equal(df, expected)
492492

493+
def test_setitem_enlargement_with_none_key(self):
494+
# GH#59153
495+
# Test that enlarging a MultiIndex DataFrame works when one or more
496+
# level keys are None
497+
index = MultiIndex.from_tuples(
498+
[("A", "a1"), ("A", "a2"), ("B", "b1"), ("B", None)]
499+
)
500+
df = DataFrame([(0, 6), (1, 5), (2, 4), (3, 7)], index=index)
501+
502+
# Test 1: Enlarge with a new index entry where second key is None
503+
df.loc[("A", None), :] = [12, 13]
504+
expected_index = MultiIndex.from_tuples(
505+
[
506+
("A", "a1"),
507+
("A", "a2"),
508+
("B", "b1"),
509+
("B", None),
510+
("A", None),
511+
]
512+
)
513+
expected = DataFrame(
514+
[[0, 6], [1, 5], [2, 4], [3, 7], [12, 13]],
515+
index=expected_index,
516+
)
517+
tm.assert_frame_equal(df, expected)
518+
519+
# Test 2: Enlarge with None in first level key
520+
df.loc[(None, "c1"), :] = [14, 15]
521+
expected_index = MultiIndex.from_tuples(
522+
[
523+
("A", "a1"),
524+
("A", "a2"),
525+
("B", "b1"),
526+
("B", None),
527+
("A", None),
528+
(None, "c1"),
529+
]
530+
)
531+
expected = DataFrame(
532+
[[0, 6], [1, 5], [2, 4], [3, 7], [12, 13], [14, 15]],
533+
index=expected_index,
534+
)
535+
tm.assert_frame_equal(df, expected)
536+
493537

494538
def test_frame_setitem_view_direct(multiindex_dataframe_random_data):
495539
# this works because we are modifying the underlying array

0 commit comments

Comments
 (0)