11""" test label based indexing with loc """
22from collections import namedtuple
3+ import contextlib
34from datetime import (
45 date ,
56 datetime ,
@@ -648,8 +649,9 @@ def test_loc_setitem_consistency_empty(self):
648649 expected ["x" ] = expected ["x" ].astype (np .int64 )
649650 tm .assert_frame_equal (df , expected )
650651
652+ # incompatible dtype warning
651653 @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 ):
653655 # .loc[:,column] setting with slice == len of the column
654656 # GH10408
655657 levels = [
@@ -673,13 +675,24 @@ def test_loc_setitem_consistency_slice_column_len(self):
673675 ]
674676 df = DataFrame (values , index = mi , columns = cols )
675677
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 ()
683696
684697 # Adding a new key
685698 df .loc [:, ("Respondent" , "Duration" )] = (
@@ -1269,20 +1282,23 @@ def test_loc_reverse_assignment(self):
12691282
12701283 tm .assert_series_equal (result , expected )
12711284
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 ):
12741286 # GH#20388
12751287
12761288 col_data = [str (np .random .default_rng (2 ).random () * 1e-12 ) for _ in range (5 )]
12771289 result = DataFrame (col_data , columns = ["A" ])
1278- expected = DataFrame (col_data , columns = ["A" ], dtype = object )
1290+ expected = DataFrame (col_data , columns = ["A" ])
12791291 tm .assert_frame_equal (result , expected )
12801292
12811293 # assigning with loc/iloc attempts to set the values inplace, which
12821294 # 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 )
12861302
12871303 # assigning the entire column using __setitem__ swaps in the new array
12881304 # GH#???
@@ -1458,9 +1474,6 @@ def test_loc_setitem_categorical_values_partial_column_slice(self):
14581474 df .loc [2 :3 , "b" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
14591475 tm .assert_frame_equal (df , exp )
14601476
1461- @pytest .mark .xfail (
1462- using_string_dtype () and not HAS_PYARROW , reason = "TODO(infer_string)"
1463- )
14641477 def test_loc_setitem_single_row_categorical (self , using_infer_string ):
14651478 # GH#25495
14661479 df = DataFrame ({"Alpha" : ["a" ], "Numeric" : [0 ]})
0 commit comments