Skip to content

Commit

Permalink
Deprecate passing args as positional in DataFrame/Series.interpolate (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed May 19, 2021
1 parent bda839c commit a395185
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
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
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
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
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,
)

# ----------------------------------------------------------------------
# Add index
_AXIS_ORDERS = ["index"]
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/methods/test_interpolate.py
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
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)

0 comments on commit a395185

Please sign in to comment.