Skip to content

Commit 7b7e526

Browse files
jbrockmendelluckyvs1
authored andcommitted
BUG: DataFrame.iloc.__setitem__ with IntegerArray values (pandas-dev#39040)
1 parent 43b0298 commit 7b7e526

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pandas/core/internals/blocks.py

+8
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,14 @@ def setitem(self, indexer, value):
984984

985985
values = values.astype(arr_value.dtype, copy=False)
986986

987+
elif is_ea_value:
988+
# GH#38952
989+
if values.ndim == 1:
990+
values[indexer] = value
991+
else:
992+
# TODO(EA2D): special case not needed with 2D EA
993+
values[indexer] = value.to_numpy(values.dtype).reshape(-1, 1)
994+
987995
# set
988996
else:
989997
values[indexer] = value

pandas/tests/indexing/test_iloc.py

+19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Index,
1515
NaT,
1616
Series,
17+
array as pd_array,
1718
concat,
1819
date_range,
1920
isna,
@@ -64,6 +65,24 @@ def test_iloc_getitem_list_int(self):
6465
class TestiLocBaseIndependent:
6566
"""Tests Independent Of Base Class"""
6667

68+
@pytest.mark.parametrize("box", [pd_array, Series])
69+
def test_iloc_setitem_ea_inplace(self, frame_or_series, box):
70+
# GH#38952 Case with not setting a full column
71+
# IntegerArray without NAs
72+
arr = pd_array([1, 2, 3, 4])
73+
obj = frame_or_series(arr.to_numpy("i8"))
74+
values = obj.values
75+
76+
obj.iloc[:2] = box(arr[2:])
77+
expected = frame_or_series(np.array([3, 4, 3, 4], dtype="i8"))
78+
tm.assert_equal(obj, expected)
79+
80+
# Check that we are actually in-place
81+
if frame_or_series is Series:
82+
assert obj.values is values
83+
else:
84+
assert obj.values.base is values.base and values.base is not None
85+
6786
def test_is_scalar_access(self):
6887
# GH#32085 index with duplicates doesnt matter for _is_scalar_access
6988
index = Index([1, 2, 1])

0 commit comments

Comments
 (0)