-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API / CoW: constructing Series from Series creates lazy copy (with default copy=False) #49524
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
jorisvandenbossche
merged 7 commits into
pandas-dev:main
from
jorisvandenbossche:cow-constructor-from-pandas
Jan 13, 2023
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
24f624d
API / CoW: constructing Series from Series creates lazy copy (with de…
jorisvandenbossche e69c87b
Merge remote-tracking branch 'upstream/main' into cow-constructor-fro…
jorisvandenbossche e6ae662
Merge remote-tracking branch 'upstream/main' into cow-constructor-fro…
jorisvandenbossche 1ebbfe8
limit to CoW
jorisvandenbossche 006ced4
add whatsnew
jorisvandenbossche bc01a51
Merge remote-tracking branch 'upstream/main' into cow-constructor-fro…
jorisvandenbossche 4bd8ea1
use helper
jorisvandenbossche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import numpy as np | ||
|
||
from pandas import Series | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Copy/view behaviour for Series / DataFrame constructors | ||
|
||
|
||
def test_series_from_series(using_copy_on_write): | ||
# Case: constructing a Series from another Series object follows CoW rules: | ||
# a new object is returned and thus mutations are not propagated | ||
ser = Series([1, 2, 3], name="name") | ||
|
||
# default is copy=False -> new Series is a shallow copy / view of original | ||
result = Series(ser) | ||
|
||
# the shallow copy still shares memory | ||
assert np.shares_memory(ser.values, result.values) | ||
|
||
if using_copy_on_write: | ||
assert result._mgr.refs is not None | ||
|
||
if using_copy_on_write: | ||
# mutating new series copy doesn't mutate original | ||
result.iloc[0] = 0 | ||
assert ser.iloc[0] == 1 | ||
# mutating triggered a copy-on-write -> no longer shares memory | ||
assert not np.shares_memory(ser.values, result.values) | ||
else: | ||
# mutating shallow copy does mutate original | ||
result.iloc[0] = 0 | ||
assert ser.iloc[0] == 0 | ||
# and still shares memory | ||
assert np.shares_memory(ser.values, result.values) | ||
|
||
# the same when modifying the parent | ||
result = Series(ser) | ||
|
||
if using_copy_on_write: | ||
# mutating original doesn't mutate new series | ||
ser.iloc[0] = 0 | ||
assert result.iloc[0] == 1 | ||
else: | ||
# mutating original does mutate shallow copy | ||
ser.iloc[0] = 0 | ||
assert result.iloc[0] == 0 | ||
|
||
|
||
def test_series_from_series_with_reindex(using_copy_on_write): | ||
# Case: constructing a Series from another Series with specifying an index | ||
# that potentially requires a reindex of the values | ||
ser = Series([1, 2, 3], name="name") | ||
|
||
# passing an index that doesn't actually require a reindex of the values | ||
# -> without CoW we get an actual mutating view | ||
for index in [ | ||
ser.index, | ||
ser.index.copy(), | ||
list(ser.index), | ||
ser.index.rename("idx"), | ||
]: | ||
result = Series(ser, index=index) | ||
assert np.shares_memory(ser.values, result.values) | ||
result.iloc[0] = 0 | ||
if using_copy_on_write: | ||
assert ser.iloc[0] == 1 | ||
else: | ||
assert ser.iloc[0] == 0 | ||
|
||
# ensure that if an actual reindex is needed, we don't have any refs | ||
# (mutating the result wouldn't trigger CoW) | ||
result = Series(ser, index=[0, 1, 2, 3]) | ||
assert not np.shares_memory(ser.values, result.values) | ||
if using_copy_on_write: | ||
assert result._mgr.refs is None or result._mgr.refs[0] is None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Added a new section like is being added in #50471. We can consolidate those different blurbs later on in a coherent whatsnew.