Skip to content

REGR: setting column with setitem should not modify existing array inplace #35266

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

Closed
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
12 changes: 9 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1684,15 +1684,21 @@ def isetter(loc, v):
com.is_null_slice(idx) or com.is_full_slice(idx, len(self.obj))
for idx in pi
):
ser = v
if ser._mgr.any_extension_types:
# avoid `iset` for extension arrays, as this doesn't
# change the underlying values inplace (GH33457)
ser._mgr = ser._mgr.setitem(indexer=pi, value=v)
else:
ser = v
self.obj._iset_item(loc, ser)
else:
# set the item, possibly having a dtype change
ser = ser.copy()
ser._mgr = ser._mgr.setitem(indexer=pi, value=v)
ser._maybe_update_cacher(clear=True)

# reset the sliced object if unique
self.obj._iset_item(loc, ser)
# reset the sliced object if unique
self.obj._iset_item(loc, ser)

# we need an iterable, with a ndim of at least 1
# eg. don't pass through np.array(0)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,7 @@ def should_store(self, value: ArrayLike) -> bool:

def set(self, locs, values):
assert locs.tolist() == [0]
self.values[:] = values
self.values = values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isnt this going to break views?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be, but that would mean it was broken in 1.0 as well, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the [:] was added after 1.0, then I guess so, yes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the [:] (i.e. behavior in master) in a released 1.0.x?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is referenced in the issue and the top post: #32831 -> this is only in master


def putmask(
self, mask, new, inplace: bool = False, axis: int = 0, transpose: bool = False,
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,15 @@ def test_iloc_setitem_categorical_updates_inplace(self):
expected = pd.Categorical(["C", "B", "A"])
tm.assert_categorical_equal(cat, expected)

# __setitem__ under the other hand does not work in-place
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you we split this test into 2 tests

cat = pd.Categorical(["A", "B", "C"])
df = pd.DataFrame({1: cat, 2: [1, 2, 3]})

df[1] = cat[::-1]

expected = pd.Categorical(["A", "B", "C"])
tm.assert_categorical_equal(cat, expected)

def test_iloc_with_boolean_operation(self):
# GH 20627
result = DataFrame([[0, 1], [2, 3], [4, 5], [6, np.nan]])
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,3 +1100,24 @@ def test_long_text_missing_labels_inside_loc_error_message_limited():
error_message_regex = "long_missing_label_text_0.*\\\\n.*long_missing_label_text_1"
with pytest.raises(KeyError, match=error_message_regex):
s.loc[["a", "c"] + missing_labels]


def test_setitem_EA_column_update():
# https://github.com/pandas-dev/pandas/issues/33457

df = pd.DataFrame(
{
"int": [1, 2, 3],
"int2": [3, 4, 5],
"float": [0.1, 0.2, 0.3],
"EA": pd.array([1, 2, None], dtype="Int64"),
}
)
original_arr = df.EA.array

# overwrite column with new array
df["EA"] = pd.array([1, 2, 3], dtype="Int64")
# ensure original array was not modified
assert original_arr is not df.EA.array
expected = pd.array([1, 2, None], dtype="Int64")
tm.assert_extension_array_equal(original_arr, expected)