@@ -563,7 +563,12 @@ def _maybe_downcast(
563563 return blocks
564564
565565 nbs = extend_blocks (
566- [blk .convert (using_cow = using_cow , copy = not using_cow ) for blk in blocks ]
566+ [
567+ blk .convert (
568+ using_cow = using_cow , copy = not using_cow , convert_string = False
569+ )
570+ for blk in blocks
571+ ]
567572 )
568573 if caller == "fillna" :
569574 if len (nbs ) != len (blocks ) or not all (
@@ -636,6 +641,7 @@ def convert(
636641 * ,
637642 copy : bool = True ,
638643 using_cow : bool = False ,
644+ convert_string : bool = True ,
639645 ) -> list [Block ]:
640646 """
641647 Attempt to coerce any object types to better types. Return a copy
@@ -648,7 +654,10 @@ def convert(
648654
649655 if self .ndim != 1 and self .shape [0 ] != 1 :
650656 blocks = self .split_and_operate (
651- Block .convert , copy = copy , using_cow = using_cow
657+ Block .convert ,
658+ copy = copy ,
659+ using_cow = using_cow ,
660+ convert_string = convert_string ,
652661 )
653662 if all (blk .dtype .kind == "O" for blk in blocks ):
654663 # Avoid fragmenting the block if convert is a no-op
@@ -666,6 +675,7 @@ def convert(
666675 res_values = lib .maybe_convert_objects (
667676 values , # type: ignore[arg-type]
668677 convert_non_numeric = True ,
678+ convert_string = convert_string ,
669679 )
670680 refs = None
671681 if (
@@ -851,6 +861,7 @@ def replace(
851861 mask : npt .NDArray [np .bool_ ] | None = None ,
852862 using_cow : bool = False ,
853863 already_warned = None ,
864+ convert_string = None ,
854865 ) -> list [Block ]:
855866 """
856867 replace the to_replace value with value, possible to create new
@@ -915,7 +926,11 @@ def replace(
915926 if get_option ("future.no_silent_downcasting" ) is True :
916927 blocks = [blk ]
917928 else :
918- blocks = blk .convert (copy = False , using_cow = using_cow )
929+ blocks = blk .convert (
930+ copy = False ,
931+ using_cow = using_cow ,
932+ convert_string = convert_string or self .dtype != _dtype_obj ,
933+ )
919934 if len (blocks ) > 1 or blocks [0 ].dtype != blk .dtype :
920935 warnings .warn (
921936 # GH#54710
@@ -944,6 +959,7 @@ def replace(
944959 inplace = True ,
945960 mask = mask ,
946961 using_cow = using_cow ,
962+ convert_string = convert_string ,
947963 )
948964
949965 else :
@@ -958,6 +974,7 @@ def replace(
958974 inplace = True ,
959975 mask = mask [i : i + 1 ],
960976 using_cow = using_cow ,
977+ convert_string = convert_string ,
961978 )
962979 )
963980 return blocks
@@ -970,6 +987,7 @@ def _replace_regex(
970987 inplace : bool = False ,
971988 mask = None ,
972989 using_cow : bool = False ,
990+ convert_string : bool = True ,
973991 already_warned = None ,
974992 ) -> list [Block ]:
975993 """
@@ -1029,7 +1047,9 @@ def _replace_regex(
10291047 )
10301048 already_warned .warned_already = True
10311049
1032- nbs = block .convert (copy = False , using_cow = using_cow )
1050+ nbs = block .convert (
1051+ copy = False , using_cow = using_cow , convert_string = convert_string
1052+ )
10331053 opt = get_option ("future.no_silent_downcasting" )
10341054 if (len (nbs ) > 1 or nbs [0 ].dtype != block .dtype ) and not opt :
10351055 warnings .warn (
@@ -1068,6 +1088,8 @@ def replace_list(
10681088 values ._replace (to_replace = src_list , value = dest_list , inplace = True )
10691089 return [blk ]
10701090
1091+ convert_string = self .dtype != _dtype_obj
1092+
10711093 # Exclude anything that we know we won't contain
10721094 pairs = [
10731095 (x , y )
@@ -1152,6 +1174,7 @@ def replace_list(
11521174 inplace = inplace ,
11531175 regex = regex ,
11541176 using_cow = using_cow ,
1177+ convert_string = convert_string ,
11551178 )
11561179
11571180 if using_cow and i != src_len :
@@ -1174,7 +1197,9 @@ def replace_list(
11741197 nbs = []
11751198 for res_blk in result :
11761199 converted = res_blk .convert (
1177- copy = True and not using_cow , using_cow = using_cow
1200+ copy = True and not using_cow ,
1201+ using_cow = using_cow ,
1202+ convert_string = convert_string ,
11781203 )
11791204 if len (converted ) > 1 or converted [0 ].dtype != res_blk .dtype :
11801205 warnings .warn (
@@ -1204,6 +1229,7 @@ def _replace_coerce(
12041229 inplace : bool = True ,
12051230 regex : bool = False ,
12061231 using_cow : bool = False ,
1232+ convert_string : bool = True ,
12071233 ) -> list [Block ]:
12081234 """
12091235 Replace value corresponding to the given boolean array with another
@@ -1233,6 +1259,7 @@ def _replace_coerce(
12331259 inplace = inplace ,
12341260 mask = mask ,
12351261 using_cow = using_cow ,
1262+ convert_string = convert_string ,
12361263 )
12371264 else :
12381265 if value is None :
@@ -1256,6 +1283,7 @@ def _replace_coerce(
12561283 inplace = inplace ,
12571284 mask = mask ,
12581285 using_cow = using_cow ,
1286+ convert_string = convert_string ,
12591287 )
12601288
12611289 # ---------------------------------------------------------------------
0 commit comments