Skip to content

Commit

Permalink
REGR: inplace Series op not actually operating inplace (pandas-dev#37508
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jbrockmendel authored and ukarroum committed Nov 2, 2020
1 parent 18ba7d2 commit 1ef3170
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Fixed regressions
- 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`)
- Fixed regression in setitem with a Series getting aligned before setting the values (:issue:`37427`)
- Fixed regression in :attr:`MultiIndex.is_monotonic_increasing` returning wrong results with ``NaN`` in at least one of the levels (:issue:`37220`)
- Fixed regression in inplace arithmetic operation on a Series not updating the parent DataFrame (:issue:`36373`)

.. ---------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11115,6 +11115,11 @@ def _inplace_method(self, other, op):
"""
result = op(self, other)

if self.ndim == 1 and result._indexed_same(self) and result.dtype == self.dtype:
# GH#36498 this inplace op can _actually_ be inplace.
self._values[:] = result._values
return self

# Delete cacher
self._reset_cacher()

Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1698,3 +1698,16 @@ def test_arith_list_of_arraylike_raise(to_add):
df + to_add
with pytest.raises(ValueError, match=msg):
to_add + df


def test_inplace_arithmetic_series_update():
# https://github.com/pandas-dev/pandas/issues/36373
df = DataFrame({"A": [1, 2, 3]})
series = df["A"]
vals = series._values

series += 1
assert series._values is vals

expected = DataFrame({"A": [2, 3, 4]})
tm.assert_frame_equal(df, expected)
1 change: 1 addition & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ def test_identity_slice_returns_new_object(self):
original_series[:3] = [7, 8, 9]
assert all(sliced_series[:3] == [7, 8, 9])

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

0 comments on commit 1ef3170

Please sign in to comment.