Skip to content

Commit f451617

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

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

pandas/tests/indexing/multiindex/test_setitem.py

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

493+
def test_setitem_enlargement_multiindex_with_none(self):
494+
# GH#59153
495+
# enlarging a DataFrame with a MultiIndex containing None values
496+
index = MultiIndex.from_tuples(
497+
[("A", "a1"), ("A", "a2"), ("B", "b1"), ("B", None)]
498+
)
499+
df = DataFrame([(0.0, 6.0), (1.0, 5.0), (2.0, 4.0), (3.0, 7.0)], index=index)
500+
df.loc[("A", None), :] = [12.0, 13.0]
501+
expected_index = MultiIndex.from_tuples(
502+
[("A", "a1"), ("A", "a2"), ("B", "b1"), ("B", None), ("A", None)]
503+
)
504+
expected = DataFrame(
505+
[[0.0, 6.0], [1.0, 5.0], [2.0, 4.0], [3.0, 7.0], [12.0, 13.0]],
506+
index=expected_index,
507+
columns=[0, 1],
508+
)
509+
tm.assert_frame_equal(df, expected, check_index_type=False)
510+
result = df.loc[("A", None), :]
511+
expected_row = Series([12.0, 13.0], index=[0, 1], name=("A", np.nan))
512+
tm.assert_series_equal(result, expected_row)
513+
493514

494515
def test_frame_setitem_view_direct(multiindex_dataframe_random_data):
495516
# this works because we are modifying the underlying array
@@ -534,23 +555,3 @@ def test_frame_setitem_partial_multiindex():
534555
expected["d"] = 8
535556
tm.assert_frame_equal(result, expected)
536557

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)