Skip to content

Commit ba94748

Browse files
committed
BUG: Fixed Series.replace for EA with casting
Closes pandas-dev#34530
1 parent 08febd2 commit ba94748

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,7 @@ Reshaping
10031003
- Bug where :meth:`Index.astype` would lose the name attribute when converting from ``Float64Index`` to ``Int64Index``, or when casting to an ``ExtensionArray`` dtype (:issue:`32013`)
10041004
- :meth:`Series.append` will now raise a ``TypeError`` when passed a DataFrame or a sequence containing Dataframe (:issue:`31413`)
10051005
- :meth:`DataFrame.replace` and :meth:`Series.replace` will raise a ``TypeError`` if ``to_replace`` is not an expected type. Previously the ``replace`` would fail silently (:issue:`18634`)
1006+
- Bug in :meth:`~DataFrame.replace` raising an ``AssertionError`` when replacing values in an extension dtype with values of a different dtype (:issue:`34530`)
10061007
- Bug on inplace operation of a Series that was adding a column to the DataFrame from where it was originally dropped from (using inplace=True) (:issue:`30484`)
10071008
- Bug in :meth:`DataFrame.apply` where callback was called with :class:`Series` parameter even though ``raw=True`` requested. (:issue:`32423`)
10081009
- Bug in :meth:`DataFrame.pivot_table` losing timezone information when creating a :class:`MultiIndex` level from a column with timezone-aware dtype (:issue:`32558`)

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/extension/base/methods.py

+6
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,9 @@ def test_equals(self, data, na_value, as_series, box):
452452
# other types
453453
assert data.equals(None) is False
454454
assert data[[0]].equals(data[0]) is False
455+
456+
def test_replace_nonsense(self, data):
457+
# https://github.com/pandas-dev/pandas/issues/34530
458+
ser = pd.Series(data)
459+
ser.replace("", "") # no exception
460+
ser.to_frame().replace("", "") # no exception

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)