Skip to content

BUG: DataFrame.iloc.__setitem__ with IntegerArray values #39040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,14 @@ def setitem(self, indexer, value):

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

elif is_ea_value:
# GH#38952
if values.ndim == 1:
values[indexer] = value
else:
# TODO(EA2D): special case not needed with 2D EA
values[indexer] = value.to_numpy(values.dtype).reshape(-1, 1)

# set
else:
values[indexer] = value
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Index,
NaT,
Series,
array as pd_array,
concat,
date_range,
isna,
Expand Down Expand Up @@ -64,6 +65,24 @@ def test_iloc_getitem_list_int(self):
class TestiLocBaseIndependent:
"""Tests Independent Of Base Class"""

@pytest.mark.parametrize("box", [pd_array, Series])
def test_iloc_setitem_ea_inplace(self, frame_or_series, box):
# GH#38952 Case with not setting a full column
# IntegerArray without NAs
arr = pd_array([1, 2, 3, 4])
obj = frame_or_series(arr.to_numpy("i8"))
values = obj.values

obj.iloc[:2] = box(arr[2:])
expected = frame_or_series(np.array([3, 4, 3, 4], dtype="i8"))
tm.assert_equal(obj, expected)

# Check that we are actually in-place
if frame_or_series is Series:
assert obj.values is values
else:
assert obj.values.base is values.base and values.base is not None

def test_is_scalar_access(self):
# GH#32085 index with duplicates doesnt matter for _is_scalar_access
index = Index([1, 2, 1])
Expand Down