Skip to content

Commit 18f9460

Browse files
BUG: Fixed Series.replace for EA with casting (#34733)
1 parent 13e96e8 commit 18f9460

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v1.0.5.rst

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Note this disables the ability to read Parquet files from directories on S3
2424
again (:issue:`26388`, :issue:`34632`), which was added in the 1.0.4 release,
2525
but is now targeted for pandas 1.1.0.
2626

27+
- Fixed regression in :meth:`~DataFrame.replace` raising an ``AssertionError`` when replacing values in an extension dtype with values of a different dtype (:issue:`34530`)
28+
2729
.. _whatsnew_105.bug_fixes:
2830

2931
Bug fixes

pandas/core/internals/blocks.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,11 @@ def replace(
745745
if is_object_dtype(self):
746746
raise
747747

748-
assert not self._can_hold_element(value), value
748+
if not self.is_extension:
749+
# TODO: https://github.com/pandas-dev/pandas/issues/32586
750+
# Need an ExtensionArray._can_hold_element to indicate whether
751+
# a scalar value can be placed in the array.
752+
assert not self._can_hold_element(value), value
749753

750754
# try again with a compatible block
751755
block = self.astype(object)

pandas/tests/series/methods/test_replace.py

+5
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,8 @@ def test_replace_only_one_dictlike_arg(self):
402402
msg = "Series.replace cannot use dict-value and non-None to_replace"
403403
with pytest.raises(ValueError, match=msg):
404404
ser.replace(to_replace, value)
405+
406+
def test_replace_extension_other(self):
407+
# https://github.com/pandas-dev/pandas/issues/34530
408+
ser = pd.Series(pd.array([1, 2, 3], dtype="Int64"))
409+
ser.replace("", "") # no exception

0 commit comments

Comments
 (0)