Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate passing args as positional in DataFrame/Series.interpolate #41510

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ Deprecations
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
- Deprecated behavior of :meth:`DatetimeIndex.union` with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`)
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)

.. ---------------------------------------------------------------------------

Expand Down
24 changes: 24 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
Appender,
Substitution,
deprecate_kwarg,
deprecate_nonkeyword_arguments,
doc,
rewrite_axis_style_signature,
)
Expand Down Expand Up @@ -10632,6 +10633,29 @@ def values(self) -> np.ndarray:
self._consolidate_inplace()
return self._mgr.as_array(transpose=True)

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "method"])
def interpolate(
self: DataFrame,
method: str = "linear",
axis: Axis = 0,
limit: int | None = None,
inplace: bool = False,
limit_direction: str | None = None,
limit_area: str | None = None,
downcast: str | None = None,
**kwargs,
) -> DataFrame | None:
return super().interpolate(
method,
axis,
limit,
inplace,
limit_direction,
limit_area,
downcast,
**kwargs,
)


DataFrame._add_numeric_operations()

Expand Down
1 change: 0 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6696,7 +6696,6 @@ def replace(
else:
return result.__finalize__(self, method="replace")

@final
def interpolate(
self: FrameOrSeries,
method: str = "linear",
Expand Down
24 changes: 24 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from pandas.util._decorators import (
Appender,
Substitution,
deprecate_nonkeyword_arguments,
doc,
)
from pandas.util._validators import (
Expand Down Expand Up @@ -5256,6 +5257,29 @@ def to_period(self, freq=None, copy=True) -> Series:
self, method="to_period"
)

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "method"])
def interpolate(
self: Series,
method: str = "linear",
axis: Axis = 0,
limit: int | None = None,
inplace: bool = False,
limit_direction: str | None = None,
limit_area: str | None = None,
downcast: str | None = None,
**kwargs,
) -> Series | None:
return super().interpolate(
method,
axis,
limit,
inplace,
limit_direction,
limit_area,
downcast,
**kwargs,
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if deprecate_nonkeyword_arguments is updated to accept a class argument then maybe could do something like

    interpolate = deprecate_nonkeyword_arguments(
        version=None, allowed_args=["self", "method"], class="DataFrame"
    )(NDFrame.interpolate)

# ----------------------------------------------------------------------
# Add index
_AXIS_ORDERS = ["index"]
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,15 @@ def test_interp_fillna_methods(self, axis, method):
expected = df.fillna(axis=axis, method=method)
result = df.interpolate(method=method, axis=axis)
tm.assert_frame_equal(result, expected)

def test_interpolate_pos_args_deprecation(self):
# https://github.com/pandas-dev/pandas/issues/41485
df = DataFrame({"a": [1, 2, 3]})
msg = (
r"In a future version of pandas all arguments of DataFrame.interpolate "
r"except for the argument 'method' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.interpolate("pad", 0)
expected = DataFrame({"a": [1, 2, 3]})
tm.assert_frame_equal(result, expected)
12 changes: 12 additions & 0 deletions pandas/tests/series/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,15 @@ def test_interpolate_unsorted_index(self, ascending, expected_values):
result = ts.sort_index(ascending=ascending).interpolate(method="index")
expected = Series(data=expected_values, index=expected_values, dtype=float)
tm.assert_series_equal(result, expected)

def test_interpolate_pos_args_deprecation(self):
# https://github.com/pandas-dev/pandas/issues/41485
ser = Series([1, 2, 3])
msg = (
r"In a future version of pandas all arguments of Series.interpolate except "
r"for the argument 'method' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = ser.interpolate("pad", 0)
expected = Series([1, 2, 3])
tm.assert_series_equal(result, expected)