Skip to content

Commit e7e5df5

Browse files
authored
BUG: setitem raising for rhs with midx columns and tuple indexer (#49124)
1 parent 856e1e2 commit e7e5df5

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ Interval
220220
Indexing
221221
^^^^^^^^
222222
- Bug in :meth:`DataFrame.reindex` filling with wrong values when indexing columns and index for ``uint`` dtypes (:issue:`48184`)
223+
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` when right hand side is :class:`DataFrame` with :class:`MultiIndex` columns (:issue:`49121`)
223224
- Bug in :meth:`DataFrame.reindex` casting dtype to ``object`` when :class:`DataFrame` has single extension array column when re-indexing ``columns`` and ``index`` (:issue:`48190`)
224225
- Bug in :func:`~DataFrame.describe` when formatting percentiles in the resulting index showed more decimals than needed (:issue:`46362`)
225226
- Bug in :meth:`DataFrame.compare` does not recognize differences when comparing ``NA`` with value in nullable dtypes (:issue:`48939`)

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4110,7 +4110,7 @@ def _set_item_frame_value(self, key, value: DataFrame) -> None:
41104110
if key in self.columns:
41114111
loc = self.columns.get_loc(key)
41124112
cols = self.columns[loc]
4113-
len_cols = 1 if is_scalar(cols) else len(cols)
4113+
len_cols = 1 if is_scalar(cols) or isinstance(cols, tuple) else len(cols)
41144114
if len_cols != len(value.columns):
41154115
raise ValueError("Columns must be same length as key")
41164116

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,14 @@ def test_setitem_frame_overwrite_with_ea_dtype(self, any_numeric_ea_dtype):
748748
)
749749
tm.assert_frame_equal(df, expected)
750750

751+
def test_setitem_frame_midx_columns(self):
752+
# GH#49121
753+
df = DataFrame({("a", "b"): [10]})
754+
expected = df.copy()
755+
col_name = ("a", "b")
756+
df[col_name] = df[[col_name]]
757+
tm.assert_frame_equal(df, expected)
758+
751759

752760
class TestSetitemTZAwareValues:
753761
@pytest.fixture

0 commit comments

Comments
 (0)