99import numpy as np
1010import pytest
1111
12- from pandas ._config import using_string_dtype
13-
14- from pandas .compat import (
15- HAS_PYARROW ,
16- WASM ,
17- )
12+ from pandas .compat import WASM
1813from pandas .compat .numpy import np_version_gte1p24
1914from pandas .errors import IndexingError
2015
3227 NaT ,
3328 Period ,
3429 Series ,
30+ StringDtype ,
3531 Timedelta ,
3632 Timestamp ,
3733 array ,
@@ -535,14 +531,16 @@ def test_append_timedelta_does_not_cast(self, td, using_infer_string, request):
535531 tm .assert_series_equal (ser , expected )
536532 assert isinstance (ser ["td" ], Timedelta )
537533
538- @pytest .mark .xfail (using_string_dtype (), reason = "TODO(infer_string)" )
539534 def test_setitem_with_expansion_type_promotion (self ):
540535 # GH#12599
541536 ser = Series (dtype = object )
542537 ser ["a" ] = Timestamp ("2016-01-01" )
543538 ser ["b" ] = 3.0
544539 ser ["c" ] = "foo"
545- expected = Series ([Timestamp ("2016-01-01" ), 3.0 , "foo" ], index = ["a" , "b" , "c" ])
540+ expected = Series (
541+ [Timestamp ("2016-01-01" ), 3.0 , "foo" ],
542+ index = Index (["a" , "b" , "c" ], dtype = object ),
543+ )
546544 tm .assert_series_equal (ser , expected )
547545
548546 def test_setitem_not_contained (self , string_series ):
@@ -826,11 +824,6 @@ def test_mask_key(self, obj, key, expected, raises, val, indexer_sli):
826824 else :
827825 indexer_sli (obj )[mask ] = val
828826
829- @pytest .mark .xfail (
830- using_string_dtype () and not HAS_PYARROW ,
831- reason = "TODO(infer_string)" ,
832- strict = False ,
833- )
834827 def test_series_where (self , obj , key , expected , raises , val , is_inplace ):
835828 mask = np .zeros (obj .shape , dtype = bool )
836829 mask [key ] = True
@@ -846,6 +839,11 @@ def test_series_where(self, obj, key, expected, raises, val, is_inplace):
846839 obj = obj .copy ()
847840 arr = obj ._values
848841
842+ if raises and obj .dtype == "string" :
843+ with pytest .raises (TypeError , match = "Invalid value" ):
844+ obj .where (~ mask , val )
845+ return
846+
849847 res = obj .where (~ mask , val )
850848
851849 if val is NA and res .dtype == object :
@@ -858,25 +856,23 @@ def test_series_where(self, obj, key, expected, raises, val, is_inplace):
858856
859857 self ._check_inplace (is_inplace , orig , arr , obj )
860858
861- @pytest .mark .xfail (using_string_dtype (), reason = "TODO(infer_string)" , strict = False )
862- def test_index_where (self , obj , key , expected , raises , val , using_infer_string ):
859+ def test_index_where (self , obj , key , expected , raises , val ):
863860 mask = np .zeros (obj .shape , dtype = bool )
864861 mask [key ] = True
865862
866- if using_infer_string and obj .dtype == object :
863+ if raises and obj .dtype == "string" :
867864 with pytest .raises (TypeError , match = "Invalid value" ):
868865 Index (obj ).where (~ mask , val )
869866 else :
870867 res = Index (obj ).where (~ mask , val )
871868 expected_idx = Index (expected , dtype = expected .dtype )
872869 tm .assert_index_equal (res , expected_idx )
873870
874- @pytest .mark .xfail (using_string_dtype (), reason = "TODO(infer_string)" , strict = False )
875- def test_index_putmask (self , obj , key , expected , raises , val , using_infer_string ):
871+ def test_index_putmask (self , obj , key , expected , raises , val ):
876872 mask = np .zeros (obj .shape , dtype = bool )
877873 mask [key ] = True
878874
879- if using_infer_string and obj .dtype == object :
875+ if raises and obj .dtype == "string" :
880876 with pytest .raises (TypeError , match = "Invalid value" ):
881877 Index (obj ).putmask (mask , val )
882878 else :
@@ -1372,6 +1368,19 @@ def raises(self):
13721368 return False
13731369
13741370
1371+ @pytest .mark .parametrize (
1372+ "val,exp_dtype,raises" ,
1373+ [
1374+ (1 , object , True ),
1375+ ("e" , StringDtype (na_value = np .nan ), False ),
1376+ ],
1377+ )
1378+ class TestCoercionString (CoercionTest ):
1379+ @pytest .fixture
1380+ def obj (self ):
1381+ return Series (["a" , "b" , "c" , "d" ], dtype = StringDtype (na_value = np .nan ))
1382+
1383+
13751384@pytest .mark .parametrize (
13761385 "val,exp_dtype,raises" ,
13771386 [
0 commit comments