Skip to content

Commit 860d5fd

Browse files
Backport PR #34733 on branch 1.0.x (BUG: Fixed Series.replace for EA with casting) (#34819)
Co-authored-by: Tom Augspurger <[email protected]>
1 parent e0b8fbc commit 860d5fd

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
@@ -779,7 +779,11 @@ def replace(
779779
if is_object_dtype(self):
780780
raise
781781

782-
assert not self._can_hold_element(value), value
782+
if not self.is_extension:
783+
# TODO: https://github.com/pandas-dev/pandas/issues/32586
784+
# Need an ExtensionArray._can_hold_element to indicate whether
785+
# a scalar value can be placed in the array.
786+
assert not self._can_hold_element(value), value
783787

784788
# try again with a compatible block
785789
block = self.astype(object)

pandas/tests/series/methods/test_replace.py

+5
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,8 @@ def test_replace_no_cast(self, ser, exp):
362362
expected = pd.Series(exp)
363363

364364
tm.assert_series_equal(result, expected)
365+
366+
def test_replace_extension_other(self):
367+
# https://github.com/pandas-dev/pandas/issues/34530
368+
ser = pd.Series(pd.array([1, 2, 3], dtype="Int64"))
369+
ser.replace("", "") # no exception

0 commit comments

Comments
 (0)