Skip to content

CoW: Add ChainedAssignmentError for replace with inplace=True #54023

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

Merged
merged 4 commits into from
Jul 17, 2023

Conversation

phofl
Copy link
Member

@phofl phofl commented Jul 6, 2023

xref #48998

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

@phofl phofl added this to the 2.1 milestone Jul 6, 2023
@@ -7611,6 +7611,16 @@ def replace(
)

inplace = validate_bool_kwarg(inplace, "inplace")
if inplace:
if not PYPY and using_copy_on_write():
refcount = 2 if PY311 else 3
Copy link
Member

Choose a reason for hiding this comment

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

This can be updated now the other PR is merged

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Comment on lines 71 to 78
pd.Series(cat, copy=False).replace(to_replace, value, inplace=True)
if using_copy_on_write:
with tm.raises_chained_assignment_error():
pd.Series(cat, copy=False).replace(to_replace, value, inplace=True)
else:
pd.Series(cat, copy=False).replace(to_replace, value, inplace=True)
tm.assert_categorical_equal(cat, expected)
Copy link
Member

Choose a reason for hiding this comment

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

In this case, I assume we are not necessarily testing that it is done on a single line, but just that the inplace works (and updates the underlying array. So I think it might be better to change the test to:

ser = pd.Series(cat, copy=False)
ser.replace(to_replace, value, inplace=True)

which should then work the same for both CoW and not?

Copy link
Member Author

Choose a reason for hiding this comment

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

Don't care much either way, updated the test

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Thanks!

@jorisvandenbossche jorisvandenbossche merged commit fcf7811 into pandas-dev:main Jul 17, 2023
@phofl phofl deleted the cow_replace_chained branch July 17, 2023 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants