Skip to content

Commit

Permalink
TST: Copy on Write for filter (pandas-dev#50589)
Browse files Browse the repository at this point in the history
  • Loading branch information
lithomas1 authored Jan 6, 2023
1 parent b329806 commit 4520f84
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,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
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()
Expand Down

0 comments on commit 4520f84

Please sign in to comment.