-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TST: add copy/view test for setting columns with an array/series #47070
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
mroeschke
merged 3 commits into
pandas-dev:main
from
jorisvandenbossche:cow-tests-set-column
May 25, 2022
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import numpy as np | ||
|
||
from pandas import ( | ||
DataFrame, | ||
Index, | ||
RangeIndex, | ||
Series, | ||
) | ||
import pandas._testing as tm | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Copy/view behaviour for the values that are set in a DataFrame | ||
|
||
|
||
def test_set_column_with_array(): | ||
# Case: setting an array as a new column (df[col] = arr) copies that data | ||
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) | ||
arr = np.array([1, 2, 3]) | ||
|
||
df["c"] = arr | ||
|
||
# the array data is copied | ||
assert not np.shares_memory(df["c"].values, arr) | ||
# and thus modifying the array does not modify the DataFrame | ||
arr[0] = 0 | ||
tm.assert_series_equal(df["c"], Series([1, 2, 3], name="c")) | ||
|
||
|
||
def test_set_column_with_series(using_copy_on_write): | ||
# Case: setting a series as a new column (df[col] = s) copies that data | ||
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) | ||
s = Series([1, 2, 3]) | ||
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. nitpick s->ser pls |
||
|
||
df["c"] = s | ||
|
||
if using_copy_on_write: | ||
# with CoW we can delay the copy | ||
assert np.shares_memory(df["c"].values, s.values) | ||
else: | ||
# the series data is copied | ||
assert not np.shares_memory(df["c"].values, s.values) | ||
|
||
# and modifying the series does not modify the DataFrame | ||
s.iloc[0] = 0 | ||
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. could also assert |
||
tm.assert_series_equal(df["c"], Series([1, 2, 3], name="c")) | ||
|
||
|
||
def test_set_column_with_index(using_copy_on_write): | ||
# Case: setting an index as a new column (df[col] = idx) copies that data | ||
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) | ||
idx = Index([1, 2, 3]) | ||
|
||
df["c"] = idx | ||
|
||
# the index data is copied | ||
assert not np.shares_memory(df["c"].values, idx.values) | ||
|
||
# and thus modifying the index does not modify the DataFrame | ||
idx.values[0] = 0 | ||
tm.assert_series_equal(df["c"], Series([1, 2, 3], name="c")) | ||
|
||
# however, in case of a RangeIndex, we currently don't copy the cached | ||
# "materialized" values | ||
idx = RangeIndex(1, 4) | ||
arr = idx.values | ||
|
||
df["d"] = idx | ||
|
||
if using_copy_on_write: | ||
assert not np.shares_memory(df["d"].values, arr) | ||
arr[0] = 0 | ||
tm.assert_series_equal(df["d"], Series([1, 2, 3], name="d")) | ||
else: | ||
assert np.shares_memory(df["d"].values, arr) | ||
arr[0] = 0 | ||
tm.assert_series_equal(df["d"], Series([0, 2, 3], name="d")) | ||
|
||
|
||
def test_set_columns_with_dataframe(using_copy_on_write): | ||
# Case: setting a DataFrame as new columns copies that data | ||
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) | ||
df2 = DataFrame({"c": [7, 8, 9], "d": [10, 11, 12]}) | ||
|
||
df[["c", "d"]] = df2 | ||
|
||
if using_copy_on_write: | ||
# with CoW we can delay the copy | ||
assert np.shares_memory(df["c"].values, df2["c"].values) | ||
else: | ||
# the data is copied | ||
assert not np.shares_memory(df["c"].values, df2["c"].values) | ||
|
||
# and modifying the set DataFrame does not modify the original DataFrame | ||
df2.iloc[0, 0] = 0 | ||
tm.assert_series_equal(df["c"], Series([7, 8, 9], name="c")) |
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.
Uh oh!
There was an error while loading. Please reload this page.