-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
API / CoW: Copy NumPy arrays by default in DataFrame constructor #51731
Changes from 27 commits
563257e
f3161a3
3a95311
17cf5ae
07aa26d
8e84d85
f3ccf0f
5cdc6ad
3e384ea
49ee53f
fcc7be2
9223836
a474bf5
d5a0268
0be7fc6
293f8a5
3376d06
265d9e3
9ac1bae
db92ce4
be9cb04
4bf3ee8
e2eceec
65965c6
ecb756c
8e837d9
177fbbc
2bbff3b
5bef4ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,8 +47,9 @@ def test_fillna_dict_inplace_nonunique_columns(self, using_copy_on_write): | |
def test_fillna_on_column_view(self, using_copy_on_write): | ||
# GH#46149 avoid unnecessary copies | ||
arr = np.full((40, 50), np.nan) | ||
df = DataFrame(arr) | ||
df = DataFrame(arr, copy=False) | ||
|
||
# TODO(CoW): This should raise a chained assignment error | ||
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. Added this to the list in the overview issue #48998 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. Thx |
||
df[0].fillna(-1, inplace=True) | ||
if using_copy_on_write: | ||
assert np.isnan(arr[:, 0]).all() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -309,14 +309,14 @@ def test_constructor_dtype_nocast_view_2d_array( | |
def test_1d_object_array_does_not_copy(self): | ||
# https://github.com/pandas-dev/pandas/issues/39272 | ||
arr = np.array(["a", "b"], dtype="object") | ||
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. Or add 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. sure, done |
||
df = DataFrame(arr) | ||
df = DataFrame(arr, copy=False) | ||
assert np.shares_memory(df.values, arr) | ||
|
||
@td.skip_array_manager_invalid_test | ||
def test_2d_object_array_does_not_copy(self): | ||
# https://github.com/pandas-dev/pandas/issues/39272 | ||
arr = np.array([["a", "b"], ["c", "d"]], dtype="object") | ||
df = DataFrame(arr) | ||
df = DataFrame(arr, copy=False) | ||
assert np.shares_memory(df.values, arr) | ||
|
||
def test_constructor_dtype_list_data(self): | ||
|
@@ -2107,13 +2107,18 @@ def test_constructor_frame_shallow_copy(self, float_frame): | |
cop.index = np.arange(len(cop)) | ||
tm.assert_frame_equal(float_frame, orig) | ||
|
||
def test_constructor_ndarray_copy(self, float_frame, using_array_manager): | ||
def test_constructor_ndarray_copy( | ||
self, float_frame, using_array_manager, using_copy_on_write | ||
): | ||
if not using_array_manager: | ||
arr = float_frame.values.copy() | ||
df = DataFrame(arr) | ||
|
||
arr[5] = 5 | ||
assert (df.values[5] == 5).all() | ||
if using_copy_on_write: | ||
assert not (df.values[5] == 5).all() | ||
else: | ||
assert (df.values[5] == 5).all() | ||
|
||
df = DataFrame(arr, copy=True) | ||
arr[6] = 6 | ||
|
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.
Should we add a warning like (about copy=False): "in that case pandas does not guarantee correct Copy-on-Write behaviour in case the numpy array would get modified after creating the DataFrame"?
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.
Added