You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For all other dtypes (I think, just checked with the one below), if the value to replace with in replace() doesn't fit into the calling series, then we "upcast" to object dtype and then do the replacement anyway.
However, for the future string dtype, and then trying to replace a value with a non-string, we do not cast to object dtype currently, but raise instead:
>>>pd.options.future.infer_string=True>>>ser=pd.Series(["a", "b"])
>>>ser.replace("a", 1)
...
File~/scipy/repos/pandas/pandas/core/internals/blocks.py:713, inBlock.replace(self, to_replace, value, inplace, mask)
709elifself._can_hold_element(value):
710# TODO(CoW): Maybe split here as well into columns where mask has True711# and rest?712blk=self._maybe_copy(inplace)
-->713putmask_inplace(blk.values, mask, value)
714return [blk]
716elifself.ndim==1orself.shape[0] ==1:
...
File~/scipy/repos/pandas/pandas/core/arrays/string_.py:746, in__setitem__(self, key, value)
...
TypeError: Invalidvalue'1'fordtype'str'. Valueshouldbeastringormissingvalue, got'int'instead.
Making replace() strict (preserve dtype) in general is a much bigger topic, so I think for now we should just keep the current behaviour of upcasting to object dtype when needed.
The text was updated successfully, but these errors were encountered:
jorisvandenbossche
changed the title
BUG (string dtype): replace value in string column with non-string should cast to object dtype instead of raising an error
BUG (string dtype): replace() value in string column with non-string should cast to object dtype instead of raising an error
Nov 12, 2024
For all other dtypes (I think, just checked with the one below), if the value to replace with in
replace()
doesn't fit into the calling series, then we "upcast" to object dtype and then do the replacement anyway.Simple example with an integer series:
However, for the future string dtype, and then trying to replace a value with a non-string, we do not cast to object dtype currently, but raise instead:
Making
replace()
strict (preserve dtype) in general is a much bigger topic, so I think for now we should just keep the current behaviour of upcasting to object dtype when needed.The text was updated successfully, but these errors were encountered: