From 47c83f9213816804d0c401bf4b6e7b42c3a9048b Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Fri, 19 Jun 2020 21:09:12 -0500 Subject: [PATCH 1/7] Tests --- pandas/tests/frame/methods/test_replace.py | 6 ++++++ pandas/tests/series/methods/test_replace.py | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index 3bcc26e85e347..589ecbcf1ebba 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1403,3 +1403,9 @@ def test_replace_with_duplicate_columns(self, replacement): result["B"] = result["B"].replace(7, replacement) tm.assert_frame_equal(result, expected) + + def test_replace_equal_value_different_dtype(self): + df = pd.DataFrame([1, 2, 3], dtype=int) + result = df.replace(1.0, 0) + expected = pd.DataFrame([0, 2, 3], dtype=int) + tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/series/methods/test_replace.py b/pandas/tests/series/methods/test_replace.py index 8f57cf3191d5d..15085ca43b9c8 100644 --- a/pandas/tests/series/methods/test_replace.py +++ b/pandas/tests/series/methods/test_replace.py @@ -407,3 +407,9 @@ def test_replace_extension_other(self): # https://github.com/pandas-dev/pandas/issues/34530 ser = pd.Series(pd.array([1, 2, 3], dtype="Int64")) ser.replace("", "") # no exception + + def test_replace_equal_value_different_dtype(self): + ser = pd.Series([1, 2, 3], dtype=int) + result = ser.replace(1.0, 0) + expected = pd.Series([0, 2, 3], dtype=int) + tm.assert_series_equal(result, expected) From f45017f401d237c95dc39c466a7cfef0decce900 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Fri, 19 Jun 2020 21:09:34 -0500 Subject: [PATCH 2/7] Replace object block --- pandas/core/internals/blocks.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 38c495e1dd0f3..d33425368c786 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -688,9 +688,13 @@ def replace( # retry if not self._can_hold_element(to_replace): if not isinstance(to_replace, list): - if inplace: - return [self] - return [self.copy()] + return self.astype(object).replace( + to_replace=to_replace, + value=value, + inplace=inplace, + regex=regex, + convert=convert, + ) to_replace = [x for x in to_replace if self._can_hold_element(x)] if not len(to_replace): From 268056c3ddd6fbbea1850e357897025a83cad767 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Fri, 19 Jun 2020 21:48:43 -0500 Subject: [PATCH 3/7] Note --- doc/source/whatsnew/v1.1.0.rst | 1 + pandas/tests/frame/methods/test_replace.py | 1 + pandas/tests/series/methods/test_replace.py | 1 + 3 files changed, 3 insertions(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index f6ad3a800283d..f9d53e92a2ddb 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -1059,6 +1059,7 @@ Reshaping - Fixed bug in :func:`melt` where melting MultiIndex columns with ``col_level`` > 0 would raise a ``KeyError`` on ``id_vars`` (:issue:`34129`) - Bug in :meth:`Series.where` with an empty Series and empty ``cond`` having non-bool dtype (:issue:`34592`) - Fixed regression where :meth:`DataFrame.apply` would raise ``ValueError`` for elements whth ``S`` dtype (:issue:`34529`) +- Bug where :meth:`DataFrame.replace` and :meth:`Series.replace` would fail when replacement value was of a different dtype but equal to values in the original object (:issue:`34871`) Sparse ^^^^^^ diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index 589ecbcf1ebba..90fc344e7353d 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1405,6 +1405,7 @@ def test_replace_with_duplicate_columns(self, replacement): tm.assert_frame_equal(result, expected) def test_replace_equal_value_different_dtype(self): + # https://github.com/pandas-dev/pandas/issues/34871 df = pd.DataFrame([1, 2, 3], dtype=int) result = df.replace(1.0, 0) expected = pd.DataFrame([0, 2, 3], dtype=int) diff --git a/pandas/tests/series/methods/test_replace.py b/pandas/tests/series/methods/test_replace.py index 15085ca43b9c8..240aa5f00d4b1 100644 --- a/pandas/tests/series/methods/test_replace.py +++ b/pandas/tests/series/methods/test_replace.py @@ -409,6 +409,7 @@ def test_replace_extension_other(self): ser.replace("", "") # no exception def test_replace_equal_value_different_dtype(self): + # https://github.com/pandas-dev/pandas/issues/34871 ser = pd.Series([1, 2, 3], dtype=int) result = ser.replace(1.0, 0) expected = pd.Series([0, 2, 3], dtype=int) From 60ef5fcef35862f217c54801496a2e994dd35edd Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 21 Jun 2020 17:52:09 -0500 Subject: [PATCH 4/7] Types --- pandas/tests/frame/methods/test_replace.py | 4 ++-- pandas/tests/series/methods/test_replace.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index 90fc344e7353d..4d65ec8904b6f 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1406,7 +1406,7 @@ def test_replace_with_duplicate_columns(self, replacement): def test_replace_equal_value_different_dtype(self): # https://github.com/pandas-dev/pandas/issues/34871 - df = pd.DataFrame([1, 2, 3], dtype=int) + df = pd.DataFrame([1, 2, 3]) result = df.replace(1.0, 0) - expected = pd.DataFrame([0, 2, 3], dtype=int) + expected = pd.DataFrame([0, 2, 3]) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/series/methods/test_replace.py b/pandas/tests/series/methods/test_replace.py index 240aa5f00d4b1..c87370ccb8716 100644 --- a/pandas/tests/series/methods/test_replace.py +++ b/pandas/tests/series/methods/test_replace.py @@ -410,7 +410,7 @@ def test_replace_extension_other(self): def test_replace_equal_value_different_dtype(self): # https://github.com/pandas-dev/pandas/issues/34871 - ser = pd.Series([1, 2, 3], dtype=int) + ser = pd.Series([1, 2, 3]) result = ser.replace(1.0, 0) - expected = pd.Series([0, 2, 3], dtype=int) + expected = pd.Series([0, 2, 3]) tm.assert_series_equal(result, expected) From d87954e3c059198ae8c71e44b2ad0676edbfbd65 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 1 Aug 2020 16:59:45 -0500 Subject: [PATCH 5/7] Remove note --- doc/source/whatsnew/v1.1.0.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 3609f0fc749fe..43d1244c15d8a 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -1166,7 +1166,6 @@ Reshaping - Fixed bug in :func:`melt` where melting MultiIndex columns with ``col_level`` > 0 would raise a ``KeyError`` on ``id_vars`` (:issue:`34129`) - Bug in :meth:`Series.where` with an empty Series and empty ``cond`` having non-bool dtype (:issue:`34592`) - Fixed regression where :meth:`DataFrame.apply` would raise ``ValueError`` for elements whth ``S`` dtype (:issue:`34529`) -- Bug where :meth:`DataFrame.replace` and :meth:`Series.replace` would fail when replacement value was of a different dtype but equal to values in the original object (:issue:`34871`) Sparse ^^^^^^ From 34302019a9d1c0b2560073288716bbb105545c61 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 1 Aug 2020 17:02:14 -0500 Subject: [PATCH 6/7] Add note back --- doc/source/whatsnew/v1.1.1.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/source/whatsnew/v1.1.1.rst b/doc/source/whatsnew/v1.1.1.rst index 443589308ad4c..8f81237b94af2 100644 --- a/doc/source/whatsnew/v1.1.1.rst +++ b/doc/source/whatsnew/v1.1.1.rst @@ -44,6 +44,10 @@ Bug fixes - +**Reshaping** + +- Bug where :meth:`DataFrame.replace` and :meth:`Series.replace` would fail when replacement value was of a different dtype but equal to values in the original object (:issue:`34871`). + .. --------------------------------------------------------------------------- .. _whatsnew_111.contributors: From 464c81d1d0fddfffc607c4703766a41a3e9d9c41 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Fri, 18 Sep 2020 21:40:58 -0500 Subject: [PATCH 7/7] Move --- doc/source/whatsnew/v1.1.1.rst | 4 ---- doc/source/whatsnew/v1.2.0.rst | 1 + 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.1.1.rst b/doc/source/whatsnew/v1.1.1.rst index 27989e8dbe71a..77ea67f76f655 100644 --- a/doc/source/whatsnew/v1.1.1.rst +++ b/doc/source/whatsnew/v1.1.1.rst @@ -46,10 +46,6 @@ Bug fixes - Bug in ``.groupby(..).rolling(..)`` where passing ``closed`` with column selection would raise a ``ValueError`` (:issue:`35549`) - Bug in :class:`DataFrame` constructor failing to raise ``ValueError`` in some cases when ``data`` and ``index`` have mismatched lengths (:issue:`33437`) -**Reshaping** - -- Bug where :meth:`DataFrame.replace` and :meth:`Series.replace` would fail when replacement value was of a different dtype but equal to values in the original object (:issue:`34871`). - .. --------------------------------------------------------------------------- .. _whatsnew_111.contributors: diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 6923b42d3340b..bfdbc15673efb 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -338,6 +338,7 @@ Groupby/resample/rolling - Bug in :meth:`DataFrameGroupby.tshift` failing to raise ``ValueError`` when a frequency cannot be inferred for the index of a group (:issue:`35937`) - Bug in :meth:`DataFrame.groupby` does not always maintain column index name for ``any``, ``all``, ``bfill``, ``ffill``, ``shift`` (:issue:`29764`) - Bug in :meth:`DataFrameGroupBy.apply` raising error with ``np.nan`` group(s) when ``dropna=False`` (:issue:`35889`) +- Bug where :meth:`DataFrame.replace` and :meth:`Series.replace` would fail when replacement value was of a different dtype but equal to values in the original object (:issue:`34871`). - Reshaping