Skip to content

Commit e308f89

Browse files
hasan-yamansimonjayhawkins
authored andcommitted
BUG: Fix behavior of replace_list with mixed types. (pandas-dev#40555)
Co-authored-by: Simon Hawkins <[email protected]>
1 parent cad1639 commit e308f89

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

doc/source/whatsnew/v1.2.5.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Regression in :func:`concat` between two :class:`DataFrames` where one has an :class:`Index` that is all-None and the other is :class:`DatetimeIndex` incorrectly raising (:issue:`40841`)
1818
- Regression in :func:`read_csv` when using ``memory_map=True`` with an non-UTF8 encoding (:issue:`40986`)
19-
-
19+
- Regression in :meth:`DataFrame.replace` and :meth:`Series.replace` when the values to replace is a NumPy float array (:issue:`40371`)
2020

2121
.. ---------------------------------------------------------------------------
2222

pandas/core/internals/blocks.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
TYPE_CHECKING,
77
Any,
88
Callable,
9+
Iterable,
10+
Sequence,
911
cast,
1012
)
1113
import warnings
@@ -763,8 +765,8 @@ def _replace_regex(
763765
@final
764766
def _replace_list(
765767
self,
766-
src_list: list[Any],
767-
dest_list: list[Any],
768+
src_list: Iterable[Any],
769+
dest_list: Sequence[Any],
768770
inplace: bool = False,
769771
regex: bool = False,
770772
) -> list[Block]:
@@ -779,6 +781,14 @@ def _replace_list(
779781
# so un-tile here
780782
return self.replace(src_list, dest_list[0], inplace, regex)
781783

784+
# https://github.com/pandas-dev/pandas/issues/40371
785+
# the following pairs check code caused a regression so we catch that case here
786+
# until the issue is fixed properly in can_hold_element
787+
788+
# error: "Iterable[Any]" has no attribute "tolist"
789+
if hasattr(src_list, "tolist"):
790+
src_list = src_list.tolist() # type: ignore[attr-defined]
791+
782792
# Exclude anything that we know we won't contain
783793
pairs = [
784794
(x, y) for x, y in zip(src_list, dest_list) if self._can_hold_element(x)

pandas/tests/frame/methods/test_replace.py

+19
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,25 @@ def test_replace_bytes(self, frame_or_series):
14281428
obj = obj.replace({None: np.nan})
14291429
tm.assert_equal(obj, expected)
14301430

1431+
@pytest.mark.parametrize(
1432+
"data, to_replace, value, expected",
1433+
[
1434+
([1], [1.0], [0], [0]),
1435+
([1], [1], [0], [0]),
1436+
([1.0], [1.0], [0], [0.0]),
1437+
([1.0], [1], [0], [0.0]),
1438+
],
1439+
)
1440+
@pytest.mark.parametrize("box", [list, tuple, np.array])
1441+
def test_replace_list_with_mixed_type(
1442+
self, data, to_replace, value, expected, box, frame_or_series
1443+
):
1444+
# GH#40371
1445+
obj = frame_or_series(data)
1446+
expected = frame_or_series(expected)
1447+
result = obj.replace(box(to_replace), value)
1448+
tm.assert_equal(result, expected)
1449+
14311450

14321451
class TestDataFrameReplaceRegex:
14331452
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)