-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API / CoW: Copy arrays by default in Series constructor #52022
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 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8be558e
API-CoW: Copy arrays in Series constructor
phofl 38c7f37
Fix
phofl b657561
Fix tests
phofl 6ec5865
Address review
phofl 3a8eb25
Merge remote-tracking branch 'upstream/main' into cow_series_from_array
phofl 48339d7
Merge remote-tracking branch 'upstream/main' into cow_series_from_array
phofl 0f98a2d
Update whatsnew
phofl bfd6595
Move
phofl d99c4e9
Fix
phofl 0e2c11e
Fix
phofl 3e7151a
Update series.py
phofl d881734
Merge remote-tracking branch 'upstream/main' into cow_series_from_array
phofl 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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
from pandas import ( | ||
DataFrame, | ||
Series, | ||
|
@@ -82,6 +83,36 @@ def test_series_from_series_with_reindex(using_copy_on_write): | |
assert not result._mgr.blocks[0].refs.has_reference() | ||
|
||
|
||
@pytest.mark.parametrize("fastpath", [False, True]) | ||
@pytest.mark.parametrize("dtype", [None, "int64"]) | ||
@pytest.mark.parametrize("idx", [None, pd.RangeIndex(start=0, stop=3, step=1)]) | ||
@pytest.mark.parametrize( | ||
"arr", [np.array([1, 2, 3], dtype="int64"), pd.array([1, 2, 3], dtype="Int64")] | ||
) | ||
def test_series_from_array(using_copy_on_write, idx, dtype, fastpath, arr): | ||
ser = Series(arr, dtype=dtype) | ||
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. Should idx and fastpath be passed here 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. Ah thx, yes |
||
ser_orig = ser.copy() | ||
data = getattr(arr, "_data", arr) | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(ser), data) | ||
else: | ||
assert np.shares_memory(get_array(ser), data) | ||
|
||
arr[0] = 100 | ||
if using_copy_on_write: | ||
tm.assert_series_equal(ser, ser_orig) | ||
else: | ||
expected = Series([100, 2, 3], dtype=dtype if dtype is not None else arr.dtype) | ||
tm.assert_series_equal(ser, expected) | ||
|
||
|
||
@pytest.mark.parametrize("copy", [True, False, None]) | ||
def test_series_from_array_different_dtype(using_copy_on_write, copy): | ||
arr = np.array([1, 2, 3], dtype="int64") | ||
ser = Series(arr, dtype="int32", copy=copy) | ||
assert not np.shares_memory(get_array(ser), arr) | ||
|
||
|
||
@pytest.mark.parametrize("func", [lambda x: x, lambda x: x._mgr]) | ||
@pytest.mark.parametrize("columns", [None, ["a"]]) | ||
def test_dataframe_constructor_mgr_or_df(using_copy_on_write, columns, func): | ||
|
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
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.
i find this difficult to reason about (... in part bc im distracted by being on a call). if a user explicitly passes
copy=False
, we should never ignore that. Does this (or any of the other CoW-constructor PRs that im having trouble keeping track of) respect that?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 pass an array then we respect copy=False with the drawback that your series gets modified when you modify the array, if you pass a Series/DataFrame we make a lazy copy to set up references
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.
This is actually the reason why I changed the default to None, so that we can respect a user actually passing False
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 also don't find it easy to read, but I think it can be simplified?
Wouldn't it be enough to check for if
copy
was originally None? Because ifcopy=True
, I would expect the copy to happen anyway later on (assuming it currently honored that keyword)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 wanted to avoid
using_copy_on_write
before hitting the fastpath, but only using copy is certainly more readable, changed