Skip to content

Commit 3addc0e

Browse files
committed
new tests
1 parent 5608c83 commit 3addc0e

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3136,7 +3136,7 @@ def _setitem_multilevel(self, key, value):
31363136
key = (key,)
31373137
if isinstance(value, DataFrame):
31383138
if len(key) + value.columns.nlevels != self.columns.nlevels:
3139-
raise TypeError(
3139+
raise ValueError(
31403140
"Must pass key/value pair that conforms with number of column "
31413141
"levels"
31423142
)

pandas/tests/indexing/multiindex/test_multiindex.py

+23
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,26 @@ def test_multiindex_frame_assign(self):
159159
axis=1,
160160
)
161161
tm.assert_frame_equal(result, expected)
162+
163+
# invalid usage
164+
msg = "Must pass key/value pair that conforms with number of column levels"
165+
166+
# too few levels at level one
167+
with pytest.raises(ValueError, match=msg):
168+
df2["m"] = df0
169+
170+
# too few levels at level two - this appears to be desired
171+
# with pytest.raises(ValueError, match=msg):
172+
# df2["m", "x"] = df0["a"]
173+
174+
# too many levels at level one
175+
with pytest.raises(ValueError, match=msg):
176+
df2["m"] = df2
177+
178+
# too many levels at level two
179+
with pytest.raises(ValueError, match=msg):
180+
df2["m", "x"] = df1
181+
182+
# too many levels at level three
183+
with pytest.raises(ValueError, match=msg):
184+
df2["m", "x", "a"] = df0

0 commit comments

Comments
 (0)