From e18a8576248557f6407e0351b6f0a8185e6d1616 Mon Sep 17 00:00:00 2001 From: fbourgey Date: Mon, 19 Aug 2024 14:48:40 -0400 Subject: [PATCH 1/8] changed AttributeError to ValueError --- pandas/core/generic.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 0f0078fc3398b..7910cbee5d28a 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7486,9 +7486,13 @@ def replace( if inplace: return None return self.copy(deep=False) - if is_dict_like(to_replace): if is_dict_like(value): # {'A' : NA} -> {'A' : 0} + is_series = isinstance(self, ABCSeries) + if is_series: + raise ValueError( + "Series.replace cannot use dict-like to_replace dict-like." + ) # Note: Checking below for `in foo.keys()` instead of # `in foo` is needed for when we have a Series and not dict mapping = { From 1868f074d08329aab5900131506e008058121a78 Mon Sep 17 00:00:00 2001 From: fbourgey Date: Mon, 19 Aug 2024 15:02:29 -0400 Subject: [PATCH 2/8] added test for replace method from dict_like to dict_like for a pd.Series --- pandas/tests/series/methods/test_replace.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandas/tests/series/methods/test_replace.py b/pandas/tests/series/methods/test_replace.py index de0855bf7192e..d87193c2f0937 100644 --- a/pandas/tests/series/methods/test_replace.py +++ b/pandas/tests/series/methods/test_replace.py @@ -496,6 +496,15 @@ def test_replace_only_one_dictlike_arg(self, fixed_now_ts): with pytest.raises(ValueError, match=msg): ser.replace(to_replace, value) + def test_replace_dict_like_with_dict_like(self): + # GH 59452 + s = pd.Series([1, 2, 3, 4, 5]) + to_replace = pd.Series([1]) + value = pd.Series([75]) + msg = "Series.replace cannot use dict-like to_replace dict-like." + with pytest.raises(ValueError, match=msg): + s.replace(to_replace, value) + def test_replace_extension_other(self, frame_or_series): # https://github.com/pandas-dev/pandas/issues/34530 obj = frame_or_series(pd.array([1, 2, 3], dtype="Int64")) From 7b0c934451051555c884946c35f11562a6fca44a Mon Sep 17 00:00:00 2001 From: fbourgey Date: Mon, 19 Aug 2024 15:17:21 -0400 Subject: [PATCH 3/8] added entry in whatsnew.rst --- doc/source/whatsnew/v3.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index f25edd39cf7da..9287a84788a72 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -484,6 +484,7 @@ Other Removals - Removed the ``method`` keyword in ``ExtensionArray.fillna``, implement ``ExtensionArray._pad_or_backfill`` instead (:issue:`53621`) - Removed the attribute ``dtypes`` from :class:`.DataFrameGroupBy` (:issue:`51997`) - Enforced deprecation of ``argmin``, ``argmax``, ``idxmin``, and ``idxmax`` returning a result when ``skipna=False`` and an NA value is encountered or all values are NA values; these operations will now raise in such cases (:issue:`33941`, :issue:`51276`) +- Replaced ``AttributeError`` with ``ValueError`` when using :meth:`Series.replace` with ``dict_like`` and ``dict_like`` (:issue:`59452`) .. --------------------------------------------------------------------------- .. _whatsnew_300.performance: From 9ebd7405ff22d151c1458c9e20d1cc40b6b2b527 Mon Sep 17 00:00:00 2001 From: Florian Bourgey Date: Mon, 19 Aug 2024 17:14:01 -0400 Subject: [PATCH 4/8] Update doc/source/whatsnew/v3.0.0.rst removed rst entry Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v3.0.0.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 9287a84788a72..f25edd39cf7da 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -484,7 +484,6 @@ Other Removals - Removed the ``method`` keyword in ``ExtensionArray.fillna``, implement ``ExtensionArray._pad_or_backfill`` instead (:issue:`53621`) - Removed the attribute ``dtypes`` from :class:`.DataFrameGroupBy` (:issue:`51997`) - Enforced deprecation of ``argmin``, ``argmax``, ``idxmin``, and ``idxmax`` returning a result when ``skipna=False`` and an NA value is encountered or all values are NA values; these operations will now raise in such cases (:issue:`33941`, :issue:`51276`) -- Replaced ``AttributeError`` with ``ValueError`` when using :meth:`Series.replace` with ``dict_like`` and ``dict_like`` (:issue:`59452`) .. --------------------------------------------------------------------------- .. _whatsnew_300.performance: From a06f9c745be6e9c2946c715a609bbdb83fdd601b Mon Sep 17 00:00:00 2001 From: Florian Bourgey Date: Mon, 19 Aug 2024 17:14:09 -0400 Subject: [PATCH 5/8] Update pandas/core/generic.py Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- pandas/core/generic.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 7910cbee5d28a..f680037d74576 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7488,8 +7488,7 @@ def replace( return self.copy(deep=False) if is_dict_like(to_replace): if is_dict_like(value): # {'A' : NA} -> {'A' : 0} - is_series = isinstance(self, ABCSeries) - if is_series: + if isinstance(self, ABCSeries): raise ValueError( "Series.replace cannot use dict-like to_replace dict-like." ) From be7e98b98ba12c605133a1330b1c8ce135623e68 Mon Sep 17 00:00:00 2001 From: Florian Bourgey Date: Mon, 19 Aug 2024 17:14:21 -0400 Subject: [PATCH 6/8] Update pandas/core/generic.py Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index f680037d74576..e1adbd48edc81 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7490,7 +7490,7 @@ def replace( if is_dict_like(value): # {'A' : NA} -> {'A' : 0} if isinstance(self, ABCSeries): raise ValueError( - "Series.replace cannot use dict-like to_replace dict-like." + "to_replace and value cannot be dict-like for Series.replace" ) # Note: Checking below for `in foo.keys()` instead of # `in foo` is needed for when we have a Series and not dict From 02a56a6833cea03ac37b856a7a0d6a0e7a9bedc9 Mon Sep 17 00:00:00 2001 From: fbourgey Date: Tue, 20 Aug 2024 08:23:25 -0400 Subject: [PATCH 7/8] updated msg test function --- pandas/tests/series/methods/test_replace.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/methods/test_replace.py b/pandas/tests/series/methods/test_replace.py index d87193c2f0937..611fcc114db6c 100644 --- a/pandas/tests/series/methods/test_replace.py +++ b/pandas/tests/series/methods/test_replace.py @@ -501,7 +501,7 @@ def test_replace_dict_like_with_dict_like(self): s = pd.Series([1, 2, 3, 4, 5]) to_replace = pd.Series([1]) value = pd.Series([75]) - msg = "Series.replace cannot use dict-like to_replace dict-like." + msg = "to_replace and value cannot be dict-like for Series.replace" with pytest.raises(ValueError, match=msg): s.replace(to_replace, value) From 1084a601e4ae2403f74c1ed5bdeccb8d7c519914 Mon Sep 17 00:00:00 2001 From: fbourgey Date: Tue, 20 Aug 2024 19:36:48 -0400 Subject: [PATCH 8/8] breaking line for 88 instead of 89 characters --- pandas/core/generic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index e1adbd48edc81..e6e218c743460 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7490,7 +7490,8 @@ def replace( if is_dict_like(value): # {'A' : NA} -> {'A' : 0} if isinstance(self, ABCSeries): raise ValueError( - "to_replace and value cannot be dict-like for Series.replace" + "to_replace and value cannot be dict-like for " + "Series.replace" ) # Note: Checking below for `in foo.keys()` instead of # `in foo` is needed for when we have a Series and not dict