1
1
"""test label based indexing with loc"""
2
2
3
3
from collections import namedtuple
4
+ import contextlib
4
5
from datetime import (
5
6
date ,
6
7
datetime ,
13
14
import numpy as np
14
15
import pytest
15
16
16
- from pandas ._config import using_string_dtype
17
-
18
17
from pandas ._libs import index as libindex
19
- from pandas .compat import HAS_PYARROW
20
18
from pandas .errors import IndexingError
21
19
22
20
import pandas as pd
@@ -615,8 +613,7 @@ def test_loc_setitem_consistency_empty(self):
615
613
expected ["x" ] = expected ["x" ].astype (np .int64 )
616
614
tm .assert_frame_equal (df , expected )
617
615
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 ):
620
617
# .loc[:,column] setting with slice == len of the column
621
618
# GH10408
622
619
levels = [
@@ -640,12 +637,23 @@ def test_loc_setitem_consistency_slice_column_len(self):
640
637
]
641
638
df = DataFrame (values , index = mi , columns = cols )
642
639
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
+
649
657
df = df .infer_objects ()
650
658
651
659
# Adding a new key
@@ -1211,20 +1219,23 @@ def test_loc_reverse_assignment(self):
1211
1219
1212
1220
tm .assert_series_equal (result , expected )
1213
1221
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 ):
1216
1223
# GH#20388
1217
1224
1218
1225
col_data = [str (np .random .default_rng (2 ).random () * 1e-12 ) for _ in range (5 )]
1219
1226
result = DataFrame (col_data , columns = ["A" ])
1220
- expected = DataFrame (col_data , columns = ["A" ], dtype = object )
1227
+ expected = DataFrame (col_data , columns = ["A" ])
1221
1228
tm .assert_frame_equal (result , expected )
1222
1229
1223
1230
# assigning with loc/iloc attempts to set the values inplace, which
1224
1231
# 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 )
1228
1239
1229
1240
# assigning the entire column using __setitem__ swaps in the new array
1230
1241
# GH#???
@@ -1389,9 +1400,6 @@ def test_loc_setitem_categorical_values_partial_column_slice(self):
1389
1400
df .loc [1 :2 , "a" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
1390
1401
df .loc [2 :3 , "b" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
1391
1402
1392
- @pytest .mark .xfail (
1393
- using_string_dtype () and not HAS_PYARROW , reason = "TODO(infer_string)"
1394
- )
1395
1403
def test_loc_setitem_single_row_categorical (self , using_infer_string ):
1396
1404
# GH#25495
1397
1405
df = DataFrame ({"Alpha" : ["a" ], "Numeric" : [0 ]})
0 commit comments