Skip to content

Commit 4520f84

Browse files
authored
TST: Copy on Write for filter (#50589)
1 parent b329806 commit 4520f84

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

pandas/tests/copy_view/test_methods.py

+21
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,27 @@ def test_select_dtypes(using_copy_on_write):
221221
tm.assert_frame_equal(df, df_orig)
222222

223223

224+
@pytest.mark.parametrize(
225+
"filter_kwargs", [{"items": ["a"]}, {"like": "a"}, {"regex": "a"}]
226+
)
227+
def test_filter(using_copy_on_write, filter_kwargs):
228+
# Case: selecting columns using `filter()` returns a new dataframe
229+
# + afterwards modifying the result
230+
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
231+
df_orig = df.copy()
232+
df2 = df.filter(**filter_kwargs)
233+
if using_copy_on_write:
234+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
235+
else:
236+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
237+
238+
# mutating df2 triggers a copy-on-write for that column/block
239+
if using_copy_on_write:
240+
df2.iloc[0, 0] = 0
241+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
242+
tm.assert_frame_equal(df, df_orig)
243+
244+
224245
def test_pop(using_copy_on_write):
225246
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
226247
df_orig = df.copy()

0 commit comments

Comments
 (0)