Skip to content

Commit a65f001

Browse files
jbrockmendeljorisvandenbossche
authored andcommitted
REGR: inplace Series op not actually operating inplace (pandas-dev#37508)
1 parent 344fa71 commit a65f001

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

doc/source/whatsnew/v1.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Fixed regressions
2929
- Fixed regression in :class:`StataReader` which required ``chunksize`` to be manually set when using an iterator to read a dataset (:issue:`37280`)
3030
- Fixed regression in setitem with :meth:`DataFrame.iloc` which raised error when trying to set a value while filtering with a boolean list (:issue:`36741`)
3131
- Fixed regression in :attr:`MultiIndex.is_monotonic_increasing` returning wrong results with ``NaN`` in at least one of the levels (:issue:`37220`)
32+
- Fixed regression in inplace arithmetic operation on a Series not updating the parent DataFrame (:issue:`36373`)
3233

3334
.. ---------------------------------------------------------------------------
3435

pandas/core/ops/methods.py

+11
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,19 @@ def _wrap_inplace_method(method):
9393

9494
def f(self, other):
9595
result = method(self, other)
96+
97+
if (
98+
self.ndim == 1
99+
and result._indexed_same(self)
100+
and result.dtype == self.dtype
101+
):
102+
# GH#36498 this inplace op can _actually_ be inplace.
103+
self._values[:] = result._values
104+
return self
105+
96106
# Delete cacher
97107
self._reset_cacher()
108+
98109
# this makes sure that we are aligned like the input
99110
# we are updating inplace so we want to ignore is_copy
100111
self._update_inplace(

pandas/tests/frame/test_arithmetic.py

+13
Original file line numberDiff line numberDiff line change
@@ -1566,3 +1566,16 @@ def test_arith_reindex_with_duplicates():
15661566
result = df1 + df2
15671567
expected = pd.DataFrame([[np.nan, 0, 0]], columns=["first", "second", "second"])
15681568
tm.assert_frame_equal(result, expected)
1569+
1570+
1571+
def test_inplace_arithmetic_series_update():
1572+
# https://github.com/pandas-dev/pandas/issues/36373
1573+
df = DataFrame({"A": [1, 2, 3]})
1574+
series = df["A"]
1575+
vals = series._values
1576+
1577+
series += 1
1578+
assert series._values is vals
1579+
1580+
expected = DataFrame({"A": [2, 3, 4]})
1581+
tm.assert_frame_equal(df, expected)

pandas/tests/indexing/test_loc.py

+1
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ def test_identity_slice_returns_new_object(self):
896896
original_series[:3] = [7, 8, 9]
897897
assert all(sliced_series[:3] == [7, 8, 9])
898898

899+
@pytest.mark.xfail(reason="accidental fix reverted - GH37497")
899900
def test_loc_copy_vs_view(self):
900901
# GH 15631
901902
x = DataFrame(zip(range(3), range(3)), columns=["a", "b"])

0 commit comments

Comments
 (0)