1
1
import numpy as np
2
+ import pytest
2
3
3
4
from pandas import (
4
5
Categorical ,
@@ -12,7 +13,7 @@ def test_replace_categorical_inplace_reference(using_copy_on_write):
12
13
df = DataFrame ({"a" : Categorical ([1 , 2 , 3 ])})
13
14
df_orig = df .copy ()
14
15
arr_a = get_array (df , "a" )
15
- view = df [:] # noqa
16
+ view = df [:]
16
17
df .replace (to_replace = [1 ], value = 2 , inplace = True )
17
18
18
19
if using_copy_on_write :
@@ -27,7 +28,7 @@ def test_replace_categorical_inplace_reference(using_copy_on_write):
27
28
def test_replace_inplace_reference (using_copy_on_write ):
28
29
df = DataFrame ({"a" : [1.5 , 2 , 3 ]})
29
30
arr_a = get_array (df , "a" )
30
- view = df [:] # noqa
31
+ view = df [:]
31
32
df .replace (to_replace = [1.5 ], value = 15.5 , inplace = True )
32
33
33
34
if using_copy_on_write :
@@ -36,3 +37,22 @@ def test_replace_inplace_reference(using_copy_on_write):
36
37
assert view ._mgr ._has_no_reference (0 )
37
38
else :
38
39
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