Skip to content

Commit 68ef1da

Browse files
authored
BUG: Fix error message for assigning a column with an empty DataFrame (#56120)
1 parent 4cd6d7e commit 68ef1da

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v2.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ Other
590590
- Bug in :meth:`Dataframe.from_dict` which would always sort the rows of the created :class:`DataFrame`. (:issue:`55683`)
591591
- Bug in rendering ``inf`` values inside a a :class:`DataFrame` with the ``use_inf_as_na`` option enabled (:issue:`55483`)
592592
- Bug in rendering a :class:`Series` with a :class:`MultiIndex` when one of the index level's names is 0 not having that name displayed (:issue:`55415`)
593+
- Bug in the error message when assigning an empty dataframe to a column (:issue:`55956`)
593594

594595
.. ***DO NOT USE THIS SECTION***
595596

pandas/core/frame.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4375,11 +4375,15 @@ def _set_item_frame_value(self, key, value: DataFrame) -> None:
43754375

43764376
return self.isetitem(locs, value)
43774377

4378-
if len(value.columns) != 1:
4378+
if len(value.columns) > 1:
43794379
raise ValueError(
43804380
"Cannot set a DataFrame with multiple columns to the single "
43814381
f"column {key}"
43824382
)
4383+
elif len(value.columns) == 0:
4384+
raise ValueError(
4385+
f"Cannot set a DataFrame without columns to the column {key}"
4386+
)
43834387

43844388
self[key] = value[value.columns[0]]
43854389

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ def test_setitem_error_msmgs(self):
9393
with pytest.raises(ValueError, match=msg):
9494
df["gr"] = df.groupby(["b", "c"]).count()
9595

96+
# GH 55956, specific message for zero columns
97+
msg = "Cannot set a DataFrame without columns to the column gr"
98+
with pytest.raises(ValueError, match=msg):
99+
df["gr"] = DataFrame()
100+
96101
def test_setitem_benchmark(self):
97102
# from the vb_suite/frame_methods/frame_insert_columns
98103
N = 10

0 commit comments

Comments
 (0)