diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index c2112f259f1f4..a5f460abdb6dd 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -737,6 +737,7 @@ Other Deprecations - Deprecated backward-compatibility behavior for :meth:`DataFrame.select_dtypes` matching "str" dtype when ``np.object_`` is specified (:issue:`61916`) - Deprecated option "future.no_silent_downcasting", as it is no longer used. In a future version accessing this option will raise (:issue:`59502`) - Deprecated slicing on a :class:`Series` or :class:`DataFrame` with a :class:`DatetimeIndex` using a ``datetime.date`` object, explicitly cast to :class:`Timestamp` instead (:issue:`35830`) +- Deprecated the 'inplace' keyword from :meth:`Resampler.interpolate`, as passing ``True`` raises ``AttributeError`` (:issue:`58690`) .. --------------------------------------------------------------------------- .. _whatsnew_300.prior_deprecations: diff --git a/pandas/core/resample.py b/pandas/core/resample.py index eb5f963598b02..f76272798a03f 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -26,7 +26,10 @@ to_offset, ) from pandas._typing import NDFrameT -from pandas.errors import AbstractMethodError +from pandas.errors import ( + AbstractMethodError, + Pandas4Warning, +) from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.dtypes import ( @@ -847,7 +850,6 @@ def interpolate( *, axis: Axis = 0, limit: int | None = None, - inplace: bool = False, limit_direction: Literal["forward", "backward", "both"] = "forward", limit_area=None, **kwargs, @@ -892,8 +894,6 @@ def interpolate( limit : int, optional Maximum number of consecutive NaNs to fill. Must be greater than 0. - inplace : bool, default False - Update the data in place if possible. limit_direction : {{'forward', 'backward', 'both'}}, Optional Consecutive NaNs will be filled in this direction. @@ -987,6 +987,19 @@ def interpolate( Note that the series correctly decreases between two anchors ``07:00:00`` and ``07:00:02``. """ + if "inplace" in kwargs: + # GH#58690 + warnings.warn( + f"The 'inplace' keyword in {type(self).__name__}.interpolate " + "is deprecated and will be removed in a future version. " + "resample(...).interpolate is never inplace.", + Pandas4Warning, + stacklevel=find_stack_level(), + ) + inplace = kwargs.pop("inplace") + if inplace: + raise ValueError("Cannot interpolate inplace on a resampled object.") + result = self._upsample("asfreq") # If the original data has timestamps which are not aligned with the @@ -1020,7 +1033,7 @@ def interpolate( method=method, axis=axis, limit=limit, - inplace=inplace, + inplace=False, limit_direction=limit_direction, limit_area=limit_area, **kwargs, diff --git a/pandas/tests/resample/test_base.py b/pandas/tests/resample/test_base.py index 12128be7c2d30..6bfad10b5c32d 100644 --- a/pandas/tests/resample/test_base.py +++ b/pandas/tests/resample/test_base.py @@ -3,6 +3,8 @@ import numpy as np import pytest +from pandas.errors import Pandas4Warning + from pandas.core.dtypes.common import is_extension_array_dtype import pandas as pd @@ -109,6 +111,22 @@ def test_resample_interpolate(index): tm.assert_frame_equal(result, expected) +def test_resample_interpolate_inplace_deprecated(): + # GH#58690 + dti = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D") + + df = DataFrame(range(len(dti)), index=dti) + rs = df.resample("1min") + msg = "The 'inplace' keyword in DatetimeIndexResampler.interpolate" + with tm.assert_produces_warning(Pandas4Warning, match=msg): + rs.interpolate(inplace=False) + + msg2 = "Cannot interpolate inplace on a resampled object" + with pytest.raises(ValueError, match=msg2): + with tm.assert_produces_warning(Pandas4Warning, match=msg): + rs.interpolate(inplace=True) + + def test_resample_interpolate_regular_sampling_off_grid( all_1d_no_arg_interpolation_methods, ):