-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API / CoW: DataFrame(<dict of Series>, copy=False) constructor now gives lazy copy #50777
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 23 commits into
pandas-dev:main
from
jorisvandenbossche:cow-dataframe-constructor-from-series
Feb 10, 2023
Merged
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6ce3306
API / CoW: DataFrame(<dict of Series>, copy=False) constructor now gi…
jorisvandenbossche 104cab9
expand test
jorisvandenbossche e192a63
try fix mypy
jorisvandenbossche 0b4f337
Merge remote-tracking branch 'upstream/main' into cow-dataframe-const…
jorisvandenbossche a300b08
clean-up usage of internals in reshape/concat.py
jorisvandenbossche df21ca2
further fix mypy
jorisvandenbossche bc5d77a
Merge remote-tracking branch 'upstream/main' into cow-dataframe-const…
jorisvandenbossche e7d2f0f
expand tests
jorisvandenbossche 27e8549
fix reindex case
jorisvandenbossche a05c066
Merge remote-tracking branch 'upstream/main' into cow-dataframe-const…
jorisvandenbossche 10238ba
clean-up
jorisvandenbossche 6d860bc
add whatsnew
jorisvandenbossche 5bb6ab0
Merge remote-tracking branch 'upstream/main' into cow-dataframe-const…
jorisvandenbossche b24ea5f
add xfailed test for int64->Int64
jorisvandenbossche 941676b
Merge remote-tracking branch 'upstream/main' into cow-dataframe-const…
jorisvandenbossche 6e32a34
refactor
jorisvandenbossche 82bc4bb
cleanup
jorisvandenbossche 7fc8594
fix for AM
jorisvandenbossche 9d11606
Merge remote-tracking branch 'upstream/main' into cow-dataframe-const…
jorisvandenbossche 3d2d5e6
fix + address feedback
jorisvandenbossche 48a00a1
fixup typing
jorisvandenbossche 9a2f97c
Merge remote-tracking branch 'upstream/main' into cow-dataframe-const…
jorisvandenbossche 5476780
try fix typing
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
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,12 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import Series | ||
from pandas import ( | ||
DataFrame, | ||
Series, | ||
) | ||
import pandas._testing as tm | ||
from pandas.tests.copy_view.util import get_array | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Copy/view behaviour for Series / DataFrame constructors | ||
|
@@ -73,3 +79,37 @@ def test_series_from_series_with_reindex(using_copy_on_write): | |
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 | ||
|
||
|
||
@pytest.mark.parametrize("dtype", [None, "int64"]) | ||
@pytest.mark.parametrize("columns", [None, ["a", "b"], ["a", "b", "c"]]) | ||
def test_dataframe_from_dict_of_series(using_copy_on_write, columns, dtype): | ||
# Case: constructing a DataFrame from Series objects with copy=False | ||
# has to do a lazy following CoW rules | ||
# (the default for DataFrame(dict) is still to copy to ensure consolidation) | ||
s1 = Series([1, 2, 3]) | ||
s2 = Series([4, 5, 6]) | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
s1_orig = s1.copy() | ||
expected = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}, dtype=dtype, columns=columns) | ||
|
||
result = DataFrame({"a": s1, "b": s2}, columns=columns, dtype=dtype, copy=False) | ||
|
||
# the shallow copy still shares memory | ||
assert np.shares_memory(get_array(result, "a"), s1.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. Can you use get_array here? 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. Also below |
||
|
||
# mutating the new dataframe doesn't mutate original | ||
result.iloc[0, 0] = 10 | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "a"), s1.values) | ||
tm.assert_series_equal(s1, s1_orig) | ||
else: | ||
assert s1.iloc[0] == 10 | ||
|
||
# the same when modifying the parent series | ||
result = DataFrame({"a": s1, "b": s2}, columns=columns, dtype=dtype, copy=False) | ||
s1.iloc[0] = 10 | ||
phofl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "a"), s1.values) | ||
tm.assert_frame_equal(result, expected) | ||
else: | ||
assert result.iloc[0, 0] == 10 |
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.
isn't this only relevant if reindex did not make a copy?
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.
Yes, but in
create_block_manager_from_column_arrays
(in managers.py) I will only actually use this parent if it has a ref itself, and so ifreindex
did a copy here, this Series will not have any refs, and it will not be tracked as parent in the endThere 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.
True this would work, but the following does not work:
In this scenario astype makes a copy while reindex does not. Hence, hval has a ref to the object created from astype which is inherited by the constructed element that is our result. Consequentially, iloc triggers an unnecessary copy
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.
Hmm, yes, if you have both a reindex and astype, that makes things a bit more complicated ..
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.
Updated for this by checking in each step if an actual copy was made, and then if any of the steps did make a copy, we don't add the Series to the parents (thus, also if the first astype step copied, and then the next reindex step did not copy, we wouldn't add the parent, and not trigger a CoW unnecessarily)
And added a test that covers your example above (that was failing before the fix)