-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: Update pandas.Series.copy docstring #20261
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 2 commits
2686324
582f047
f1560e1
c4aed31
541edc2
18c7be3
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 |
---|---|---|
|
@@ -4506,22 +4506,110 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs): | |
|
||
def copy(self, deep=True): | ||
""" | ||
Make a copy of this objects data. | ||
Make a copy of this object's indices and data. | ||
|
||
When `deep=True` (default), a new object will be created with a | ||
copy of the calling object's data and indices. Modifications to | ||
the data or indices of the copy will not be reflected in the | ||
original object (see notes below). | ||
|
||
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. indices are immutable, mention this |
||
When `deep=False`, a new object will be created without copying | ||
the calling object's data (only a reference to the data is | ||
copied). Any changes to the data of the original will be reflected | ||
in the shallow copy (and vice versa). | ||
|
||
Parameters | ||
---------- | ||
deep : boolean or string, default True | ||
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. `True` instead of just True 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. str instead of string |
||
Make a deep copy, including a copy of the data and the indices. | ||
With ``deep=False`` neither the indices or the data are copied. | ||
|
||
Note that when ``deep=True`` data is copied, actual python objects | ||
will not be copied recursively, only the reference to the object. | ||
This is in contrast to ``copy.deepcopy`` in the Standard Library, | ||
which recursively copies object data. | ||
With `deep=False` neither the indices or the data are copied. | ||
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. "neither...nor" instead of "or" |
||
|
||
Returns | ||
------- | ||
copy : type of caller | ||
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. Move "type of caller" down as the description and say something like "Object type matches caller." 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. Still need to clean up the line that says Otherwise nice job on all the edits - changes I requested all lgtm 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, I would make this "Series or DataFrame" |
||
|
||
Notes | ||
----- | ||
When `deep=True`, data is copied but actual python objects | ||
will not be copied recursively, only the reference to the object. | ||
This is in contrast to `copy.deepcopy` in the Standard Library, | ||
which recursively copies object data. (See examples below). | ||
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. "...copies object data (see examples below)." - reduces some punctuation and saves space |
||
|
||
Examples | ||
-------- | ||
>>> s = pd.Series([1, 2], index=["a", "b"]) | ||
>>> s | ||
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. This is simple enough that I don't think you need to print |
||
a 1 | ||
b 2 | ||
dtype: int64 | ||
>>> s_copy = s.copy() | ||
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. blank line between cases (if they are separate / distinct) |
||
>>> s_copy | ||
a 1 | ||
b 2 | ||
dtype: int64 | ||
|
||
Shallow copy versus default (deep) copy: | ||
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. Since this is a "section header" can you get it to render in bold (i.e. between asterisks)? |
||
|
||
In a shallow copy, the data is shared with the original object. | ||
|
||
>>> s = pd.Series([1,2], index=["a", "b"]) | ||
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. Space after first comma |
||
>>> deep_copy = s.copy() | ||
>>> shallow_copy = s.copy(deep=False) | ||
>>> id(s) == id(shallow_copy) | ||
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. These examples are certainly comprehensive but perhaps too verbose. Can you refactor to make the index / values comparisons fit on one line? Similar comment with the value assignment - any consolidation will make it more readable |
||
False | ||
>>> id(s.values) == id(shallow_copy.values) | ||
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. Just again wondering if these can be more concise. Perhaps choosing a different variable name will allow you to do |
||
True | ||
>>> id(s) == id(deep_copy) | ||
False | ||
>>> id(s.values) == id(deep_copy.values) | ||
False | ||
>>> s[0] = 3 | ||
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. break this up with some text describing what you are showing |
||
>>> s | ||
a 3 | ||
b 2 | ||
dtype: int64 | ||
>>> shallow_copy | ||
a 3 | ||
b 2 | ||
dtype: int64 | ||
>>> deep_copy | ||
a 1 | ||
b 2 | ||
dtype: int64 | ||
>>> shallow_copy[0] = 4 | ||
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. Instead of having two different assignments I think you can have |
||
>>> s | ||
a 4 | ||
b 2 | ||
dtype: int64 | ||
>>> shallow_copy | ||
a 4 | ||
b 2 | ||
dtype: int64 | ||
>>> deep_copy | ||
a 1 | ||
b 2 | ||
dtype: int64 | ||
|
||
When copying an object containing python objects, deep copy will | ||
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. Capitalize Python and maybe say "a deep copy" |
||
copy the data, but will not do so recursively. | ||
|
||
>>> s = pd.Series([[1, 2], [3, 4]]) | ||
>>> s_copy = s.copy() | ||
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. show this with a deep copy 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 wasn't sure how to address this comment - do you mean to show the behavior of |
||
>>> s[0][0] = 10 | ||
>>> s | ||
0 [10, 2] | ||
1 [3, 4] | ||
dtype: object | ||
>>> s_copy | ||
0 [10, 2] | ||
1 [3, 4] | ||
dtype: object | ||
|
||
For deep-copying python objects, the following can be used: | ||
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. this last example is not necessary |
||
|
||
>>> import copy | ||
>>> deep_deep_copy = pd.Series(copy.deepcopy(s.values), | ||
... index=copy.deepcopy(s.index)) | ||
""" | ||
data = self._data.copy(deep=deep) | ||
return self._constructor(data).__finalize__(self) | ||
|
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.
Unless you want to have a Notes section I would say "see Examples below" as a reference to the section actually showing that