@@ -76,7 +76,7 @@ def test_astype_dict_like(self, dtype_class):
7676
7777 dt1 = dtype_class ({"abc" : str })
7878 result = ser .astype (dt1 )
79- expected = Series (["0" , "2" , "4" , "6" , "8" ], name = "abc" )
79+ expected = Series (["0" , "2" , "4" , "6" , "8" ], name = "abc" , dtype = object )
8080 tm .assert_series_equal (result , expected )
8181
8282 dt2 = dtype_class ({"abc" : "float64" })
@@ -170,10 +170,12 @@ def test_astype_empty_constructor_equality(self, dtype):
170170 Series ([string .digits * 10 , rand_str (63 ), rand_str (64 ), np .nan , 1.0 ]),
171171 ],
172172 )
173- def test_astype_str_map (self , dtype , series ):
173+ def test_astype_str_map (self , dtype , series , using_infer_string ):
174174 # see GH#4405
175175 result = series .astype (dtype )
176176 expected = series .map (str )
177+ if using_infer_string :
178+ expected = expected .astype (object )
177179 tm .assert_series_equal (result , expected )
178180
179181 def test_astype_float_to_period (self ):
@@ -283,13 +285,13 @@ def test_astype_str_cast_dt64(self):
283285 ts = Series ([Timestamp ("2010-01-04 00:00:00" )])
284286 res = ts .astype (str )
285287
286- expected = Series (["2010-01-04" ])
288+ expected = Series (["2010-01-04" ], dtype = object )
287289 tm .assert_series_equal (res , expected )
288290
289291 ts = Series ([Timestamp ("2010-01-04 00:00:00" , tz = "US/Eastern" )])
290292 res = ts .astype (str )
291293
292- expected = Series (["2010-01-04 00:00:00-05:00" ])
294+ expected = Series (["2010-01-04 00:00:00-05:00" ], dtype = object )
293295 tm .assert_series_equal (res , expected )
294296
295297 def test_astype_str_cast_td64 (self ):
@@ -298,7 +300,7 @@ def test_astype_str_cast_td64(self):
298300 td = Series ([Timedelta (1 , unit = "d" )])
299301 ser = td .astype (str )
300302
301- expected = Series (["1 days" ])
303+ expected = Series (["1 days" ], dtype = object )
302304 tm .assert_series_equal (ser , expected )
303305
304306 def test_dt64_series_astype_object (self ):
@@ -345,7 +347,7 @@ def test_astype_from_float_to_str(self, dtype):
345347 # https://github.com/pandas-dev/pandas/issues/36451
346348 ser = Series ([0.1 ], dtype = dtype )
347349 result = ser .astype (str )
348- expected = Series (["0.1" ])
350+ expected = Series (["0.1" ], dtype = object )
349351 tm .assert_series_equal (result , expected )
350352
351353 @pytest .mark .parametrize (
@@ -416,7 +418,7 @@ def test_astype_cast_object_int(self):
416418
417419 tm .assert_series_equal (result , Series (np .arange (1 , 5 )))
418420
419- def test_astype_unicode (self ):
421+ def test_astype_unicode (self , using_infer_string ):
420422 # see GH#7758: A bit of magic is required to set
421423 # default encoding to utf-8
422424 digits = string .digits
@@ -433,12 +435,14 @@ def test_astype_unicode(self):
433435 item = "野菜食べないとやばい"
434436 ser = Series ([item .encode ()])
435437 result = ser .astype (np .str_ )
436- expected = Series ([item ])
438+ expected = Series ([item ], dtype = object )
437439 tm .assert_series_equal (result , expected )
438440
439441 for ser in test_series :
440442 res = ser .astype (np .str_ )
441443 expec = ser .map (str )
444+ if using_infer_string :
445+ expec = expec .astype (object )
442446 tm .assert_series_equal (res , expec )
443447
444448 # Restore the former encoding
@@ -534,12 +538,12 @@ def test_astype_categorical_to_other(self):
534538 expected = ser
535539 tm .assert_series_equal (ser .astype ("category" ), expected )
536540 tm .assert_series_equal (ser .astype (CategoricalDtype ()), expected )
537- msg = r"Cannot cast object dtype to float64"
541+ msg = r"Cannot cast object|string dtype to float64"
538542 with pytest .raises (ValueError , match = msg ):
539543 ser .astype ("float64" )
540544
541545 cat = Series (Categorical (["a" , "b" , "b" , "a" , "a" , "c" , "c" , "c" ]))
542- exp = Series (["a" , "b" , "b" , "a" , "a" , "c" , "c" , "c" ])
546+ exp = Series (["a" , "b" , "b" , "a" , "a" , "c" , "c" , "c" ], dtype = object )
543547 tm .assert_series_equal (cat .astype ("str" ), exp )
544548 s2 = Series (Categorical (["1" , "2" , "3" , "4" ]))
545549 exp2 = Series ([1 , 2 , 3 , 4 ]).astype ("int" )
0 commit comments