Skip to content

Commit

Permalink
CoW - don't try to update underlying values of Series/column inplace …
Browse files Browse the repository at this point in the history
…for inplace operator (#55745)
  • Loading branch information
jorisvandenbossche authored Nov 17, 2023
1 parent fc0164c commit ef2c61a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 6 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12378,7 +12378,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 @@ -1815,12 +1815,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
# 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

0 comments on commit ef2c61a

Please sign in to comment.