Skip to content

Commit 7bc9992

Browse files
simonjayhawkinshasan-yaman
and
hasan-yaman
authored
Backport PR #40555: BUG: Fix behavior of replace_list with mixed types. (#41761)
Co-authored-by: hasan-yaman <[email protected]>
1 parent e64410f commit 7bc9992

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
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

+9
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,15 @@ def _replace_list(
860860
"""
861861
See BlockManager._replace_list docstring.
862862
"""
863+
864+
# https://github.com/pandas-dev/pandas/issues/40371
865+
# the following pairs check code caused a regression so we catch that case here
866+
# until the issue is fixed properly in can_hold_element
867+
868+
# error: "Iterable[Any]" has no attribute "tolist"
869+
if hasattr(src_list, "tolist"):
870+
src_list = src_list.tolist() # type: ignore[attr-defined]
871+
863872
# Exclude anything that we know we won't contain
864873
pairs = [
865874
(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
@@ -1665,3 +1665,22 @@ def test_replace_bytes(self, frame_or_series):
16651665
expected = obj.copy()
16661666
obj = obj.replace({None: np.nan})
16671667
tm.assert_equal(obj, expected)
1668+
1669+
@pytest.mark.parametrize(
1670+
"data, to_replace, value, expected",
1671+
[
1672+
([1], [1.0], [0], [0]),
1673+
([1], [1], [0], [0]),
1674+
([1.0], [1.0], [0], [0.0]),
1675+
([1.0], [1], [0], [0.0]),
1676+
],
1677+
)
1678+
@pytest.mark.parametrize("box", [list, tuple, np.array])
1679+
def test_replace_list_with_mixed_type(
1680+
self, data, to_replace, value, expected, box, frame_or_series
1681+
):
1682+
# GH#40371
1683+
obj = frame_or_series(data)
1684+
expected = frame_or_series(expected)
1685+
result = obj.replace(box(to_replace), value)
1686+
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)