-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CoW: Add warning for replace with inplace #56060
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
Changes from 4 commits
dff1fef
89185cd
7562ca0
d74ad2b
97bf95f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -100,6 +100,7 @@ | |||||||||
SettingWithCopyError, | ||||||||||
SettingWithCopyWarning, | ||||||||||
_chained_assignment_method_msg, | ||||||||||
_chained_assignment_warning_method_msg, | ||||||||||
) | ||||||||||
from pandas.util._decorators import ( | ||||||||||
deprecate_nonkeyword_arguments, | ||||||||||
|
@@ -7773,6 +7774,17 @@ def replace( | |||||||||
ChainedAssignmentError, | ||||||||||
stacklevel=2, | ||||||||||
) | ||||||||||
elif not PYPY and not using_copy_on_write(): | ||||||||||
ctr = sys.getrefcount(self) | ||||||||||
ref_count = REF_COUNT | ||||||||||
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"): | ||||||||||
ref_count += 1 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem might be that in the warning mode, this is not the case? (so that might need to add a Although the tests are not failing .. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand your comment, why would this not be the case? I double checked and it seems to work in warning mode There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related to #55838 (comment), I thought I had disabled the item cache for the warning mode, which I would think to affect the count. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I double checked this locally, the cache is still populated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, will take a closer look There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, no, the The
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, that suggestion gives a bunch of failures (false positives) in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which is logical, because that's the whole point with And so what you have (checking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Getting a single column with In theory you can get a Series as a result of a calculation that doesn't do this ( So to summarize: your fix is probably fully correct, I only didn't understand why ;) You might want to add a comment about it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes your conclusion is correct as far as I can tell. That was the reason why I added the hasattr check. Sorry for omitting this information. Added a comment |
||||||||||
if ctr <= ref_count: | ||||||||||
warnings.warn( | ||||||||||
_chained_assignment_warning_method_msg, | ||||||||||
FutureWarning, | ||||||||||
stacklevel=2, | ||||||||||
) | ||||||||||
|
||||||||||
if not is_bool(regex) and to_replace is not None: | ||||||||||
raise ValueError("'to_replace' must be 'None' if 'regex' is not a bool") | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
from pandas import ( | ||
Categorical, | ||
DataFrame, | ||
option_context, | ||
) | ||
import pandas._testing as tm | ||
from pandas.tests.copy_view.util import get_array | ||
|
@@ -395,6 +396,17 @@ def test_replace_chained_assignment(using_copy_on_write): | |
with tm.raises_chained_assignment_error(): | ||
df[["a"]].replace(1, 100, inplace=True) | ||
tm.assert_frame_equal(df, df_orig) | ||
else: | ||
with tm.assert_produces_warning(FutureWarning, match="inplace method"): | ||
with option_context("mode.chained_assignment", None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it otherwise also raise a SettingWithCopyWarning? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, [["a"]] currently copies |
||
df[["a"]].replace(1, 100, inplace=True) | ||
|
||
with tm.assert_produces_warning(FutureWarning, match="inplace method"): | ||
with option_context("mode.chained_assignment", None): | ||
df[df.a > 5].replace(1, 100, inplace=True) | ||
|
||
with tm.assert_produces_warning(FutureWarning, match="inplace method"): | ||
df["a"].replace(1, 100, inplace=True) | ||
|
||
|
||
def test_replace_listlike(using_copy_on_write): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(maybe a bit long to put in every place that has this check (after the other PRs), but a comment like this would have explained it to me)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am fine with adding this here. Maybe adding a link to this comment for the other prs?