-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TST: Copy on Write for filter #50589
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
Changes from all commits
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 |
---|---|---|
|
@@ -174,6 +174,27 @@ def test_select_dtypes(using_copy_on_write): | |
tm.assert_frame_equal(df, df_orig) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"filter_kwargs", [{"items": ["a"]}, {"like": "a"}, {"regex": "a"}] | ||
) | ||
def test_filter(using_copy_on_write, filter_kwargs): | ||
# Case: selecting columns using `filter()` returns a new dataframe | ||
# + afterwards modifying the result | ||
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}) | ||
df_orig = df.copy() | ||
df2 = df.filter(**filter_kwargs) | ||
if using_copy_on_write: | ||
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a")) | ||
else: | ||
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) | ||
|
||
# mutating df2 triggers a copy-on-write for that column/block | ||
if using_copy_on_write: | ||
df2.iloc[0, 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. This was raising a SettingWithCopy error(reasonable, since filter uses loc) so I just moved it inside the CoW case. 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. Yeah had the same problem somewhere else |
||
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) | ||
tm.assert_frame_equal(df, df_orig) | ||
|
||
|
||
def test_pop(using_copy_on_write): | ||
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}) | ||
df_orig = df.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.
Out of curiosity, do you think that numpy.may_share_memory() function can be faster than using np.shares_memory()? I compared it once, and it seems like it could be the case, but not super important.
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.
This doesn't really matter for tests, but seems like it could be faster