@@ -563,7 +563,12 @@ def _maybe_downcast(
563
563
return blocks
564
564
565
565
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
+ ]
567
572
)
568
573
if caller == "fillna" :
569
574
if len (nbs ) != len (blocks ) or not all (
@@ -636,6 +641,7 @@ def convert(
636
641
* ,
637
642
copy : bool = True ,
638
643
using_cow : bool = False ,
644
+ convert_string : bool = True ,
639
645
) -> list [Block ]:
640
646
"""
641
647
Attempt to coerce any object types to better types. Return a copy
@@ -648,7 +654,10 @@ def convert(
648
654
649
655
if self .ndim != 1 and self .shape [0 ] != 1 :
650
656
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 ,
652
661
)
653
662
if all (blk .dtype .kind == "O" for blk in blocks ):
654
663
# Avoid fragmenting the block if convert is a no-op
@@ -666,6 +675,7 @@ def convert(
666
675
res_values = lib .maybe_convert_objects (
667
676
values , # type: ignore[arg-type]
668
677
convert_non_numeric = True ,
678
+ convert_string = convert_string ,
669
679
)
670
680
refs = None
671
681
if (
@@ -851,6 +861,7 @@ def replace(
851
861
mask : npt .NDArray [np .bool_ ] | None = None ,
852
862
using_cow : bool = False ,
853
863
already_warned = None ,
864
+ convert_string = None ,
854
865
) -> list [Block ]:
855
866
"""
856
867
replace the to_replace value with value, possible to create new
@@ -915,7 +926,11 @@ def replace(
915
926
if get_option ("future.no_silent_downcasting" ) is True :
916
927
blocks = [blk ]
917
928
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
+ )
919
934
if len (blocks ) > 1 or blocks [0 ].dtype != blk .dtype :
920
935
warnings .warn (
921
936
# GH#54710
@@ -944,6 +959,7 @@ def replace(
944
959
inplace = True ,
945
960
mask = mask ,
946
961
using_cow = using_cow ,
962
+ convert_string = convert_string ,
947
963
)
948
964
949
965
else :
@@ -958,6 +974,7 @@ def replace(
958
974
inplace = True ,
959
975
mask = mask [i : i + 1 ],
960
976
using_cow = using_cow ,
977
+ convert_string = convert_string ,
961
978
)
962
979
)
963
980
return blocks
@@ -970,6 +987,7 @@ def _replace_regex(
970
987
inplace : bool = False ,
971
988
mask = None ,
972
989
using_cow : bool = False ,
990
+ convert_string : bool = True ,
973
991
already_warned = None ,
974
992
) -> list [Block ]:
975
993
"""
@@ -1029,7 +1047,9 @@ def _replace_regex(
1029
1047
)
1030
1048
already_warned .warned_already = True
1031
1049
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
+ )
1033
1053
opt = get_option ("future.no_silent_downcasting" )
1034
1054
if (len (nbs ) > 1 or nbs [0 ].dtype != block .dtype ) and not opt :
1035
1055
warnings .warn (
@@ -1068,6 +1088,8 @@ def replace_list(
1068
1088
values ._replace (to_replace = src_list , value = dest_list , inplace = True )
1069
1089
return [blk ]
1070
1090
1091
+ convert_string = self .dtype != _dtype_obj
1092
+
1071
1093
# Exclude anything that we know we won't contain
1072
1094
pairs = [
1073
1095
(x , y )
@@ -1152,6 +1174,7 @@ def replace_list(
1152
1174
inplace = inplace ,
1153
1175
regex = regex ,
1154
1176
using_cow = using_cow ,
1177
+ convert_string = convert_string ,
1155
1178
)
1156
1179
1157
1180
if using_cow and i != src_len :
@@ -1174,7 +1197,9 @@ def replace_list(
1174
1197
nbs = []
1175
1198
for res_blk in result :
1176
1199
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 ,
1178
1203
)
1179
1204
if len (converted ) > 1 or converted [0 ].dtype != res_blk .dtype :
1180
1205
warnings .warn (
@@ -1204,6 +1229,7 @@ def _replace_coerce(
1204
1229
inplace : bool = True ,
1205
1230
regex : bool = False ,
1206
1231
using_cow : bool = False ,
1232
+ convert_string : bool = True ,
1207
1233
) -> list [Block ]:
1208
1234
"""
1209
1235
Replace value corresponding to the given boolean array with another
@@ -1233,6 +1259,7 @@ def _replace_coerce(
1233
1259
inplace = inplace ,
1234
1260
mask = mask ,
1235
1261
using_cow = using_cow ,
1262
+ convert_string = convert_string ,
1236
1263
)
1237
1264
else :
1238
1265
if value is None :
@@ -1256,6 +1283,7 @@ def _replace_coerce(
1256
1283
inplace = inplace ,
1257
1284
mask = mask ,
1258
1285
using_cow = using_cow ,
1286
+ convert_string = convert_string ,
1259
1287
)
1260
1288
1261
1289
# ---------------------------------------------------------------------
0 commit comments