@@ -32,7 +32,9 @@ def random_text(nobs=100):
32
32
33
33
34
34
class TestCaching :
35
- def test_slice_consolidate_invalidate_item_cache (self , using_copy_on_write ):
35
+ def test_slice_consolidate_invalidate_item_cache (
36
+ self , using_copy_on_write , warn_copy_on_write
37
+ ):
36
38
# this is chained assignment, but will 'work'
37
39
with option_context ("chained_assignment" , None ):
38
40
# #3970
@@ -49,7 +51,9 @@ def test_slice_consolidate_invalidate_item_cache(self, using_copy_on_write):
49
51
with tm .raises_chained_assignment_error ():
50
52
df ["bb" ].iloc [0 ] = 0.17
51
53
else :
52
- df ["bb" ].iloc [0 ] = 0.17
54
+ # TODO(CoW-warn) custom warning message
55
+ with tm .assert_cow_warning (warn_copy_on_write ):
56
+ df ["bb" ].iloc [0 ] = 0.17
53
57
df ._clear_item_cache ()
54
58
if not using_copy_on_write :
55
59
tm .assert_almost_equal (df ["bb" ][0 ], 0.17 )
@@ -74,7 +78,9 @@ def test_setitem_cache_updating(self, do_ref):
74
78
assert df .loc [0 , "c" ] == 0.0
75
79
assert df .loc [7 , "c" ] == 1.0
76
80
77
- def test_setitem_cache_updating_slices (self , using_copy_on_write ):
81
+ def test_setitem_cache_updating_slices (
82
+ self , using_copy_on_write , warn_copy_on_write
83
+ ):
78
84
# GH 7084
79
85
# not updating cache on series setting with slices
80
86
expected = DataFrame (
@@ -102,7 +108,8 @@ def test_setitem_cache_updating_slices(self, using_copy_on_write):
102
108
with tm .raises_chained_assignment_error ():
103
109
out [row ["C" ]][six :eix ] = v
104
110
else :
105
- out [row ["C" ]][six :eix ] = v
111
+ with tm .assert_cow_warning (warn_copy_on_write ):
112
+ out [row ["C" ]][six :eix ] = v
106
113
107
114
if not using_copy_on_write :
108
115
tm .assert_frame_equal (out , expected )
@@ -113,17 +120,21 @@ def test_setitem_cache_updating_slices(self, using_copy_on_write):
113
120
114
121
out = DataFrame ({"A" : [0 , 0 , 0 ]}, index = date_range ("5/7/2014" , "5/9/2014" ))
115
122
for ix , row in df .iterrows ():
116
- out .loc [six :eix , row ["C" ]] += row ["D" ]
123
+ # TODO(CoW-warn) should not warn
124
+ with tm .assert_produces_warning (FutureWarning ):
125
+ out .loc [six :eix , row ["C" ]] += row ["D" ]
117
126
118
127
tm .assert_frame_equal (out , expected )
119
128
tm .assert_series_equal (out ["A" ], expected ["A" ])
120
129
121
- def test_altering_series_clears_parent_cache (self , using_copy_on_write ):
130
+ def test_altering_series_clears_parent_cache (
131
+ self , using_copy_on_write , warn_copy_on_write
132
+ ):
122
133
# GH #33675
123
134
df = DataFrame ([[1 , 2 ], [3 , 4 ]], index = ["a" , "b" ], columns = ["A" , "B" ])
124
135
ser = df ["A" ]
125
136
126
- if using_copy_on_write :
137
+ if using_copy_on_write or warn_copy_on_write :
127
138
assert "A" not in df ._item_cache
128
139
else :
129
140
assert "A" in df ._item_cache
@@ -138,7 +149,7 @@ def test_altering_series_clears_parent_cache(self, using_copy_on_write):
138
149
139
150
140
151
class TestChaining :
141
- def test_setitem_chained_setfault (self , using_copy_on_write ):
152
+ def test_setitem_chained_setfault (self , using_copy_on_write , warn_copy_on_write ):
142
153
# GH6026
143
154
data = ["right" , "left" , "left" , "left" , "right" , "left" , "timeout" ]
144
155
mdata = ["right" , "left" , "left" , "left" , "right" , "left" , "none" ]
@@ -150,6 +161,8 @@ def test_setitem_chained_setfault(self, using_copy_on_write):
150
161
df .response [mask ] = "none"
151
162
tm .assert_frame_equal (df , DataFrame ({"response" : data }))
152
163
else :
164
+ # TODO(CoW-warn) should warn
165
+ # with tm.assert_cow_warning(warn_copy_on_write):
153
166
df .response [mask ] = "none"
154
167
tm .assert_frame_equal (df , DataFrame ({"response" : mdata }))
155
168
@@ -161,6 +174,8 @@ def test_setitem_chained_setfault(self, using_copy_on_write):
161
174
df .response [mask ] = "none"
162
175
tm .assert_frame_equal (df , DataFrame ({"response" : data }))
163
176
else :
177
+ # TODO(CoW-warn) should warn
178
+ # with tm.assert_cow_warning(warn_copy_on_write):
164
179
df .response [mask ] = "none"
165
180
tm .assert_frame_equal (df , DataFrame ({"response" : mdata }))
166
181
@@ -172,6 +187,8 @@ def test_setitem_chained_setfault(self, using_copy_on_write):
172
187
df .response [mask ] = "none"
173
188
tm .assert_frame_equal (df , df_original )
174
189
else :
190
+ # TODO(CoW-warn) should warn
191
+ # with tm.assert_cow_warning(warn_copy_on_write):
175
192
df .response [mask ] = "none"
176
193
tm .assert_frame_equal (df , DataFrame ({"response" : mdata , "response1" : data }))
177
194
@@ -183,7 +200,8 @@ def test_setitem_chained_setfault(self, using_copy_on_write):
183
200
df ["A" ].iloc [0 ] = np .nan
184
201
expected = DataFrame ({"A" : ["foo" , "bar" , "bah" , "foo" , "bar" ]})
185
202
else :
186
- df ["A" ].iloc [0 ] = np .nan
203
+ with tm .assert_cow_warning (warn_copy_on_write ):
204
+ df ["A" ].iloc [0 ] = np .nan
187
205
expected = DataFrame ({"A" : [np .nan , "bar" , "bah" , "foo" , "bar" ]})
188
206
result = df .head ()
189
207
tm .assert_frame_equal (result , expected )
@@ -193,7 +211,8 @@ def test_setitem_chained_setfault(self, using_copy_on_write):
193
211
with tm .raises_chained_assignment_error ():
194
212
df .A .iloc [0 ] = np .nan
195
213
else :
196
- df .A .iloc [0 ] = np .nan
214
+ with tm .assert_cow_warning (warn_copy_on_write ):
215
+ df .A .iloc [0 ] = np .nan
197
216
result = df .head ()
198
217
tm .assert_frame_equal (result , expected )
199
218
@@ -636,7 +655,9 @@ def test_cache_updating2(self, using_copy_on_write):
636
655
expected = Series ([0 , 0 , 0 , 2 , 0 ], name = "f" )
637
656
tm .assert_series_equal (df .f , expected )
638
657
639
- def test_iloc_setitem_chained_assignment (self , using_copy_on_write ):
658
+ def test_iloc_setitem_chained_assignment (
659
+ self , using_copy_on_write , warn_copy_on_write
660
+ ):
640
661
# GH#3970
641
662
with option_context ("chained_assignment" , None ):
642
663
df = DataFrame ({"aa" : range (5 ), "bb" : [2.2 ] * 5 })
@@ -648,7 +669,8 @@ def test_iloc_setitem_chained_assignment(self, using_copy_on_write):
648
669
with tm .raises_chained_assignment_error ():
649
670
df ["bb" ].iloc [0 ] = 0.13
650
671
else :
651
- df ["bb" ].iloc [0 ] = 0.13
672
+ with tm .assert_cow_warning (warn_copy_on_write ):
673
+ df ["bb" ].iloc [0 ] = 0.13
652
674
653
675
# GH#3970 this lookup used to break the chained setting to 0.15
654
676
df .iloc [ck ]
@@ -657,7 +679,8 @@ def test_iloc_setitem_chained_assignment(self, using_copy_on_write):
657
679
with tm .raises_chained_assignment_error ():
658
680
df ["bb" ].iloc [0 ] = 0.15
659
681
else :
660
- df ["bb" ].iloc [0 ] = 0.15
682
+ with tm .assert_cow_warning (warn_copy_on_write ):
683
+ df ["bb" ].iloc [0 ] = 0.15
661
684
662
685
if not using_copy_on_write :
663
686
assert df ["bb" ].iloc [0 ] == 0.15
0 commit comments