-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
DOC: Improve "inplace" param in Dataframe.rename() docs #24037
DOC: Improve "inplace" param in Dataframe.rename() docs #24037
Conversation
Hello @nmusolino! Thanks for submitting the PR.
|
As described in #23076, the description of "inplace" in the current documentation is "backwards". Arguably, there should be a wider cleanup to use a consistent phrasing for Here is some prior art for how "inplace" is documented in DataFrame methods with a similar purpose:
DataFrame.rename_axis(). This is probably from a shared NDFrame docstring.
|
Codecov Report
@@ Coverage Diff @@
## master #24037 +/- ##
=======================================
Coverage 42.46% 42.46%
=======================================
Files 161 161
Lines 51557 51557
=======================================
Hits 21892 21892
Misses 29665 29665
Continue to review full report at Codecov.
|
1 similar comment
Codecov Report
@@ Coverage Diff @@
## master #24037 +/- ##
=======================================
Coverage 42.46% 42.46%
=======================================
Files 161 161
Lines 51557 51557
=======================================
Hits 21892 21892
Misses 29665 29665
Continue to review full report at Codecov.
|
That sounds like a plan. Maybe we could do it in this PR even. |
I'm willing to work on that in a future PR. My suggestion is to get this PR in right now, in order to fix the mistaken docstring and close the associated issue (#23076). The problem with the current The current docstring does not follow that convention, and in fact inverts that convention. As for the wider change, I think that will take more discussion:
Or even, should that effort be made in light of #16529: API/DEPR: Deprecate inplace parameter? I just saw that deprecation proposal now. |
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.
+1 on standardizing the descriptions for inplace
. But the proposed description is no exactly correct.
Whether to return a new DataFrame. If True then value of copy is | ||
ignored. | ||
If True, perform the operation in-place, modifying this DataFrame, | ||
and return None. |
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 don't think this is technically correct. The name is misleading, but using inplace
doesn't necessarily mean that we're modifying the DataFrame
, as we can be returning a copy.
I think the previous description is more accurate, except that it sounds like inplace=True
will return a new DataFrame
.
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.
Sorry, I don't understand your first paragraph. Can you give an example of when an operation with inplace=True
doesn't modify "self", i.e. the data frame on which the method is called?
When inplace is True, the method should modify the data frame, and no second frame should be created. When inplace is False, the method should return a second data frame, and self
should be unmodified.
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.
If you have a DataFrame
named df
, using the memory in the address 1000-2000, and you do df.operation(inplace=True)
, df
will be modified, but the data can be now in the memory address 3000-4000.
So, technically speaking, the operation is not in place, as the result has been copied to a different location (even if the reference df
is reassigned to the new memory).
That's why I don't like your description, as for a user who doesn't care about the memory is perfectly fine and correct. But if you think about the memory, you description is not really correct.
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.
@nmusolino there are about 2 inplace operations that actually operate inplace. all others, simply do an internal copy, do the op, then assign back to the top level data. inplace has never been a real operation in pandas, nor will it ever be. This is why we are deprecating it. You can certainly give a longer explanation, but discouraging usage would be good here.
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.
@jreback are we ready to deprecate all inplace
parameters? A quick grep shows around 60 methods with an inplace
parameter, so it's not so much work to start raising warnings (and update the descriptions) for all them.
Or do we prefer to wait?
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.
See #16529 - I think we were planning to wait until 1.0, though can always push up.
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.
Thanks for pointing out the issue @gfyoung, I'll continue the conversation there.
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.
@datapythonista, @jreback , I understand that these operations may involve copies. I wrote earlier that "internal data structures (e.g. block manager or index) might be copied even when inplace is True."
I think the distinction you are trying to draw is that a rename
or fillna
operation does not necessarily overwrite individual elements in place; is that right? As a simple example, I seem to recall that Index objects are generally immutable, so any operation that changes the index would necessarily replace the index.
Avoiding that implication is a reasonable point. To be clear, however, the methods are modifying the dataframe.
How about this description?
inplace : boolean, default False
If true, rename index labels in this DataFrame and return None,
without creating a new DataFrame.
Or a simpler, more generic version:
inplace : boolean, default False
If true, modify this DataFrame and return None; does not create a
new DataFrame.
Closing in favor of #24063, where the |
git diff upstream/master -u -- "*.py" | flake8 --diff