Skip to content

Commit cf2d8f9

Browse files
authored
TST: Test mask/where inplace don't modify views with CoW (#51303)
1 parent 180d81f commit cf2d8f9

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

pandas/tests/copy_view/test_replace.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import pytest
23

34
from pandas import (
45
Categorical,
@@ -12,7 +13,7 @@ def test_replace_categorical_inplace_reference(using_copy_on_write):
1213
df = DataFrame({"a": Categorical([1, 2, 3])})
1314
df_orig = df.copy()
1415
arr_a = get_array(df, "a")
15-
view = df[:] # noqa
16+
view = df[:]
1617
df.replace(to_replace=[1], value=2, inplace=True)
1718

1819
if using_copy_on_write:
@@ -27,7 +28,7 @@ def test_replace_categorical_inplace_reference(using_copy_on_write):
2728
def test_replace_inplace_reference(using_copy_on_write):
2829
df = DataFrame({"a": [1.5, 2, 3]})
2930
arr_a = get_array(df, "a")
30-
view = df[:] # noqa
31+
view = df[:]
3132
df.replace(to_replace=[1.5], value=15.5, inplace=True)
3233

3334
if using_copy_on_write:
@@ -36,3 +37,22 @@ def test_replace_inplace_reference(using_copy_on_write):
3637
assert view._mgr._has_no_reference(0)
3738
else:
3839
assert np.shares_memory(get_array(df, "a"), arr_a)
40+
41+
42+
@pytest.mark.parametrize("method", ["where", "mask"])
43+
def test_masking_inplace(using_copy_on_write, method):
44+
df = DataFrame({"a": [1.5, 2, 3]})
45+
df_orig = df.copy()
46+
arr_a = get_array(df, "a")
47+
view = df[:]
48+
49+
method = getattr(df, method)
50+
method(df["a"] > 1.6, -1, inplace=True)
51+
52+
if using_copy_on_write:
53+
assert not np.shares_memory(get_array(df, "a"), arr_a)
54+
assert df._mgr._has_no_reference(0)
55+
assert view._mgr._has_no_reference(0)
56+
tm.assert_frame_equal(view, df_orig)
57+
else:
58+
assert np.shares_memory(get_array(df, "a"), arr_a)

0 commit comments

Comments
 (0)