From 95f1cb34c97c308b4f6fb9f9b1baa06bc54f02cc Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Fri, 1 Sep 2023 13:39:28 +0200 Subject: [PATCH 1/3] REGR: interpolate raising if fill_value is given --- doc/source/whatsnew/v2.1.1.rst | 1 + pandas/core/generic.py | 2 +- pandas/tests/series/methods/test_interpolate.py | 7 +++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.1.rst b/doc/source/whatsnew/v2.1.1.rst index b7e45d043d869..8a59c6137bd5c 100644 --- a/doc/source/whatsnew/v2.1.1.rst +++ b/doc/source/whatsnew/v2.1.1.rst @@ -15,6 +15,7 @@ Fixed regressions ~~~~~~~~~~~~~~~~~ - Fixed regression in :func:`read_csv` when ``usecols`` is given and ``dtypes`` is a dict for ``engine="python"`` (:issue:`54868`) - Fixed regression in :meth:`DataFrame.__setitem__` raising ``AssertionError`` when setting a :class:`Series` with a partial :class:`MultiIndex` (:issue:`54875`) +- Fixed regression in :meth:`Series.interpolate` raising when ``fill_value`` was given (:issue:`54920`) - Fixed regression when comparing a :class:`Series` with ``datetime64`` dtype with ``None`` (:issue:`54870`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b9407ebe6624a..15b9ff66a5218 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8180,7 +8180,7 @@ def interpolate( stacklevel=find_stack_level(), ) - if "fill_value" in kwargs: + if method in fillna_methods and "fill_value" in kwargs: raise ValueError( "'fill_value' is not a valid keyword for " f"{type(self).__name__}.interpolate" diff --git a/pandas/tests/series/methods/test_interpolate.py b/pandas/tests/series/methods/test_interpolate.py index 619690f400d98..a558187b18a64 100644 --- a/pandas/tests/series/methods/test_interpolate.py +++ b/pandas/tests/series/methods/test_interpolate.py @@ -858,3 +858,10 @@ def test_interpolate_asfreq_raises(self): with pytest.raises(ValueError, match=msg): with tm.assert_produces_warning(FutureWarning, match=msg2): ser.interpolate(method="asfreq") + + def test_interpolate_fill_value(self): + # GH#54920 + ser = Series([np.nan, 0, 1, np.nan, 3, np.nan]) + result = ser.interpolate(method="nearest", fill_value=0) + expected = Series([np.nan, 0, 1, 1, 3, 0]) + tm.assert_series_equal(result, expected) From ae41e5edabbb7a9afabe236ad9dc74632bd308de Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sat, 2 Sep 2023 10:58:23 +0200 Subject: [PATCH 2/3] Update test and message --- pandas/core/generic.py | 3 ++- pandas/tests/series/methods/test_interpolate.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 15b9ff66a5218..f94b3f187daab 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8183,7 +8183,8 @@ def interpolate( if method in fillna_methods and "fill_value" in kwargs: raise ValueError( "'fill_value' is not a valid keyword for " - f"{type(self).__name__}.interpolate" + f"{type(self).__name__}.interpolate with method from" + f"{fillna_methods}" ) if isinstance(obj.index, MultiIndex) and method != "linear": diff --git a/pandas/tests/series/methods/test_interpolate.py b/pandas/tests/series/methods/test_interpolate.py index a558187b18a64..549f429f09d35 100644 --- a/pandas/tests/series/methods/test_interpolate.py +++ b/pandas/tests/series/methods/test_interpolate.py @@ -861,6 +861,7 @@ def test_interpolate_asfreq_raises(self): def test_interpolate_fill_value(self): # GH#54920 + pytest.importorskip("scipy") ser = Series([np.nan, 0, 1, np.nan, 3, np.nan]) result = ser.interpolate(method="nearest", fill_value=0) expected = Series([np.nan, 0, 1, 1, 3, 0]) From 843beee19fddb548adace27144622b5c28034515 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Tue, 5 Sep 2023 11:42:59 -0700 Subject: [PATCH 3/3] Update pandas/core/generic.py --- 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 f94b3f187daab..68e15303b69c0 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8183,7 +8183,7 @@ def interpolate( if method in fillna_methods and "fill_value" in kwargs: raise ValueError( "'fill_value' is not a valid keyword for " - f"{type(self).__name__}.interpolate with method from" + f"{type(self).__name__}.interpolate with method from " f"{fillna_methods}" )