Skip to content

Commit ef2c61a

Browse files
CoW - don't try to update underlying values of Series/column inplace for inplace operator (#55745)
1 parent fc0164c commit ef2c61a

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

pandas/core/generic.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -12378,7 +12378,12 @@ def _inplace_method(self, other, op) -> Self:
1237812378

1237912379
result = op(self, other)
1238012380

12381-
if self.ndim == 1 and result._indexed_same(self) and result.dtype == self.dtype:
12381+
if (
12382+
self.ndim == 1
12383+
and result._indexed_same(self)
12384+
and result.dtype == self.dtype
12385+
and not using_copy_on_write()
12386+
):
1238212387
# GH#36498 this inplace op can _actually_ be inplace.
1238312388
# Item "ArrayManager" of "Union[ArrayManager, SingleArrayManager,
1238412389
# BlockManager, SingleBlockManager]" has no attribute "setitem_inplace"

pandas/tests/copy_view/test_methods.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -1815,12 +1815,22 @@ def test_update_chained_assignment(using_copy_on_write):
18151815
tm.assert_frame_equal(df, df_orig)
18161816

18171817

1818-
def test_inplace_arithmetic_series():
1818+
def test_inplace_arithmetic_series(using_copy_on_write):
18191819
ser = Series([1, 2, 3])
1820+
ser_orig = ser.copy()
18201821
data = get_array(ser)
18211822
ser *= 2
1822-
assert np.shares_memory(get_array(ser), data)
1823-
tm.assert_numpy_array_equal(data, get_array(ser))
1823+
if using_copy_on_write:
1824+
# https://github.com/pandas-dev/pandas/pull/55745
1825+
# changed to NOT update inplace because there is no benefit (actual
1826+
# operation already done non-inplace). This was only for the optics
1827+
# of updating the backing array inplace, but we no longer want to make
1828+
# that guarantee
1829+
assert not np.shares_memory(get_array(ser), data)
1830+
tm.assert_numpy_array_equal(data, get_array(ser_orig))
1831+
else:
1832+
assert np.shares_memory(get_array(ser), data)
1833+
tm.assert_numpy_array_equal(data, get_array(ser))
18241834

18251835

18261836
def test_inplace_arithmetic_series_with_reference(

0 commit comments

Comments
 (0)