-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API / CoW: return read-only numpy arrays in .values/to_numpy() #51082
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
phofl
merged 13 commits into
pandas-dev:main
from
jorisvandenbossche:cow-to-numpy-readonly
Mar 13, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9fd459c
API / CoW: return read-only numpy arrays in .values/to_numpy()
jorisvandenbossche 5a59564
Merge remote-tracking branch 'upstream/main' into cow-to-numpy-readonly
jorisvandenbossche 1939132
Merge remote-tracking branch 'upstream/main' into cow-to-numpy-readonly
jorisvandenbossche 5ec2763
actually commit tests that I wrote
jorisvandenbossche 3ade20d
also handle Series/DataFrame __array__
jorisvandenbossche ee3c9f8
Merge remote-tracking branch 'upstream/main' into cow-to-numpy-readonly
jorisvandenbossche 4123918
fix tests
jorisvandenbossche 3d08b97
Merge remote-tracking branch 'upstream/main' into cow-to-numpy-readonly
jorisvandenbossche fd9398a
Merge remote-tracking branch 'upstream/main' into cow-to-numpy-readonly
jorisvandenbossche 11b8ecd
update tests
jorisvandenbossche fbbd9aa
add failing test for np.fix
jorisvandenbossche 2f08bf2
Merge remote-tracking branch 'upstream/main' into cow-to-numpy-readonly
jorisvandenbossche 1bc1fed
proper skip
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
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,112 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import ( | ||
DataFrame, | ||
Series, | ||
) | ||
import pandas._testing as tm | ||
from pandas.tests.copy_view.util import get_array | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Copy/view behaviour for accessing underlying array of Series/DataFrame | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"method", | ||
[lambda ser: ser.values, lambda ser: np.asarray(ser)], | ||
ids=["values", "asarray"], | ||
) | ||
def test_series_values(using_copy_on_write, method): | ||
ser = Series([1, 2, 3], name="name") | ||
ser_orig = ser.copy() | ||
|
||
arr = method(ser) | ||
|
||
if using_copy_on_write: | ||
# .values still gives a view but is read-only | ||
assert np.shares_memory(arr, get_array(ser, "name")) | ||
assert arr.flags.writeable is False | ||
|
||
# mutating series through arr therefore doesn't work | ||
with pytest.raises(ValueError, match="read-only"): | ||
arr[0] = 0 | ||
tm.assert_series_equal(ser, ser_orig) | ||
|
||
# mutating the series itself still works | ||
ser.iloc[0] = 0 | ||
assert ser.values[0] == 0 | ||
else: | ||
assert arr.flags.writeable is True | ||
arr[0] = 0 | ||
assert ser.iloc[0] == 0 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"method", | ||
[lambda df: df.values, lambda df: np.asarray(df)], | ||
ids=["values", "asarray"], | ||
) | ||
def test_dataframe_values(using_copy_on_write, using_array_manager, method): | ||
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) | ||
df_orig = df.copy() | ||
|
||
arr = method(df) | ||
|
||
if using_copy_on_write: | ||
# .values still gives a view but is read-only | ||
assert np.shares_memory(arr, get_array(df, "a")) | ||
assert arr.flags.writeable is False | ||
|
||
# mutating series through arr therefore doesn't work | ||
with pytest.raises(ValueError, match="read-only"): | ||
arr[0, 0] = 0 | ||
tm.assert_frame_equal(df, df_orig) | ||
|
||
# mutating the series itself still works | ||
df.iloc[0, 0] = 0 | ||
assert df.values[0, 0] == 0 | ||
else: | ||
assert arr.flags.writeable is True | ||
arr[0, 0] = 0 | ||
if not using_array_manager: | ||
assert df.iloc[0, 0] == 0 | ||
else: | ||
tm.assert_frame_equal(df, df_orig) | ||
|
||
|
||
def test_series_to_numpy(using_copy_on_write): | ||
ser = Series([1, 2, 3], name="name") | ||
ser_orig = ser.copy() | ||
|
||
# default: copy=False, no dtype or NAs | ||
arr = ser.to_numpy() | ||
if using_copy_on_write: | ||
# to_numpy still gives a view but is read-only | ||
assert np.shares_memory(arr, get_array(ser, "name")) | ||
assert arr.flags.writeable is False | ||
|
||
# mutating series through arr therefore doesn't work | ||
with pytest.raises(ValueError, match="read-only"): | ||
arr[0] = 0 | ||
tm.assert_series_equal(ser, ser_orig) | ||
|
||
# mutating the series itself still works | ||
ser.iloc[0] = 0 | ||
assert ser.values[0] == 0 | ||
else: | ||
assert arr.flags.writeable is True | ||
arr[0] = 0 | ||
assert ser.iloc[0] == 0 | ||
|
||
# specify copy=False gives a writeable array | ||
ser = Series([1, 2, 3], name="name") | ||
arr = ser.to_numpy(copy=True) | ||
assert not np.shares_memory(arr, get_array(ser, "name")) | ||
assert arr.flags.writeable is True | ||
|
||
# specifying a dtype that already causes a copy also gives a writeable array | ||
ser = Series([1, 2, 3], name="name") | ||
arr = ser.to_numpy(dtype="float64") | ||
assert not np.shares_memory(arr, get_array(ser, "name")) | ||
assert arr.flags.writeable is True |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.