-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add lazy copy to concat and round #50501
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 all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
94c982e
Start with implmenting concat for cow
phofl 7fbba4a
Merge remote-tracking branch 'upstream/main' into cow_concat_new
phofl 0db1c39
Fix concat logic
phofl fc564f8
Add series test
phofl a7165c2
Clean up
phofl 20a62bb
Skip test
phofl bca2123
Fix test
phofl 3c4abd2
Add check
phofl 7734979
Merge remote-tracking branch 'upstream/main' into cow_concat_new
phofl 8bc3272
Push logic down
phofl 79cedd4
Fix test
phofl 6c8a704
Fix typing
phofl de678ab
Merge branch 'main' into cow_concat_new
phofl dddc9f0
Fix annotation
phofl d18416d
Fix chained concat
phofl 5ccf23e
Pass to bm
phofl 7648b23
Fix series case
phofl 99da573
Update pandas/core/internals/concat.py
phofl a6afa8b
Fix concat
phofl 3d0772c
Merge remote-tracking branch 'upstream/main' into cow_concat_new
phofl 8b49210
Fix chained series case
phofl 48a4f7e
Merge remote-tracking branch 'upstream/main' into cow_concat_new
phofl 690f072
Merge remote-tracking branch 'upstream/main' into cow_concat_new
phofl 8ba35fe
Fix mypy
phofl 2047d5c
Fix bug
phofl e959c53
Add test and flag
phofl 891e07c
Fix hdf test
phofl 04adc17
Fix mypy
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 |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import numpy as np | ||
|
||
from pandas import ( | ||
DataFrame, | ||
Series, | ||
concat, | ||
) | ||
import pandas._testing as tm | ||
from pandas.tests.copy_view.util import get_array | ||
|
||
|
||
def test_concat_frames(using_copy_on_write): | ||
df = DataFrame({"b": ["a"] * 3}) | ||
df2 = DataFrame({"a": ["a"] * 3}) | ||
df_orig = df.copy() | ||
result = concat([df, df2], axis=1) | ||
|
||
if using_copy_on_write: | ||
assert np.shares_memory(get_array(result, "b"), get_array(df, "b")) | ||
assert np.shares_memory(get_array(result, "a"), get_array(df2, "a")) | ||
else: | ||
assert not np.shares_memory(get_array(result, "b"), get_array(df, "b")) | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a")) | ||
|
||
result.iloc[0, 0] = "d" | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "b"), get_array(df, "b")) | ||
assert np.shares_memory(get_array(result, "a"), get_array(df2, "a")) | ||
|
||
result.iloc[0, 1] = "d" | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a")) | ||
tm.assert_frame_equal(df, df_orig) | ||
|
||
|
||
def test_concat_frames_updating_input(using_copy_on_write): | ||
df = DataFrame({"b": ["a"] * 3}) | ||
df2 = DataFrame({"a": ["a"] * 3}) | ||
result = concat([df, df2], axis=1) | ||
|
||
if using_copy_on_write: | ||
assert np.shares_memory(get_array(result, "b"), get_array(df, "b")) | ||
assert np.shares_memory(get_array(result, "a"), get_array(df2, "a")) | ||
else: | ||
assert not np.shares_memory(get_array(result, "b"), get_array(df, "b")) | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a")) | ||
|
||
expected = result.copy() | ||
df.iloc[0, 0] = "d" | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "b"), get_array(df, "b")) | ||
assert np.shares_memory(get_array(result, "a"), get_array(df2, "a")) | ||
|
||
df2.iloc[0, 0] = "d" | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a")) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_concat_series(using_copy_on_write): | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ser = Series([1, 2], name="a") | ||
ser2 = Series([3, 4], name="b") | ||
ser_orig = ser.copy() | ||
ser2_orig = ser2.copy() | ||
result = concat([ser, ser2], axis=1) | ||
|
||
if using_copy_on_write: | ||
assert np.shares_memory(get_array(result, "a"), ser.values) | ||
assert np.shares_memory(get_array(result, "b"), ser2.values) | ||
else: | ||
assert not np.shares_memory(get_array(result, "a"), ser.values) | ||
assert not np.shares_memory(get_array(result, "b"), ser2.values) | ||
|
||
result.iloc[0, 0] = 100 | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "a"), ser.values) | ||
assert np.shares_memory(get_array(result, "b"), ser2.values) | ||
|
||
result.iloc[0, 1] = 1000 | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "b"), ser2.values) | ||
tm.assert_series_equal(ser, ser_orig) | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tm.assert_series_equal(ser2, ser2_orig) | ||
|
||
|
||
def test_concat_frames_chained(using_copy_on_write): | ||
df1 = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]}) | ||
df2 = DataFrame({"c": [4, 5, 6]}) | ||
df3 = DataFrame({"d": [4, 5, 6]}) | ||
result = concat([concat([df1, df2], axis=1), df3], axis=1) | ||
expected = result.copy() | ||
|
||
if using_copy_on_write: | ||
assert np.shares_memory(get_array(result, "a"), get_array(df1, "a")) | ||
assert np.shares_memory(get_array(result, "c"), get_array(df2, "c")) | ||
assert np.shares_memory(get_array(result, "d"), get_array(df3, "d")) | ||
else: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a")) | ||
assert not np.shares_memory(get_array(result, "c"), get_array(df2, "c")) | ||
assert not np.shares_memory(get_array(result, "d"), get_array(df3, "d")) | ||
|
||
df1.iloc[0, 0] = 100 | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a")) | ||
|
||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_concat_series_chained(using_copy_on_write): | ||
ser1 = Series([1, 2, 3], name="a") | ||
ser2 = Series([4, 5, 6], name="c") | ||
ser3 = Series([4, 5, 6], name="d") | ||
result = concat([concat([ser1, ser2], axis=1), ser3], axis=1) | ||
expected = result.copy() | ||
|
||
if using_copy_on_write: | ||
assert np.shares_memory(get_array(result, "a"), get_array(ser1, "a")) | ||
assert np.shares_memory(get_array(result, "c"), get_array(ser2, "c")) | ||
assert np.shares_memory(get_array(result, "d"), get_array(ser3, "d")) | ||
else: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(ser1, "a")) | ||
assert not np.shares_memory(get_array(result, "c"), get_array(ser2, "c")) | ||
assert not np.shares_memory(get_array(result, "d"), get_array(ser3, "d")) | ||
|
||
ser1.iloc[0] = 100 | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(ser1, "a")) | ||
|
||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_concat_series_updating_input(using_copy_on_write): | ||
ser = Series([1, 2], name="a") | ||
ser2 = Series([3, 4], name="b") | ||
expected = DataFrame({"a": [1, 2], "b": [3, 4]}) | ||
result = concat([ser, ser2], axis=1) | ||
|
||
if using_copy_on_write: | ||
assert np.shares_memory(get_array(result, "a"), get_array(ser, "a")) | ||
assert np.shares_memory(get_array(result, "b"), get_array(ser2, "b")) | ||
else: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(ser, "a")) | ||
assert not np.shares_memory(get_array(result, "b"), get_array(ser2, "b")) | ||
|
||
ser.iloc[0] = 100 | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(ser, "a")) | ||
assert np.shares_memory(get_array(result, "b"), get_array(ser2, "b")) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
ser2.iloc[0] = 1000 | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "b"), get_array(ser2, "b")) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_concat_mixed_series_frame(using_copy_on_write): | ||
df = DataFrame({"a": [1, 2, 3], "c": 1}) | ||
ser = Series([4, 5, 6], name="d") | ||
result = concat([df, ser], axis=1) | ||
expected = result.copy() | ||
|
||
if using_copy_on_write: | ||
assert np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
assert np.shares_memory(get_array(result, "c"), get_array(df, "c")) | ||
assert np.shares_memory(get_array(result, "d"), get_array(ser, "d")) | ||
else: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
assert not np.shares_memory(get_array(result, "c"), get_array(df, "c")) | ||
assert not np.shares_memory(get_array(result, "d"), get_array(ser, "d")) | ||
|
||
ser.iloc[0] = 100 | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "d"), get_array(ser, "d")) | ||
|
||
df.iloc[0, 0] = 100 | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
tm.assert_frame_equal(result, expected) |
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
Oops, something went wrong.
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.
Do we have a test that covers this mixed case for checking that the CoW works correctly?
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.
Yep the chained series case covers this, but will add an explicit one as well after hiding the change behind the cow flag