21
21
22
22
23
23
@pytest .mark .parametrize ("dtype" , [None , "int64" ])
24
- def test_series_from_series (dtype , using_copy_on_write ):
24
+ def test_series_from_series (dtype , using_copy_on_write , warn_copy_on_write ):
25
25
# Case: constructing a Series from another Series object follows CoW rules:
26
26
# a new object is returned and thus mutations are not propagated
27
27
ser = Series ([1 , 2 , 3 ], name = "name" )
@@ -43,7 +43,8 @@ def test_series_from_series(dtype, using_copy_on_write):
43
43
assert not np .shares_memory (get_array (ser ), get_array (result ))
44
44
else :
45
45
# mutating shallow copy does mutate original
46
- result .iloc [0 ] = 0
46
+ with tm .assert_cow_warning (warn_copy_on_write ):
47
+ result .iloc [0 ] = 0
47
48
assert ser .iloc [0 ] == 0
48
49
# and still shares memory
49
50
assert np .shares_memory (get_array (ser ), get_array (result ))
@@ -57,11 +58,12 @@ def test_series_from_series(dtype, using_copy_on_write):
57
58
assert result .iloc [0 ] == 1
58
59
else :
59
60
# mutating original does mutate shallow copy
60
- ser .iloc [0 ] = 0
61
+ with tm .assert_cow_warning (warn_copy_on_write ):
62
+ ser .iloc [0 ] = 0
61
63
assert result .iloc [0 ] == 0
62
64
63
65
64
- def test_series_from_series_with_reindex (using_copy_on_write ):
66
+ def test_series_from_series_with_reindex (using_copy_on_write , warn_copy_on_write ):
65
67
# Case: constructing a Series from another Series with specifying an index
66
68
# that potentially requires a reindex of the values
67
69
ser = Series ([1 , 2 , 3 ], name = "name" )
@@ -76,7 +78,8 @@ def test_series_from_series_with_reindex(using_copy_on_write):
76
78
]:
77
79
result = Series (ser , index = index )
78
80
assert np .shares_memory (ser .values , result .values )
79
- result .iloc [0 ] = 0
81
+ with tm .assert_cow_warning (warn_copy_on_write ):
82
+ result .iloc [0 ] = 0
80
83
if using_copy_on_write :
81
84
assert ser .iloc [0 ] == 1
82
85
else :
@@ -153,6 +156,7 @@ def test_series_from_index_different_dtypes(using_copy_on_write):
153
156
assert ser ._mgr ._has_no_reference (0 )
154
157
155
158
159
+ @pytest .mark .filterwarnings ("ignore:Setting a value on a view:FutureWarning" )
156
160
@pytest .mark .parametrize ("fastpath" , [False , True ])
157
161
@pytest .mark .parametrize ("dtype" , [None , "int64" ])
158
162
@pytest .mark .parametrize ("idx" , [None , pd .RangeIndex (start = 0 , stop = 3 , step = 1 )])
@@ -186,7 +190,9 @@ def test_series_from_block_manager_different_dtype(using_copy_on_write):
186
190
187
191
@pytest .mark .parametrize ("use_mgr" , [True , False ])
188
192
@pytest .mark .parametrize ("columns" , [None , ["a" ]])
189
- def test_dataframe_constructor_mgr_or_df (using_copy_on_write , columns , use_mgr ):
193
+ def test_dataframe_constructor_mgr_or_df (
194
+ using_copy_on_write , warn_copy_on_write , columns , use_mgr
195
+ ):
190
196
df = DataFrame ({"a" : [1 , 2 , 3 ]})
191
197
df_orig = df .copy ()
192
198
@@ -201,7 +207,8 @@ def test_dataframe_constructor_mgr_or_df(using_copy_on_write, columns, use_mgr):
201
207
new_df = DataFrame (data )
202
208
203
209
assert np .shares_memory (get_array (df , "a" ), get_array (new_df , "a" ))
204
- new_df .iloc [0 ] = 100
210
+ with tm .assert_cow_warning (warn_copy_on_write and not use_mgr ):
211
+ new_df .iloc [0 ] = 100
205
212
206
213
if using_copy_on_write :
207
214
assert not np .shares_memory (get_array (df , "a" ), get_array (new_df , "a" ))
@@ -215,7 +222,7 @@ def test_dataframe_constructor_mgr_or_df(using_copy_on_write, columns, use_mgr):
215
222
@pytest .mark .parametrize ("index" , [None , [0 , 1 , 2 ]])
216
223
@pytest .mark .parametrize ("columns" , [None , ["a" , "b" ], ["a" , "b" , "c" ]])
217
224
def test_dataframe_from_dict_of_series (
218
- request , using_copy_on_write , columns , index , dtype
225
+ request , using_copy_on_write , warn_copy_on_write , columns , index , dtype
219
226
):
220
227
# Case: constructing a DataFrame from Series objects with copy=False
221
228
# has to do a lazy following CoW rules
@@ -235,6 +242,7 @@ def test_dataframe_from_dict_of_series(
235
242
assert np .shares_memory (get_array (result , "a" ), get_array (s1 ))
236
243
237
244
# mutating the new dataframe doesn't mutate original
245
+ # TODO(CoW-warn) this should also warn
238
246
result .iloc [0 , 0 ] = 10
239
247
if using_copy_on_write :
240
248
assert not np .shares_memory (get_array (result , "a" ), get_array (s1 ))
@@ -248,7 +256,8 @@ def test_dataframe_from_dict_of_series(
248
256
result = DataFrame (
249
257
{"a" : s1 , "b" : s2 }, index = index , columns = columns , dtype = dtype , copy = False
250
258
)
251
- s1 .iloc [0 ] = 10
259
+ with tm .assert_cow_warning (warn_copy_on_write ):
260
+ s1 .iloc [0 ] = 10
252
261
if using_copy_on_write :
253
262
assert not np .shares_memory (get_array (result , "a" ), get_array (s1 ))
254
263
tm .assert_frame_equal (result , expected )
@@ -278,15 +287,19 @@ def test_dataframe_from_dict_of_series_with_reindex(dtype):
278
287
@pytest .mark .parametrize (
279
288
"data, dtype" , [([1 , 2 ], None ), ([1 , 2 ], "int64" ), (["a" , "b" ], None )]
280
289
)
281
- def test_dataframe_from_series_or_index (using_copy_on_write , data , dtype , cons ):
290
+ def test_dataframe_from_series_or_index (
291
+ using_copy_on_write , warn_copy_on_write , data , dtype , cons
292
+ ):
282
293
obj = cons (data , dtype = dtype )
283
294
obj_orig = obj .copy ()
284
295
df = DataFrame (obj , dtype = dtype )
285
296
assert np .shares_memory (get_array (obj ), get_array (df , 0 ))
286
297
if using_copy_on_write :
287
298
assert not df ._mgr ._has_no_reference (0 )
288
299
289
- df .iloc [0 , 0 ] = data [- 1 ]
300
+ # TODO(CoW-warn) should not warn for an index?
301
+ with tm .assert_cow_warning (warn_copy_on_write ):
302
+ df .iloc [0 , 0 ] = data [- 1 ]
290
303
if using_copy_on_write :
291
304
tm .assert_equal (obj , obj_orig )
292
305
@@ -341,15 +354,16 @@ def test_frame_from_numpy_array(using_copy_on_write, copy, using_array_manager):
341
354
assert np .shares_memory (get_array (df , 0 ), arr )
342
355
343
356
344
- def test_dataframe_from_records_with_dataframe (using_copy_on_write ):
357
+ def test_dataframe_from_records_with_dataframe (using_copy_on_write , warn_copy_on_write ):
345
358
df = DataFrame ({"a" : [1 , 2 , 3 ]})
346
359
df_orig = df .copy ()
347
360
with tm .assert_produces_warning (FutureWarning ):
348
361
df2 = DataFrame .from_records (df )
349
362
if using_copy_on_write :
350
363
assert not df ._mgr ._has_no_reference (0 )
351
364
assert np .shares_memory (get_array (df , "a" ), get_array (df2 , "a" ))
352
- df2 .iloc [0 , 0 ] = 100
365
+ with tm .assert_cow_warning (warn_copy_on_write ):
366
+ df2 .iloc [0 , 0 ] = 100
353
367
if using_copy_on_write :
354
368
tm .assert_frame_equal (df , df_orig )
355
369
else :
0 commit comments