-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: Add clearer info when copy is False but memory shared only for certain objects #41514
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 3 commits
f9efd2a
0c70b9f
2ed2b8d
1a56449
ac82b6b
5eab8f9
0e147a1
dfd10c8
52ce3ba
818d5d6
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -223,7 +223,10 @@ class Series(base.IndexOpsMixin, generic.NDFrame): | |||||||||||
name : str, optional | ||||||||||||
The name to give to the Series. | ||||||||||||
copy : bool, default False | ||||||||||||
Copy input data. | ||||||||||||
Copy input data. If False and the Series returns a `copy` | ||||||||||||
of the data, the memory location for the values is not shared. | ||||||||||||
If False and the Series returns a `view` on the data, | ||||||||||||
the memory location for the values is shared. | ||||||||||||
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.
Suggested change
|
||||||||||||
|
||||||||||||
Examples | ||||||||||||
-------- | ||||||||||||
|
@@ -251,6 +254,36 @@ class Series(base.IndexOpsMixin, generic.NDFrame): | |||||||||||
Note that the Index is first build with the keys from the dictionary. | ||||||||||||
After this the Series is reindexed with the given Index values, hence we | ||||||||||||
get all NaN as a result. | ||||||||||||
|
||||||||||||
Constructing Series from an array with `copy=False`. | ||||||||||||
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'd call this a list, since the difference between an array and list in this context is important in terms of ability to share memory. 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.
Suggested change
|
||||||||||||
|
||||||||||||
>>> r = [1,2] | ||||||||||||
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.
Suggested change
|
||||||||||||
>>> ser = pd.Series(r, copy=False) | ||||||||||||
>>> ser.iloc[0] = 999 | ||||||||||||
>>> r | ||||||||||||
[1, 2] | ||||||||||||
>>> ser | ||||||||||||
0 999 | ||||||||||||
1 2 | ||||||||||||
dtype: int64 | ||||||||||||
|
||||||||||||
The Series returns a `copy` of the original data, so | ||||||||||||
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. What do you mean by returns here? It's more like the data backing the series does not share the same memory as the input 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. Oh right, by "returns" a copy I meant the Series created had a copy of the data. I guess "returns" is not the correct word to use. 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.
Suggested change
|
||||||||||||
the original data is unchanged. | ||||||||||||
|
||||||||||||
Constructing Series from a `numpy.array` with `copy=False`. | ||||||||||||
|
||||||||||||
>>> r = np.array([1,2]) | ||||||||||||
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.
Suggested change
|
||||||||||||
>>> ser = pd.Series(r, copy=False) | ||||||||||||
>>> ser.iloc[0] = 999 | ||||||||||||
>>> r | ||||||||||||
array([999, 2]) | ||||||||||||
>>> ser | ||||||||||||
0 999 | ||||||||||||
1 2 | ||||||||||||
dtype: int32 | ||||||||||||
|
||||||||||||
The Series returns a `view` on the original data, so | ||||||||||||
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.
Suggested change
|
||||||||||||
the original data is changed as well. | ||||||||||||
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. Same comment as above on wording of returns |
||||||||||||
""" | ||||||||||||
|
||||||||||||
_typ = "series" | ||||||||||||
|
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.
Same comment on usage of "returns" as below.
Generally I think this description might be too verbose for a parameter description and could be shortened to basically just get the idea across that a copy may still be made if necessary (as in the case of a list where we cannot share memory). Then the examples below should suffice to explain the different possible cases.
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.
(https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html has a good example of this)