Skip to content

DOC: Clarify in pandas.DataFrame.drop that a copy is returned when inplace = False. #34955

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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4006,7 +4006,8 @@ def drop(
level : int or level name, optional
For MultiIndex, level from which the labels will be removed.
inplace : bool, default False
If True, do operation inplace and return None.
Copy link
Contributor

Choose a reason for hiding this comment

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

we should actually audit all of the inplace doc-strings and make them consistent (maybe even have a rule)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought of this too but couldn’t figure out how to do it properly. I’m assuming there’s a better way besides searching for every method that takes an ‘inplace’ argument, checking if there’s any room for confusion and then make the adjustments.

Choose a reason for hiding this comment

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

Hi, I am the author of #33451. Thank you for adressing this issue. In the issue I mention pandas.DataFrame.drop as an example but the scope is much broader.

In general, I might not know whether a pandas method returns a copy or a view, also if there is no 'inplace' argument.

According to https://stackoverflow.com/questions/23296282/what-rules-does-pandas-use-to-generate-a-view-vs-a-copy it appears that methods return a copy in general (assuming there is no 'inplace' argument and there is no indexing like https://pandas.pydata.org/docs/user_guide/indexing.html#returning-a-view-versus-a-copy).
However, I am confused why some methods (e.g., https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.assign.html), which do not have an 'inplace' argument, explicitly mentioned that a copy/new object is returned.
On the other hand, other methods do not mention anything at all or a VERY vague. E.g., according to the documentation (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.query.html), pd.DataFrame.query returns a 'DataFrame resulting from the provided query expression'.
Thus, when I use a method which I haven't used before or recently, I often have to investigate whether a copy is returned. And this is very cumbersome.

I wonder whether you can confirm that methods in general return a copy? If this is the case, and if I am correct that this is not mentioned in the user guide (?), I think this should definitely be highlighted in the user guide. In this case, it might also not be necessary to clarify whether a method returns a copy or a view (if there is no 'inplace' argument). 'Only' the documentation has to be tidied, i.e., parts that mention that a copy or view is returned (e.g., https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.assign.html) should be removed.

However, if it is not true that methods return a copy in general, I would highly appreciate if every method clarifies whether it returns a copy or a view.

If False, return a copy. Otherwise, do operation
inplace and return None.
errors : {'ignore', 'raise'}, default 'raise'
If 'ignore', suppress error and only existing labels are
dropped.
Expand Down