Skip to content

Commit 8159be6

Browse files
authored
TST: Add tests for clip with CoW (#51492)
1 parent d35646f commit 8159be6

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

pandas/tests/copy_view/test_clip.py

+51
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,54 @@ def test_clip_inplace_reference(using_copy_on_write):
1919
assert df._mgr._has_no_reference(0)
2020
assert view._mgr._has_no_reference(0)
2121
tm.assert_frame_equal(df_copy, view)
22+
23+
24+
def test_clip_inplace_reference_no_op(using_copy_on_write):
25+
df = DataFrame({"a": [1.5, 2, 3]})
26+
df_copy = df.copy()
27+
arr_a = get_array(df, "a")
28+
view = df[:]
29+
df.clip(lower=0, inplace=True)
30+
31+
if using_copy_on_write:
32+
assert np.shares_memory(get_array(df, "a"), arr_a)
33+
assert not df._mgr._has_no_reference(0)
34+
assert not view._mgr._has_no_reference(0)
35+
tm.assert_frame_equal(df_copy, view)
36+
else:
37+
assert not np.shares_memory(get_array(df, "a"), arr_a)
38+
39+
40+
def test_clip_inplace(using_copy_on_write):
41+
df = DataFrame({"a": [1.5, 2, 3]})
42+
arr_a = get_array(df, "a")
43+
df.clip(lower=2, inplace=True)
44+
45+
# Clip not actually inplace right now but could be
46+
assert not np.shares_memory(get_array(df, "a"), arr_a)
47+
48+
if using_copy_on_write:
49+
assert df._mgr._has_no_reference(0)
50+
51+
52+
def test_clip(using_copy_on_write):
53+
df = DataFrame({"a": [1.5, 2, 3]})
54+
df_orig = df.copy()
55+
df2 = df.clip(lower=2)
56+
57+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
58+
59+
if using_copy_on_write:
60+
assert df._mgr._has_no_reference(0)
61+
tm.assert_frame_equal(df_orig, df)
62+
63+
64+
def test_clip_no_op(using_copy_on_write):
65+
df = DataFrame({"a": [1.5, 2, 3]})
66+
df2 = df.clip(lower=0)
67+
68+
if using_copy_on_write:
69+
assert not df._mgr._has_no_reference(0)
70+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
71+
else:
72+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))

0 commit comments

Comments
 (0)