From 149798832a5ce180af645c21864a6db73a089daa Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sun, 20 Sep 2020 13:26:44 +0100 Subject: [PATCH 01/11] Not deleting cacher --- pandas/core/ops/methods.py | 2 -- 1 file changed, 2 deletions(-) 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( From b2b9bd6e2d576cac7845e0d17cc02deeb49635a3 Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sun, 20 Sep 2020 13:43:37 +0100 Subject: [PATCH 02/11] add unit test to check if + operation on Series updating parent DataFrame --- pandas/tests/arithmetic/test_numeric.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index ecac08ffe3ba2..6bff9b7c370ba 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -1297,3 +1297,9 @@ 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(): + df = pd.DataFrame({"A": [1, 2, 3]}) + s = df["A"] + s += 1 + tm.assert_series_equal(df["A"], s) \ No newline at end of file From f7b089337edc49d3ccaf023d2a37a41ea42a60e4 Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sun, 20 Sep 2020 13:51:16 +0100 Subject: [PATCH 03/11] solve PEP 8 issues --- pandas/tests/arithmetic/test_numeric.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index 6bff9b7c370ba..7bb9cb2e2b02a 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -1298,8 +1298,10 @@ 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(): df = pd.DataFrame({"A": [1, 2, 3]}) s = df["A"] s += 1 - tm.assert_series_equal(df["A"], s) \ No newline at end of file + tm.assert_series_equal(df["A"], s) + \ No newline at end of file From 816425c0cbbb950312857b0459ac3d0773e4bc6f Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sun, 20 Sep 2020 14:04:34 +0100 Subject: [PATCH 04/11] more PEP 8 issues --- pandas/tests/arithmetic/test_numeric.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index 7bb9cb2e2b02a..ef635578930f6 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -706,7 +706,8 @@ def test_divmod_series(self, numeric_idx): @pytest.mark.parametrize("other", [np.nan, 7, -23, 2.718, -3.14, np.inf]) def test_ops_np_scalar(self, other): vals = np.random.randn(5, 3) - f = lambda x: pd.DataFrame( + + def f(x): return pd.DataFrame( x, index=list("ABCDE"), columns=["jim", "joe", "jolie"] ) @@ -1304,4 +1305,3 @@ def test_inplace_arithmetic_operation_on_series_updating_parent_dataframe(): s = df["A"] s += 1 tm.assert_series_equal(df["A"], s) - \ No newline at end of file From 8494202e5b2671aa5c146aac2d5272b0a15e20af Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sun, 20 Sep 2020 14:14:38 +0100 Subject: [PATCH 05/11] PEP 8 issues ... --- pandas/tests/arithmetic/test_numeric.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index 7bb9cb2e2b02a..6adc5f149f6bc 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -1304,4 +1304,3 @@ def test_inplace_arithmetic_operation_on_series_updating_parent_dataframe(): s = df["A"] s += 1 tm.assert_series_equal(df["A"], s) - \ No newline at end of file From 7277d3828c6ab5f832368c7892ad065b6dcc34ed Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sun, 20 Sep 2020 14:28:42 +0100 Subject: [PATCH 06/11] PEP8 ... --- pandas/tests/arithmetic/test_numeric.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index ef635578930f6..6adc5f149f6bc 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -706,8 +706,7 @@ def test_divmod_series(self, numeric_idx): @pytest.mark.parametrize("other", [np.nan, 7, -23, 2.718, -3.14, np.inf]) def test_ops_np_scalar(self, other): vals = np.random.randn(5, 3) - - def f(x): return pd.DataFrame( + f = lambda x: pd.DataFrame( x, index=list("ABCDE"), columns=["jim", "joe", "jolie"] ) From dc5e8a12416581b4e475e009ea9b0c962511a76b Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 25 Sep 2020 16:34:25 +0100 Subject: [PATCH 07/11] update test --- pandas/tests/arithmetic/test_numeric.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index 0ccfb81a83605..1546e6ba25921 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -1323,4 +1323,6 @@ def test_inplace_arithmetic_operation_on_series_updating_parent_dataframe(): df = pd.DataFrame({"A": [1, 2, 3]}) s = df["A"] s += 1 - tm.assert_series_equal(df["A"], s) + assert s is df["A"] + expected = pd.DataFrame({"A": [2, 3, 4]}) + tm.assert_frame_equal(df, expected) \ No newline at end of file From acb3c02c66ea7a3eb64db1e806331617a80168c5 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 25 Sep 2020 16:37:43 +0100 Subject: [PATCH 08/11] lint and comment --- pandas/tests/arithmetic/test_numeric.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index 1546e6ba25921..76e90d5e8a007 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -1320,9 +1320,10 @@ def test_dataframe_div_silenced(): 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) \ No newline at end of file + tm.assert_frame_equal(df, expected) From ffc051f3074a6a68dad3b9b1b7c944fa0619c565 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 25 Sep 2020 18:39:06 +0100 Subject: [PATCH 09/11] fix failing tests --- pandas/core/generic.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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: From 8640237ac36f320fff477d3972437a6b33f3ed5e Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 25 Sep 2020 19:08:02 +0100 Subject: [PATCH 10/11] remove failing test --- pandas/tests/indexing/test_loc.py | 16 ---------------- 1 file changed, 16 deletions(-) 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. From 56a4c88796a560c676540ec3a1a90e07256dfbab Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 25 Sep 2020 20:30:06 +0100 Subject: [PATCH 11/11] add release note --- doc/source/whatsnew/v1.1.3.rst | 1 + 1 file changed, 1 insertion(+) 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`)