Skip to content

Commit d95620b

Browse files
String dtype (2.3.x): avoid downcasting object to string in fillna/where/interpolate
1 parent ce56f2e commit d95620b

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

pandas/_libs/lib.pyx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2498,6 +2498,7 @@ def maybe_convert_objects(ndarray[object] objects,
24982498
bint convert_numeric=True, # NB: different default!
24992499
bint convert_to_nullable_dtype=False,
25002500
bint convert_non_numeric=False,
2501+
bint convert_string=True,
25012502
object dtype_if_all_nat=None) -> "ArrayLike":
25022503
"""
25032504
Type inference function-- convert object array to proper dtype
@@ -2741,7 +2742,11 @@ def maybe_convert_objects(ndarray[object] objects,
27412742
seen.object_ = True
27422743

27432744
elif seen.str_:
2744-
if using_string_dtype() and is_string_array(objects, skipna=True):
2745+
if (
2746+
convert_string
2747+
and using_string_dtype()
2748+
and is_string_array(objects, skipna=True)
2749+
):
27452750
from pandas.core.arrays.string_ import StringDtype
27462751

27472752
dtype = StringDtype(na_value=np.nan)

pandas/core/internals/blocks.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,12 @@ def _maybe_downcast(
552552
return blocks
553553

554554
nbs = extend_blocks(
555-
[blk.convert(using_cow=using_cow, copy=not using_cow) for blk in blocks]
555+
[
556+
blk.convert(
557+
using_cow=using_cow, copy=not using_cow, convert_string=False
558+
)
559+
for blk in blocks
560+
]
556561
)
557562
if caller == "fillna":
558563
if len(nbs) != len(blocks) or not all(
@@ -625,6 +630,7 @@ def convert(
625630
*,
626631
copy: bool = True,
627632
using_cow: bool = False,
633+
convert_string: bool = True,
628634
) -> list[Block]:
629635
"""
630636
Attempt to coerce any object types to better types. Return a copy
@@ -655,6 +661,7 @@ def convert(
655661
res_values = lib.maybe_convert_objects(
656662
values, # type: ignore[arg-type]
657663
convert_non_numeric=True,
664+
convert_string=convert_string,
658665
)
659666
refs = None
660667
if (
@@ -904,7 +911,9 @@ def replace(
904911
if get_option("future.no_silent_downcasting") is True:
905912
blocks = [blk]
906913
else:
907-
blocks = blk.convert(copy=False, using_cow=using_cow)
914+
blocks = blk.convert(
915+
copy=False, using_cow=using_cow, convert_string=False
916+
)
908917
if len(blocks) > 1 or blocks[0].dtype != blk.dtype:
909918
warnings.warn(
910919
# GH#54710
@@ -1007,7 +1016,7 @@ def _replace_regex(
10071016
)
10081017
already_warned.warned_already = True
10091018

1010-
nbs = block.convert(copy=False, using_cow=using_cow)
1019+
nbs = block.convert(copy=False, using_cow=using_cow, convert_string=False)
10111020
opt = get_option("future.no_silent_downcasting")
10121021
if (len(nbs) > 1 or nbs[0].dtype != block.dtype) and not opt:
10131022
warnings.warn(
@@ -1150,7 +1159,9 @@ def replace_list(
11501159
nbs = []
11511160
for res_blk in result:
11521161
converted = res_blk.convert(
1153-
copy=True and not using_cow, using_cow=using_cow
1162+
copy=True and not using_cow,
1163+
using_cow=using_cow,
1164+
convert_string=False,
11541165
)
11551166
if len(converted) > 1 or converted[0].dtype != res_blk.dtype:
11561167
warnings.warn(

0 commit comments

Comments
 (0)