Skip to content

Commit a395185

Browse files
authored
Deprecate passing args as positional in DataFrame/Series.interpolate (#41510)
1 parent bda839c commit a395185

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ Deprecations
648648
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
649649
- 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`)
650650
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
651+
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)
651652

652653
.. ---------------------------------------------------------------------------
653654

pandas/core/frame.py

+24
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
Appender,
7878
Substitution,
7979
deprecate_kwarg,
80+
deprecate_nonkeyword_arguments,
8081
doc,
8182
rewrite_axis_style_signature,
8283
)
@@ -10632,6 +10633,29 @@ def values(self) -> np.ndarray:
1063210633
self._consolidate_inplace()
1063310634
return self._mgr.as_array(transpose=True)
1063410635

10636+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "method"])
10637+
def interpolate(
10638+
self: DataFrame,
10639+
method: str = "linear",
10640+
axis: Axis = 0,
10641+
limit: int | None = None,
10642+
inplace: bool = False,
10643+
limit_direction: str | None = None,
10644+
limit_area: str | None = None,
10645+
downcast: str | None = None,
10646+
**kwargs,
10647+
) -> DataFrame | None:
10648+
return super().interpolate(
10649+
method,
10650+
axis,
10651+
limit,
10652+
inplace,
10653+
limit_direction,
10654+
limit_area,
10655+
downcast,
10656+
**kwargs,
10657+
)
10658+
1063510659

1063610660
DataFrame._add_numeric_operations()
1063710661

pandas/core/generic.py

-1
Original file line numberDiff line numberDiff line change
@@ -6696,7 +6696,6 @@ def replace(
66966696
else:
66976697
return result.__finalize__(self, method="replace")
66986698

6699-
@final
67006699
def interpolate(
67016700
self: FrameOrSeries,
67026701
method: str = "linear",

pandas/core/series.py

+24
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from pandas.util._decorators import (
5252
Appender,
5353
Substitution,
54+
deprecate_nonkeyword_arguments,
5455
doc,
5556
)
5657
from pandas.util._validators import (
@@ -5256,6 +5257,29 @@ def to_period(self, freq=None, copy=True) -> Series:
52565257
self, method="to_period"
52575258
)
52585259

5260+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "method"])
5261+
def interpolate(
5262+
self: Series,
5263+
method: str = "linear",
5264+
axis: Axis = 0,
5265+
limit: int | None = None,
5266+
inplace: bool = False,
5267+
limit_direction: str | None = None,
5268+
limit_area: str | None = None,
5269+
downcast: str | None = None,
5270+
**kwargs,
5271+
) -> Series | None:
5272+
return super().interpolate(
5273+
method,
5274+
axis,
5275+
limit,
5276+
inplace,
5277+
limit_direction,
5278+
limit_area,
5279+
downcast,
5280+
**kwargs,
5281+
)
5282+
52595283
# ----------------------------------------------------------------------
52605284
# Add index
52615285
_AXIS_ORDERS = ["index"]

pandas/tests/frame/methods/test_interpolate.py

+12
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,15 @@ def test_interp_fillna_methods(self, axis, method):
342342
expected = df.fillna(axis=axis, method=method)
343343
result = df.interpolate(method=method, axis=axis)
344344
tm.assert_frame_equal(result, expected)
345+
346+
def test_interpolate_pos_args_deprecation(self):
347+
# https://github.com/pandas-dev/pandas/issues/41485
348+
df = DataFrame({"a": [1, 2, 3]})
349+
msg = (
350+
r"In a future version of pandas all arguments of DataFrame.interpolate "
351+
r"except for the argument 'method' will be keyword-only"
352+
)
353+
with tm.assert_produces_warning(FutureWarning, match=msg):
354+
result = df.interpolate("pad", 0)
355+
expected = DataFrame({"a": [1, 2, 3]})
356+
tm.assert_frame_equal(result, expected)

pandas/tests/series/methods/test_interpolate.py

+12
Original file line numberDiff line numberDiff line change
@@ -811,3 +811,15 @@ def test_interpolate_unsorted_index(self, ascending, expected_values):
811811
result = ts.sort_index(ascending=ascending).interpolate(method="index")
812812
expected = Series(data=expected_values, index=expected_values, dtype=float)
813813
tm.assert_series_equal(result, expected)
814+
815+
def test_interpolate_pos_args_deprecation(self):
816+
# https://github.com/pandas-dev/pandas/issues/41485
817+
ser = Series([1, 2, 3])
818+
msg = (
819+
r"In a future version of pandas all arguments of Series.interpolate except "
820+
r"for the argument 'method' will be keyword-only"
821+
)
822+
with tm.assert_produces_warning(FutureWarning, match=msg):
823+
result = ser.interpolate("pad", 0)
824+
expected = Series([1, 2, 3])
825+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)