Skip to content

Commit e640dde

Browse files
authored
BUG: Bug in setitem raising ValueError when setting more than one column via array (#37964)
1 parent fb379d8 commit e640dde

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pandas/core/indexing.py

+3
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,9 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None):
667667
if k not in self.obj:
668668
if value is None:
669669
self.obj[k] = np.nan
670+
elif is_array_like(value) and value.ndim == 2:
671+
# GH#37964 have to select columnwise in case of array
672+
self.obj[k] = value[:, i]
670673
elif is_list_like(value):
671674
self.obj[k] = value[i]
672675
else:

pandas/tests/frame/indexing/test_setitem.py

+15
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,21 @@ def test_setitem_periodindex(self):
289289
assert isinstance(rs.index, PeriodIndex)
290290
tm.assert_index_equal(rs.index, rng)
291291

292+
def test_setitem_complete_column_with_array(self):
293+
# GH#37954
294+
df = DataFrame({"a": ["one", "two", "three"], "b": [1, 2, 3]})
295+
arr = np.array([[1, 1], [3, 1], [5, 1]])
296+
df[["c", "d"]] = arr
297+
expected = DataFrame(
298+
{
299+
"a": ["one", "two", "three"],
300+
"b": [1, 2, 3],
301+
"c": [1, 3, 5],
302+
"d": [1, 1, 1],
303+
}
304+
)
305+
tm.assert_frame_equal(df, expected)
306+
292307

293308
class TestDataFrameSetItemSlicing:
294309
def test_setitem_slice_position(self):

0 commit comments

Comments
 (0)