Skip to content

Commit 1513f66

Browse files
author
wdyy20041223
committed
TST: Add regression tests for enlarging MultiIndex with None keys (GH#59153)
1 parent 94aa1f9 commit 1513f66

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

pandas/tests/indexing/multiindex/test_setitem.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -497,58 +497,55 @@ def test_setitem_enlargement_multiindex_with_none(self):
497497
index = MultiIndex.from_tuples(
498498
[("A", "a1"), ("A", "a2"), ("B", "b1"), ("B", None)]
499499
)
500-
df = DataFrame([(0, 6), (1, 5), (2, 4), (3, 7)], index=index)
500+
df = DataFrame([(0.0, 6.0), (1.0, 5.0), (2.0, 4.0), (3.0, 7.0)], index=index)
501501

502502
# Enlarge with a new index entry where one key is None
503-
df.loc[("A", None), :] = [12, 13]
503+
df.loc[("A", None), :] = [12.0, 13.0]
504504

505505
expected_index = MultiIndex.from_tuples(
506506
[("A", "a1"), ("A", "a2"), ("B", "b1"), ("B", None), ("A", None)]
507507
)
508508
expected = DataFrame(
509-
[[0, 6], [1, 5], [2, 4], [3, 7], [12, 13]],
509+
[[0.0, 6.0], [1.0, 5.0], [2.0, 4.0], [3.0, 7.0], [12.0, 13.0]],
510510
index=expected_index,
511511
columns=[0, 1],
512-
dtype=float,
513512
)
514513
tm.assert_frame_equal(df, expected)
515514

516515
# Test retrieval of the newly added row
517516
result = df.loc[("A", None), :]
518-
expected_row = Series([12, 13], index=[0, 1], name=("A", np.nan), dtype=float)
517+
expected_row = Series([12.0, 13.0], index=[0, 1], name=("A", np.nan))
519518
tm.assert_series_equal(result, expected_row)
520519

521520
def test_setitem_enlargement_multiindex_multiple_none(self):
522521
# GH#59153
523522
# Test enlarging with multiple None keys in different levels
524523
index = MultiIndex.from_tuples([("A", "a1"), ("B", "b1")])
525-
df = DataFrame([[1, 2], [3, 4]], index=index, columns=["x", "y"])
524+
df = DataFrame([[1.0, 2.0], [3.0, 4.0]], index=index, columns=["x", "y"])
526525

527526
# Add row with None in first level
528-
df.loc[(None, "c1"), :] = [5, 6]
527+
df.loc[(None, "c1"), :] = [5.0, 6.0]
529528

530529
# Add row with None in second level
531-
df.loc[("C", None), :] = [7, 8]
530+
df.loc[("C", None), :] = [7.0, 8.0]
532531

533-
# (None, None) case removed as requested
534532
expected_index = MultiIndex.from_tuples(
535533
[
536534
("A", "a1"),
537535
("B", "b1"),
538536
(None, "c1"),
539537
("C", None),
540-
],
541-
dtype="object"
538+
]
542539
)
543540
expected = DataFrame(
544-
[[1, 2], [3, 4], [5, 6], [7, 8]],
541+
[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0]],
545542
index=expected_index,
546543
columns=["x", "y"],
547-
dtype=float,
548544
)
549545
tm.assert_frame_equal(df, expected)
550546

551547

548+
552549
def test_frame_setitem_view_direct(multiindex_dataframe_random_data):
553550
# this works because we are modifying the underlying array
554551
# really a no-no

0 commit comments

Comments
 (0)