Skip to content
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

CoW - don't try to update underlying values of Series/column inplace for inplace operator #55745

Merged
merged 3 commits into from
Nov 17, 2023
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
7 changes: 6 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12398,7 +12398,12 @@ def _inplace_method(self, other, op) -> Self:

result = op(self, other)

if self.ndim == 1 and result._indexed_same(self) and result.dtype == self.dtype:
if (
self.ndim == 1
and result._indexed_same(self)
and result.dtype == self.dtype
and not using_copy_on_write()
):
# GH#36498 this inplace op can _actually_ be inplace.
# Item "ArrayManager" of "Union[ArrayManager, SingleArrayManager,
# BlockManager, SingleBlockManager]" has no attribute "setitem_inplace"
Expand Down
16 changes: 13 additions & 3 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -1806,12 +1806,22 @@ def test_update_chained_assignment(using_copy_on_write):
tm.assert_frame_equal(df, df_orig)


def test_inplace_arithmetic_series():
def test_inplace_arithmetic_series(using_copy_on_write):
ser = Series([1, 2, 3])
ser_orig = ser.copy()
data = get_array(ser)
ser *= 2
assert np.shares_memory(get_array(ser), data)
tm.assert_numpy_array_equal(data, get_array(ser))
if using_copy_on_write:
# https://github.com/pandas-dev/pandas/pull/55745
# changed to NOT update inplace because there is no benefit (actual
Copy link
Member

Choose a reason for hiding this comment

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

GH ref pointing back to this PR/discussion

# operation already done non-inplace). This was only for the optics
# of updating the backing array inplace, but we no longer want to make
# that guarantee
assert not np.shares_memory(get_array(ser), data)
tm.assert_numpy_array_equal(data, get_array(ser_orig))
else:
assert np.shares_memory(get_array(ser), data)
tm.assert_numpy_array_equal(data, get_array(ser))


def test_inplace_arithmetic_series_with_reference(
Expand Down
Loading