diff --git a/doc/source/whatsnew/v1.1.3.rst b/doc/source/whatsnew/v1.1.3.rst index c63a78c76572f..b5022d05676a5 100644 --- a/doc/source/whatsnew/v1.1.3.rst +++ b/doc/source/whatsnew/v1.1.3.rst @@ -35,6 +35,7 @@ Fixed regressions - Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`) - Fixed regression in :meth:`read_excel` with ``engine="odf"`` caused ``UnboundLocalError`` in some cases where cells had nested child nodes (:issue:`36122`, :issue:`35802`) - Fixed regression in :class:`DataFrame` and :class:`Series` comparisons between numeric arrays and strings (:issue:`35700`, :issue:`36377`) +- Fixed regression where inplace arithmetic operation on :class:`Series` no longer updated parent :class:`DataFrame` (:issue:`36373`) - Fixed regression in :meth:`DataFrame.apply` with ``raw=True`` and user-function returning string (:issue:`35940`) - Fixed regression when setting empty :class:`DataFrame` column to a :class:`Series` in preserving name of index in frame (:issue:`36527`) - Fixed regression in :class:`Period` incorrect value for ordinal over the maximum timestamp (:issue:`36430`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a8b48f875c825..ffa51fd1d3da2 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3373,8 +3373,12 @@ def _maybe_cache_changed(self, item, value) -> None: """ The object has called back to us saying maybe it has changed. """ - loc = self._info_axis.get_loc(item) - self._mgr.iset(loc, value) + try: + loc = self._info_axis.get_loc(item) + except KeyError: + pass + else: + self._mgr.iset(loc, value) @property def _is_cached(self) -> bool_t: diff --git a/pandas/core/ops/methods.py b/pandas/core/ops/methods.py index e04db92b58c36..877df5d0c8ad0 100644 --- a/pandas/core/ops/methods.py +++ b/pandas/core/ops/methods.py @@ -93,8 +93,6 @@ def _wrap_inplace_method(method): def f(self, other): result = method(self, other) - # Delete cacher - self._reset_cacher() # this makes sure that we are aligned like the input # we are updating inplace so we want to ignore is_copy self._update_inplace( diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index 139401bdf5806..76e90d5e8a007 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -1317,3 +1317,13 @@ def test_dataframe_div_silenced(): ) with tm.assert_produces_warning(None): pdf1.div(pdf2, fill_value=0) + + +def test_inplace_arithmetic_operation_on_series_updating_parent_dataframe(): + # https://github.com/pandas-dev/pandas/issues/36373 + df = pd.DataFrame({"A": [1, 2, 3]}) + s = df["A"] + s += 1 + assert s is df["A"] + expected = pd.DataFrame({"A": [2, 3, 4]}) + tm.assert_frame_equal(df, expected) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 9b9bca77e17ec..cd807a2a25853 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -888,22 +888,6 @@ def test_identity_slice_returns_new_object(self): original_series[:3] = [7, 8, 9] assert all(sliced_series[:3] == [7, 8, 9]) - def test_loc_copy_vs_view(self): - # GH 15631 - x = DataFrame(zip(range(3), range(3)), columns=["a", "b"]) - - y = x.copy() - q = y.loc[:, "a"] - q += 2 - - tm.assert_frame_equal(x, y) - - z = x.copy() - q = z.loc[x.index, "a"] - q += 2 - - tm.assert_frame_equal(x, z) - def test_loc_uint64(self): # GH20722 # Test whether loc accept uint64 max value as index.