Skip to content

Maintain inplace operation on series #36498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 changes: 0 additions & 2 deletions pandas/core/ops/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ def _wrap_inplace_method(method):

def f(self, other):
result = method(self, other)
# Delete cacher
self._reset_cacher()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added to fix an existing bug (see #30501) so removing it would likely cause a regression

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this have been the incorrect location for the original fix? it appears there were a couple of iterations in #30501.

# this makes sure that we are aligned like the input
# we are updating inplace so we want to ignore is_copy
self._update_inplace(
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,3 +1297,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():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test passes for me on master. is there another case that fails?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I think need to test df["A"] is s which is also True on 1.1.2

>>> pd.__version__
'1.1.2'
>>>
>>> import pandas.testing as tm
>>>
>>> df = pd.DataFrame({"A": [1, 2, 3]})
>>> s = df["A"]
>>> s += 1
>>> tm.assert_series_equal(df["A"], s)
>>>
>>> s
0    2
1    3
2    4
Name: A, dtype: int64
>>>
>>> df["A"]
0    2
1    3
2    4
Name: A, dtype: int64
>>>
>>> df
   A
0  1
1  2
2  3
>>> df["A"] is s
True
>>>

but the output of __repr__ and also other ops is incorrect

so should also compare using assert_frame_equal

>>> tm.assert_frame_equal(df, pd.DataFrame({"A": [2, 3, 4]}))
Traceback (most recent call last):
...
AssertionError: DataFrame.iloc[:, 0] (column name="A") are different

DataFrame.iloc[:, 0] (column name="A") values are different (100.0 %)
[index]: [0, 1, 2]
[left]:  [1, 2, 3]
[right]: [2, 3, 4]
>>>

but this should pass with the fix here

df = pd.DataFrame({"A": [1, 2, 3]})
s = df["A"]
s += 1
tm.assert_series_equal(df["A"], s)