Skip to content

Commit bbbe25a

Browse files
fix inplace modification for 2.3.x branch with python storage
1 parent 1f218b6 commit bbbe25a

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

pandas/core/internals/blocks.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
PeriodArray,
117117
TimedeltaArray,
118118
)
119+
from pandas.core.arrays.string_ import StringDtype
119120
from pandas.core.base import PandasObject
120121
import pandas.core.common as com
121122
from pandas.core.computation import expressions
@@ -477,7 +478,9 @@ def split_and_operate(self, func, *args, **kwargs) -> list[Block]:
477478
# Up/Down-casting
478479

479480
@final
480-
def coerce_to_target_dtype(self, other, warn_on_upcast: bool = False) -> Block:
481+
def coerce_to_target_dtype(
482+
self, other, warn_on_upcast: bool = False, using_cow: bool = False
483+
) -> Block:
481484
"""
482485
coerce the current block to a dtype compat for other
483486
we will return a block, possibly object, and not raise
@@ -529,7 +532,14 @@ def coerce_to_target_dtype(self, other, warn_on_upcast: bool = False) -> Block:
529532
f"{self.values.dtype}. Please report a bug at "
530533
"https://github.com/pandas-dev/pandas/issues."
531534
)
532-
return self.astype(new_dtype, copy=False)
535+
copy = False
536+
if (
537+
not using_cow
538+
and isinstance(self.dtype, StringDtype)
539+
and self.dtype.storage == "python"
540+
):
541+
copy = True
542+
return self.astype(new_dtype, copy=copy, using_cow=using_cow)
533543

534544
@final
535545
def _maybe_downcast(
@@ -927,12 +937,13 @@ def replace(
927937
if value is None or value is NA:
928938
blk = self.astype(np.dtype(object))
929939
else:
930-
blk = self.coerce_to_target_dtype(value)
940+
blk = self.coerce_to_target_dtype(value, using_cow=using_cow)
931941
return blk.replace(
932942
to_replace=to_replace,
933943
value=value,
934944
inplace=True,
935945
mask=mask,
946+
using_cow=using_cow,
936947
)
937948

938949
else:

0 commit comments

Comments
 (0)