From 34eee1ef4c63bc12975799bfbe98fd43728ac93b Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sat, 28 Oct 2023 16:19:28 +0200 Subject: [PATCH 1/2] CoW - don't try to update underlying values of Series/column inplace for inplace operator --- pandas/core/generic.py | 7 ++++++- pandas/tests/copy_view/test_methods.py | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c49c9eddae62c..7b98ee4289cf0 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -12393,7 +12393,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" diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index 45bfc6a6fcf9b..843735d64bb0d 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -1787,12 +1787,21 @@ 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: + # 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(using_copy_on_write): From bfb0ad0f3e3278ad610f01713dd114ae268ed48b Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 17 Nov 2023 11:30:21 +0100 Subject: [PATCH 2/2] add gh ref --- pandas/tests/copy_view/test_methods.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index f8b63f186a61e..4cfa56d1431a1 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -1812,6 +1812,7 @@ def test_inplace_arithmetic_series(using_copy_on_write): data = get_array(ser) ser *= 2 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