16
16
from pandas ._config import using_pyarrow_string_dtype
17
17
18
18
from pandas ._libs import index as libindex
19
- from pandas .compat .numpy import np_version_gt2
20
19
from pandas .errors import IndexingError
21
20
22
21
import pandas as pd
@@ -383,12 +382,8 @@ def test_loc_setitem_slice(self):
383
382
df2 = DataFrame ({"a" : [0 , 1 , 1 ], "b" : [100 , 200 , 300 ]}, dtype = "uint64" )
384
383
ix = df1 ["a" ] == 1
385
384
newb2 = df2 .loc [ix , "b" ]
386
- with tm .assert_produces_warning (
387
- FutureWarning , match = "item of incompatible dtype"
388
- ):
385
+ with pytest .raises (TypeError , match = "Invalid value" ):
389
386
df1 .loc [ix , "b" ] = newb2
390
- expected = DataFrame ({"a" : [0 , 1 , 1 ], "b" : [100 , 200 , 300 ]}, dtype = "uint64" )
391
- tm .assert_frame_equal (df2 , expected )
392
387
393
388
def test_loc_setitem_dtype (self ):
394
389
# GH31340
@@ -572,54 +567,31 @@ def frame_for_consistency(self):
572
567
def test_loc_setitem_consistency (self , frame_for_consistency , val ):
573
568
# GH 6149
574
569
# coerce similarly for setitem and loc when rows have a null-slice
575
- expected = DataFrame (
576
- {
577
- "date" : Series (0 , index = range (5 ), dtype = np .int64 ),
578
- "val" : Series (range (5 ), dtype = np .int64 ),
579
- }
580
- )
581
570
df = frame_for_consistency .copy ()
582
- with tm . assert_produces_warning ( FutureWarning , match = "incompatible dtype " ):
571
+ with pytest . raises ( TypeError , match = "Invalid value " ):
583
572
df .loc [:, "date" ] = val
584
- tm .assert_frame_equal (df , expected )
585
573
586
574
def test_loc_setitem_consistency_dt64_to_str (self , frame_for_consistency ):
587
575
# GH 6149
588
576
# coerce similarly for setitem and loc when rows have a null-slice
589
577
590
- expected = DataFrame (
591
- {
592
- "date" : Series ("foo" , index = range (5 )),
593
- "val" : Series (range (5 ), dtype = np .int64 ),
594
- }
595
- )
596
578
df = frame_for_consistency .copy ()
597
- with tm . assert_produces_warning ( FutureWarning , match = "incompatible dtype " ):
579
+ with pytest . raises ( TypeError , match = "Invalid value " ):
598
580
df .loc [:, "date" ] = "foo"
599
- tm .assert_frame_equal (df , expected )
600
581
601
582
def test_loc_setitem_consistency_dt64_to_float (self , frame_for_consistency ):
602
583
# GH 6149
603
584
# coerce similarly for setitem and loc when rows have a null-slice
604
- expected = DataFrame (
605
- {
606
- "date" : Series (1.0 , index = range (5 )),
607
- "val" : Series (range (5 ), dtype = np .int64 ),
608
- }
609
- )
610
585
df = frame_for_consistency .copy ()
611
- with tm . assert_produces_warning ( FutureWarning , match = "incompatible dtype " ):
586
+ with pytest . raises ( TypeError , match = "Invalid value " ):
612
587
df .loc [:, "date" ] = 1.0
613
- tm .assert_frame_equal (df , expected )
614
588
615
589
def test_loc_setitem_consistency_single_row (self ):
616
590
# GH 15494
617
591
# setting on frame with single row
618
592
df = DataFrame ({"date" : Series ([Timestamp ("20180101" )])})
619
- with tm . assert_produces_warning ( FutureWarning , match = "incompatible dtype " ):
593
+ with pytest . raises ( TypeError , match = "Invalid value " ):
620
594
df .loc [:, "date" ] = "string"
621
- expected = DataFrame ({"date" : Series (["string" ])})
622
- tm .assert_frame_equal (df , expected )
623
595
624
596
def test_loc_setitem_consistency_empty (self ):
625
597
# empty (essentially noops)
@@ -677,16 +649,11 @@ def test_loc_setitem_consistency_slice_column_len(self):
677
649
678
650
# timedelta64[m] -> float, so this cannot be done inplace, so
679
651
# no warning
680
- with tm . assert_produces_warning ( FutureWarning , match = "incompatible dtype " ):
652
+ with pytest . raises ( TypeError , match = "Invalid value " ):
681
653
df .loc [:, ("Respondent" , "Duration" )] = df .loc [
682
654
:, ("Respondent" , "Duration" )
683
655
] / Timedelta (60_000_000_000 )
684
656
685
- expected = Series (
686
- [23.0 , 12.0 , 14.0 , 36.0 ], index = df .index , name = ("Respondent" , "Duration" )
687
- )
688
- tm .assert_series_equal (df [("Respondent" , "Duration" )], expected )
689
-
690
657
@pytest .mark .parametrize ("unit" , ["Y" , "M" , "D" , "h" , "m" , "s" , "ms" , "us" ])
691
658
def test_loc_assign_non_ns_datetime (self , unit ):
692
659
# GH 27395, non-ns dtype assignment via .loc should work
@@ -1413,13 +1380,9 @@ def test_loc_setitem_categorical_values_partial_column_slice(self):
1413
1380
# Assigning a Category to parts of a int/... column uses the values of
1414
1381
# the Categorical
1415
1382
df = DataFrame ({"a" : [1 , 1 , 1 , 1 , 1 ], "b" : list ("aaaaa" )})
1416
- exp = DataFrame ({"a" : [1 , "b" , "b" , 1 , 1 ], "b" : list ("aabba" )})
1417
- with tm .assert_produces_warning (
1418
- FutureWarning , match = "item of incompatible dtype"
1419
- ):
1383
+ with pytest .raises (TypeError , match = "Invalid value" ):
1420
1384
df .loc [1 :2 , "a" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
1421
1385
df .loc [2 :3 , "b" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
1422
- tm .assert_frame_equal (df , exp )
1423
1386
1424
1387
def test_loc_setitem_single_row_categorical (self , using_infer_string ):
1425
1388
# GH#25495
@@ -1446,9 +1409,8 @@ def test_loc_setitem_datetime_coercion(self):
1446
1409
df .loc [0 :1 , "c" ] = np .datetime64 ("2008-08-08" )
1447
1410
assert Timestamp ("2008-08-08" ) == df .loc [0 , "c" ]
1448
1411
assert Timestamp ("2008-08-08" ) == df .loc [1 , "c" ]
1449
- with tm . assert_produces_warning ( FutureWarning , match = "incompatible dtype " ):
1412
+ with pytest . raises ( TypeError , match = "Invalid value " ):
1450
1413
df .loc [2 , "c" ] = date (2005 , 5 , 5 )
1451
- assert Timestamp ("2005-05-05" ).date () == df .loc [2 , "c" ]
1452
1414
1453
1415
@pytest .mark .parametrize ("idxer" , ["var" , ["var" ]])
1454
1416
def test_loc_setitem_datetimeindex_tz (self , idxer , tz_naive_fixture ):
@@ -1459,12 +1421,13 @@ def test_loc_setitem_datetimeindex_tz(self, idxer, tz_naive_fixture):
1459
1421
# if result started off with object dtype, then the .loc.__setitem__
1460
1422
# below would retain object dtype
1461
1423
result = DataFrame (index = idx , columns = ["var" ], dtype = np .float64 )
1462
- with tm .assert_produces_warning (
1463
- FutureWarning if idxer == "var" else None , match = "incompatible dtype"
1464
- ):
1424
+ if idxer == "var" :
1425
+ with pytest .raises (TypeError , match = "Invalid value" ):
1426
+ result .loc [:, idxer ] = expected
1427
+ else :
1465
1428
# See https://github.com/pandas-dev/pandas/issues/56223
1466
1429
result .loc [:, idxer ] = expected
1467
- tm .assert_frame_equal (result , expected )
1430
+ tm .assert_frame_equal (result , expected )
1468
1431
1469
1432
def test_loc_setitem_time_key (self ):
1470
1433
index = date_range ("2012-01-01" , "2012-01-05" , freq = "30min" )
@@ -1610,16 +1573,8 @@ def test_loc_setitem_cast2(self):
1610
1573
# dtype conversion on setting
1611
1574
df = DataFrame (np .random .default_rng (2 ).random ((30 , 3 )), columns = tuple ("ABC" ))
1612
1575
df ["event" ] = np .nan
1613
- with tm .assert_produces_warning (
1614
- FutureWarning , match = "item of incompatible dtype"
1615
- ):
1576
+ with pytest .raises (TypeError , match = "Invalid value" ):
1616
1577
df .loc [10 , "event" ] = "foo"
1617
- result = df .dtypes
1618
- expected = Series (
1619
- [np .dtype ("float64" )] * 3 + [np .dtype ("object" )],
1620
- index = ["A" , "B" , "C" , "event" ],
1621
- )
1622
- tm .assert_series_equal (result , expected )
1623
1578
1624
1579
def test_loc_setitem_cast3 (self ):
1625
1580
# Test that data type is preserved . GH#5782
@@ -2974,20 +2929,9 @@ def test_loc_setitem_uint8_upcast(value):
2974
2929
# GH#26049
2975
2930
2976
2931
df = DataFrame ([1 , 2 , 3 , 4 ], columns = ["col1" ], dtype = "uint8" )
2977
- with tm . assert_produces_warning ( FutureWarning , match = "item of incompatible dtype " ):
2932
+ with pytest . raises ( TypeError , match = "Invalid value " ):
2978
2933
df .loc [2 , "col1" ] = value # value that can't be held in uint8
2979
2934
2980
- if np_version_gt2 and isinstance (value , np .int16 ):
2981
- # Note, result type of uint8 + int16 is int16
2982
- # in numpy < 2, though, numpy would inspect the
2983
- # value and see that it could fit in an uint16, resulting in a uint16
2984
- dtype = "int16"
2985
- else :
2986
- dtype = "uint16"
2987
-
2988
- expected = DataFrame ([1 , 2 , 300 , 4 ], columns = ["col1" ], dtype = dtype )
2989
- tm .assert_frame_equal (df , expected )
2990
-
2991
2935
2992
2936
@pytest .mark .parametrize (
2993
2937
"fill_val,exp_dtype" ,
0 commit comments