1
1
""" test label based indexing with loc """
2
2
from collections import namedtuple
3
+ import contextlib
3
4
from datetime import (
4
5
date ,
5
6
datetime ,
@@ -648,8 +649,9 @@ def test_loc_setitem_consistency_empty(self):
648
649
expected ["x" ] = expected ["x" ].astype (np .int64 )
649
650
tm .assert_frame_equal (df , expected )
650
651
652
+ # incompatible dtype warning
651
653
@pytest .mark .xfail (using_string_dtype (), reason = "TODO(infer_string)" )
652
- def test_loc_setitem_consistency_slice_column_len (self ):
654
+ def test_loc_setitem_consistency_slice_column_len (self , using_infer_string ):
653
655
# .loc[:,column] setting with slice == len of the column
654
656
# GH10408
655
657
levels = [
@@ -673,13 +675,24 @@ def test_loc_setitem_consistency_slice_column_len(self):
673
675
]
674
676
df = DataFrame (values , index = mi , columns = cols )
675
677
676
- df .loc [:, ("Respondent" , "StartDate" )] = to_datetime (
677
- df .loc [:, ("Respondent" , "StartDate" )]
678
- )
679
- df .loc [:, ("Respondent" , "EndDate" )] = to_datetime (
680
- df .loc [:, ("Respondent" , "EndDate" )]
681
- )
682
- df = df .infer_objects (copy = False )
678
+ ctx = contextlib .nullcontext ()
679
+ if using_infer_string :
680
+ ctx = pytest .raises (TypeError , match = "Invalid value" )
681
+
682
+ with ctx :
683
+ df .loc [:, ("Respondent" , "StartDate" )] = to_datetime (
684
+ df .loc [:, ("Respondent" , "StartDate" )]
685
+ )
686
+ with ctx :
687
+ df .loc [:, ("Respondent" , "EndDate" )] = to_datetime (
688
+ df .loc [:, ("Respondent" , "EndDate" )]
689
+ )
690
+
691
+ if using_infer_string :
692
+ # infer-objects won't infer stuff anymore
693
+ return
694
+
695
+ df = df .infer_objects ()
683
696
684
697
# Adding a new key
685
698
df .loc [:, ("Respondent" , "Duration" )] = (
@@ -1269,20 +1282,23 @@ def test_loc_reverse_assignment(self):
1269
1282
1270
1283
tm .assert_series_equal (result , expected )
1271
1284
1272
- @pytest .mark .xfail (using_string_dtype (), reason = "can't set int into string" )
1273
- def test_loc_setitem_str_to_small_float_conversion_type (self ):
1285
+ def test_loc_setitem_str_to_small_float_conversion_type (self , using_infer_string ):
1274
1286
# GH#20388
1275
1287
1276
1288
col_data = [str (np .random .default_rng (2 ).random () * 1e-12 ) for _ in range (5 )]
1277
1289
result = DataFrame (col_data , columns = ["A" ])
1278
- expected = DataFrame (col_data , columns = ["A" ], dtype = object )
1290
+ expected = DataFrame (col_data , columns = ["A" ])
1279
1291
tm .assert_frame_equal (result , expected )
1280
1292
1281
1293
# assigning with loc/iloc attempts to set the values inplace, which
1282
1294
# in this case is successful
1283
- result .loc [result .index , "A" ] = [float (x ) for x in col_data ]
1284
- expected = DataFrame (col_data , columns = ["A" ], dtype = float ).astype (object )
1285
- tm .assert_frame_equal (result , expected )
1295
+ if using_infer_string :
1296
+ with pytest .raises (TypeError , match = "Must provide strings" ):
1297
+ result .loc [result .index , "A" ] = [float (x ) for x in col_data ]
1298
+ else :
1299
+ result .loc [result .index , "A" ] = [float (x ) for x in col_data ]
1300
+ expected = DataFrame (col_data , columns = ["A" ], dtype = float ).astype (object )
1301
+ tm .assert_frame_equal (result , expected )
1286
1302
1287
1303
# assigning the entire column using __setitem__ swaps in the new array
1288
1304
# GH#???
@@ -1458,9 +1474,6 @@ def test_loc_setitem_categorical_values_partial_column_slice(self):
1458
1474
df .loc [2 :3 , "b" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
1459
1475
tm .assert_frame_equal (df , exp )
1460
1476
1461
- @pytest .mark .xfail (
1462
- using_string_dtype () and not HAS_PYARROW , reason = "TODO(infer_string)"
1463
- )
1464
1477
def test_loc_setitem_single_row_categorical (self , using_infer_string ):
1465
1478
# GH#25495
1466
1479
df = DataFrame ({"Alpha" : ["a" ], "Numeric" : [0 ]})
0 commit comments