@@ -382,7 +382,10 @@ def test_loc_setitem_slice(self):
382
382
df2 = DataFrame ({"a" : [0 , 1 , 1 ], "b" : [100 , 200 , 300 ]}, dtype = "uint64" )
383
383
ix = df1 ["a" ] == 1
384
384
newb2 = df2 .loc [ix , "b" ]
385
- df1 .loc [ix , "b" ] = newb2
385
+ with tm .assert_produces_warning (
386
+ FutureWarning , match = "item of incompatible dtype"
387
+ ):
388
+ df1 .loc [ix , "b" ] = newb2
386
389
expected = DataFrame ({"a" : [0 , 1 , 1 ], "b" : [100 , 200 , 300 ]}, dtype = "uint64" )
387
390
tm .assert_frame_equal (df2 , expected )
388
391
@@ -869,7 +872,10 @@ def test_loc_setitem_with_scalar_index(self, indexer, value):
869
872
# elementwisely, not using "setter('A', ['Z'])".
870
873
871
874
df = DataFrame ([[1 , 2 ], [3 , 4 ]], columns = ["A" , "B" ])
872
- df .loc [0 , indexer ] = value
875
+ with tm .assert_produces_warning (
876
+ FutureWarning , match = "item of incompatible dtype"
877
+ ):
878
+ df .loc [0 , indexer ] = value
873
879
result = df .loc [0 , "A" ]
874
880
875
881
assert is_scalar (result ) and result == "Z"
@@ -1415,8 +1421,11 @@ def test_loc_setitem_categorical_values_partial_column_slice(self):
1415
1421
# the Categorical
1416
1422
df = DataFrame ({"a" : [1 , 1 , 1 , 1 , 1 ], "b" : list ("aaaaa" )})
1417
1423
exp = DataFrame ({"a" : [1 , "b" , "b" , 1 , 1 ], "b" : list ("aabba" )})
1418
- df .loc [1 :2 , "a" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
1419
- df .loc [2 :3 , "b" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
1424
+ with tm .assert_produces_warning (
1425
+ FutureWarning , match = "item of incompatible dtype"
1426
+ ):
1427
+ df .loc [1 :2 , "a" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
1428
+ df .loc [2 :3 , "b" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
1420
1429
tm .assert_frame_equal (df , exp )
1421
1430
1422
1431
def test_loc_setitem_single_row_categorical (self ):
@@ -1537,11 +1546,17 @@ def test_loc_setitem_2d_to_1d_raises(self):
1537
1546
]
1538
1547
)
1539
1548
with pytest .raises (ValueError , match = msg ):
1540
- ser .loc [range (2 )] = data
1549
+ with tm .assert_produces_warning (
1550
+ FutureWarning , match = "item of incompatible dtype"
1551
+ ):
1552
+ ser .loc [range (2 )] = data
1541
1553
1542
1554
msg = r"could not broadcast input array from shape \(2,2\) into shape \(2,?\)"
1543
1555
with pytest .raises (ValueError , match = msg ):
1544
- ser .loc [:] = data
1556
+ with tm .assert_produces_warning (
1557
+ FutureWarning , match = "item of incompatible dtype"
1558
+ ):
1559
+ ser .loc [:] = data
1545
1560
1546
1561
def test_loc_getitem_interval_index (self ):
1547
1562
# GH#19977
@@ -1607,7 +1622,10 @@ def test_loc_setitem_cast2(self):
1607
1622
# dtype conversion on setting
1608
1623
df = DataFrame (np .random .rand (30 , 3 ), columns = tuple ("ABC" ))
1609
1624
df ["event" ] = np .nan
1610
- df .loc [10 , "event" ] = "foo"
1625
+ with tm .assert_produces_warning (
1626
+ FutureWarning , match = "item of incompatible dtype"
1627
+ ):
1628
+ df .loc [10 , "event" ] = "foo"
1611
1629
result = df .dtypes
1612
1630
expected = Series (
1613
1631
[np .dtype ("float64" )] * 3 + [np .dtype ("object" )],
@@ -2910,7 +2928,8 @@ def test_loc_setitem_uint8_upcast(value):
2910
2928
# GH#26049
2911
2929
2912
2930
df = DataFrame ([1 , 2 , 3 , 4 ], columns = ["col1" ], dtype = "uint8" )
2913
- df .loc [2 , "col1" ] = value # value that can't be held in uint8
2931
+ with tm .assert_produces_warning (FutureWarning , match = "item of incompatible dtype" ):
2932
+ df .loc [2 , "col1" ] = value # value that can't be held in uint8
2914
2933
2915
2934
expected = DataFrame ([1 , 2 , 300 , 4 ], columns = ["col1" ], dtype = "uint16" )
2916
2935
tm .assert_frame_equal (df , expected )
0 commit comments