Repository: pandas-dev/pandas
File: pandas/tests/indexing/multiindex/test_setitem.py (Python)

Code before:
            data=[[10, 20, 30], [10, 20, 30]],
            index=mi_expected,
            columns=["A", "B", "C"],
        )
        tm.assert_frame_equal(df, expected)

    def test_setitem_enlargement_multiindex_with_none(self):
        # GH#59153
        # enlarging a DataFrame with a MultiIndex containing None values
        index = MultiIndex.from_tuples(
            [("A", "a1"), ("A", "a2"), ("B", "b1"), ("B", None)]
        )
        df = DataFrame([(0.0, 6.0), (1.0, 5.0), (2.0, 4.0), (3.0, 7.0)], index=index)
        df.loc[("A", None), :] = [12.0, 13.0]
        expected_index = MultiIndex.from_tuples(
            [("A", "a1"), ("A", "a2"), ("B", "b1"), ("B", None), ("A", None)]
        )
        expected = DataFrame(
            [[0.0, 6.0], [1.0, 5.0], [2.0, 4.0], [3.0, 7.0], [12.0, 13.0]],
            index=expected_index,
            columns=[0, 1],
        )
        tm.assert_frame_equal(df, expected, check_index_type=False)
        result = df.loc[("A", None), :]
        expected_row = Series([12.0, 13.0], index=[0, 1], name=("A", np.nan))
        tm.assert_series_equal(result, expected_row)


def test_frame_setitem_view_direct(multiindex_dataframe_random_data):
    # this works because we are modifying the underlying array
    # really a no-no
    df = multiindex_dataframe_random_data.T
    with pytest.raises(ValueError, match="read-only"):
        df["foo"].values[:] = 0
    assert (df["foo"].values != 0).all()


def test_frame_setitem_copy_raises(multiindex_dataframe_random_data):
    # will raise/warn as its chained assignment
    df = multiindex_dataframe_random_data.T
    with tm.raises_chained_assignment_error():
        df["foo"]["one"] = 2


def test_frame_setitem_copy_no_write(multiindex_dataframe_random_data):
    frame = multiindex_dataframe_random_data.T
    expected = frame
    df = frame.copy()
    with tm.raises_chained_assignment_error():
        df["foo"]["one"] = 2


Review comment (suggestion):
```suggestion
```
I don't think we need to test this follow up

Code after:
            data=[[10, 20, 30], [10, 20, 30]],
            index=mi_expected,
            columns=["A", "B", "C"],
        )
        tm.assert_frame_equal(df, expected)

    def test_setitem_enlargement_multiindex_with_none(self):
        # GH#59153
        # enlarging a DataFrame with a MultiIndex containing None values
        index = MultiIndex.from_tuples(
            [("A", "a1"), ("A", "a2"), ("B", "b1"), ("B", None)]
        )
        df = DataFrame([(0.0, 6.0), (1.0, 5.0), (2.0, 4.0), (3.0, 7.0)], index=index)
        df.loc[("A", None), :] = [12.0, 13.0]
        expected_index = MultiIndex.from_tuples(
            [("A", "a1"), ("A", "a2"), ("B", "b1"), ("B", None), ("A", None)]
        )
        expected = DataFrame(
            [[0.0, 6.0], [1.0, 5.0], [2.0, 4.0], [3.0, 7.0], [12.0, 13.0]],
            index=expected_index,
            columns=[0, 1],
        )
        tm.assert_frame_equal(df, expected, check_index_type=False)


def test_frame_setitem_view_direct(multiindex_dataframe_random_data):
    # this works because we are modifying the underlying array
    # really a no-no
    df = multiindex_dataframe_random_data.T
    with pytest.raises(ValueError, match="read-only"):
        df["foo"].values[:] = 0
    assert (df["foo"].values != 0).all()


def test_frame_setitem_copy_raises(multiindex_dataframe_random_data):
    # will raise/warn as its chained assignment
    df = multiindex_dataframe_random_data.T
    with tm.raises_chained_assignment_error():
        df["foo"]["one"] = 2


def test_frame_setitem_copy_no_write(multiindex_dataframe_random_data):
    frame = multiindex_dataframe_random_data.T
    expected = frame
    df = frame.copy()
    with tm.raises_chained_assignment_error():
        df["foo"]["one"] = 2

    tm.assert_frame_equal(df, expected)