11"""test label based indexing with loc"""
22
33from collections import namedtuple
4+ import contextlib
45from datetime import (
56 date ,
67 datetime ,
1314import numpy as np
1415import pytest
1516
16- from pandas ._config import using_string_dtype
17-
1817from pandas ._libs import index as libindex
19- from pandas .compat import HAS_PYARROW
2018from pandas .errors import IndexingError
2119
2220import pandas as pd
@@ -615,8 +613,7 @@ def test_loc_setitem_consistency_empty(self):
615613 expected ["x" ] = expected ["x" ].astype (np .int64 )
616614 tm .assert_frame_equal (df , expected )
617615
618- @pytest .mark .xfail (using_string_dtype (), reason = "TODO(infer_string)" )
619- def test_loc_setitem_consistency_slice_column_len (self ):
616+ def test_loc_setitem_consistency_slice_column_len (self , using_infer_string ):
620617 # .loc[:,column] setting with slice == len of the column
621618 # GH10408
622619 levels = [
@@ -640,12 +637,23 @@ def test_loc_setitem_consistency_slice_column_len(self):
640637 ]
641638 df = DataFrame (values , index = mi , columns = cols )
642639
643- df .loc [:, ("Respondent" , "StartDate" )] = to_datetime (
644- df .loc [:, ("Respondent" , "StartDate" )]
645- )
646- df .loc [:, ("Respondent" , "EndDate" )] = to_datetime (
647- df .loc [:, ("Respondent" , "EndDate" )]
648- )
640+ ctx = contextlib .nullcontext ()
641+ if using_infer_string :
642+ ctx = pytest .raises (TypeError , match = "Invalid value" )
643+
644+ with ctx :
645+ df .loc [:, ("Respondent" , "StartDate" )] = to_datetime (
646+ df .loc [:, ("Respondent" , "StartDate" )]
647+ )
648+ with ctx :
649+ df .loc [:, ("Respondent" , "EndDate" )] = to_datetime (
650+ df .loc [:, ("Respondent" , "EndDate" )]
651+ )
652+
653+ if using_infer_string :
654+ # infer-objects won't infer stuff anymore
655+ return
656+
649657 df = df .infer_objects ()
650658
651659 # Adding a new key
@@ -1211,20 +1219,23 @@ def test_loc_reverse_assignment(self):
12111219
12121220 tm .assert_series_equal (result , expected )
12131221
1214- @pytest .mark .xfail (using_string_dtype (), reason = "can't set int into string" )
1215- def test_loc_setitem_str_to_small_float_conversion_type (self ):
1222+ def test_loc_setitem_str_to_small_float_conversion_type (self , using_infer_string ):
12161223 # GH#20388
12171224
12181225 col_data = [str (np .random .default_rng (2 ).random () * 1e-12 ) for _ in range (5 )]
12191226 result = DataFrame (col_data , columns = ["A" ])
1220- expected = DataFrame (col_data , columns = ["A" ], dtype = object )
1227+ expected = DataFrame (col_data , columns = ["A" ])
12211228 tm .assert_frame_equal (result , expected )
12221229
12231230 # assigning with loc/iloc attempts to set the values inplace, which
12241231 # in this case is successful
1225- result .loc [result .index , "A" ] = [float (x ) for x in col_data ]
1226- expected = DataFrame (col_data , columns = ["A" ], dtype = float ).astype (object )
1227- tm .assert_frame_equal (result , expected )
1232+ if using_infer_string :
1233+ with pytest .raises (TypeError , match = "Must provide strings" ):
1234+ result .loc [result .index , "A" ] = [float (x ) for x in col_data ]
1235+ else :
1236+ result .loc [result .index , "A" ] = [float (x ) for x in col_data ]
1237+ expected = DataFrame (col_data , columns = ["A" ], dtype = float ).astype (object )
1238+ tm .assert_frame_equal (result , expected )
12281239
12291240 # assigning the entire column using __setitem__ swaps in the new array
12301241 # GH#???
@@ -1389,9 +1400,6 @@ def test_loc_setitem_categorical_values_partial_column_slice(self):
13891400 df .loc [1 :2 , "a" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
13901401 df .loc [2 :3 , "b" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
13911402
1392- @pytest .mark .xfail (
1393- using_string_dtype () and not HAS_PYARROW , reason = "TODO(infer_string)"
1394- )
13951403 def test_loc_setitem_single_row_categorical (self , using_infer_string ):
13961404 # GH#25495
13971405 df = DataFrame ({"Alpha" : ["a" ], "Numeric" : [0 ]})
0 commit comments