Skip to content

Commit b880cf9

Browse files
authored
Deprecate passing args as positional in DataFrame/Series.clip (#41511)
1 parent 81eac3c commit b880cf9

File tree

6 files changed

+53
-109
lines changed

6 files changed

+53
-109
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ Deprecations
678678
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
679679
- 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`)
680680
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
681+
- Deprecated passing arguments as positional in :meth:`DataFrame.clip` and :meth:`Series.clip` (other than ``"upper"`` and ``"lower"``) (:issue:`41485`)
681682
- Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`)
682683
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)
683684
- Deprecated passing arguments as positional in :meth:`DataFrame.sort_index` and :meth:`Series.sort_index` (:issue:`41485`)

pandas/core/frame.py

+14
Original file line numberDiff line numberDiff line change
@@ -10642,6 +10642,20 @@ def values(self) -> np.ndarray:
1064210642
self._consolidate_inplace()
1064310643
return self._mgr.as_array(transpose=True)
1064410644

10645+
@deprecate_nonkeyword_arguments(
10646+
version=None, allowed_args=["self", "lower", "upper"]
10647+
)
10648+
def clip(
10649+
self: DataFrame,
10650+
lower=None,
10651+
upper=None,
10652+
axis: Axis | None = None,
10653+
inplace: bool = False,
10654+
*args,
10655+
**kwargs,
10656+
) -> DataFrame | None:
10657+
return super().clip(lower, upper, axis, inplace, *args, **kwargs)
10658+
1064510659
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "method"])
1064610660
def interpolate(
1064710661
self: DataFrame,

pandas/core/generic.py

-109
Original file line numberDiff line numberDiff line change
@@ -7365,115 +7365,6 @@ def _clip_with_one_bound(self, threshold, method, axis, inplace):
73657365
# GH 40420
73667366
return self.where(subset, threshold, axis=axis, inplace=inplace)
73677367

7368-
@overload
7369-
def clip(
7370-
self: FrameOrSeries,
7371-
lower=...,
7372-
upper=...,
7373-
axis: Axis | None = ...,
7374-
inplace: Literal[False] = ...,
7375-
*args,
7376-
**kwargs,
7377-
) -> FrameOrSeries:
7378-
...
7379-
7380-
@overload
7381-
def clip(
7382-
self: FrameOrSeries,
7383-
lower,
7384-
*,
7385-
axis: Axis | None,
7386-
inplace: Literal[True],
7387-
**kwargs,
7388-
) -> None:
7389-
...
7390-
7391-
@overload
7392-
def clip(
7393-
self: FrameOrSeries,
7394-
lower,
7395-
*,
7396-
inplace: Literal[True],
7397-
**kwargs,
7398-
) -> None:
7399-
...
7400-
7401-
@overload
7402-
def clip(
7403-
self: FrameOrSeries,
7404-
*,
7405-
upper,
7406-
axis: Axis | None,
7407-
inplace: Literal[True],
7408-
**kwargs,
7409-
) -> None:
7410-
...
7411-
7412-
@overload
7413-
def clip(
7414-
self: FrameOrSeries,
7415-
*,
7416-
upper,
7417-
inplace: Literal[True],
7418-
**kwargs,
7419-
) -> None:
7420-
...
7421-
7422-
@overload
7423-
def clip(
7424-
self: FrameOrSeries,
7425-
*,
7426-
axis: Axis | None,
7427-
inplace: Literal[True],
7428-
**kwargs,
7429-
) -> None:
7430-
...
7431-
7432-
@overload
7433-
def clip(
7434-
self: FrameOrSeries,
7435-
lower,
7436-
upper,
7437-
axis: Axis | None,
7438-
inplace: Literal[True],
7439-
*args,
7440-
**kwargs,
7441-
) -> None:
7442-
...
7443-
7444-
@overload
7445-
def clip(
7446-
self: FrameOrSeries,
7447-
lower,
7448-
upper,
7449-
*,
7450-
inplace: Literal[True],
7451-
**kwargs,
7452-
) -> None:
7453-
...
7454-
7455-
@overload
7456-
def clip(
7457-
self: FrameOrSeries,
7458-
*,
7459-
inplace: Literal[True],
7460-
**kwargs,
7461-
) -> None:
7462-
...
7463-
7464-
@overload
7465-
def clip(
7466-
self: FrameOrSeries,
7467-
lower=...,
7468-
upper=...,
7469-
axis: Axis | None = ...,
7470-
inplace: bool_t = ...,
7471-
*args,
7472-
**kwargs,
7473-
) -> FrameOrSeries | None:
7474-
...
7475-
7476-
@final
74777368
def clip(
74787369
self: FrameOrSeries,
74797370
lower=None,

pandas/core/series.py

+14
Original file line numberDiff line numberDiff line change
@@ -5289,6 +5289,20 @@ def to_period(self, freq=None, copy=True) -> Series:
52895289
self, method="to_period"
52905290
)
52915291

5292+
@deprecate_nonkeyword_arguments(
5293+
version=None, allowed_args=["self", "lower", "upper"]
5294+
)
5295+
def clip(
5296+
self: Series,
5297+
lower=None,
5298+
upper=None,
5299+
axis: Axis | None = None,
5300+
inplace: bool = False,
5301+
*args,
5302+
**kwargs,
5303+
) -> Series | None:
5304+
return super().clip(lower, upper, axis, inplace, *args, **kwargs)
5305+
52925306
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "method"])
52935307
def interpolate(
52945308
self: Series,

pandas/tests/frame/methods/test_clip.py

+12
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,15 @@ def test_clip_with_na_args(self, float_frame):
166166
result = df.clip(lower=t, axis=0)
167167
expected = DataFrame({"col_0": [9, -3, 0, 6, 5], "col_1": [2, -4, 6, 8, 3]})
168168
tm.assert_frame_equal(result, expected)
169+
170+
def test_clip_pos_args_deprecation(self):
171+
# https://github.com/pandas-dev/pandas/issues/41485
172+
df = DataFrame({"a": [1, 2, 3]})
173+
msg = (
174+
r"In a future version of pandas all arguments of DataFrame.clip except "
175+
r"for the arguments 'lower' and 'upper' will be keyword-only"
176+
)
177+
with tm.assert_produces_warning(FutureWarning, match=msg):
178+
result = df.clip(0, 1, 0)
179+
expected = DataFrame({"a": [1, 1, 1]})
180+
tm.assert_frame_equal(result, expected)

pandas/tests/series/methods/test_clip.py

+12
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,15 @@ def test_clip_with_datetimes(self):
127127
]
128128
)
129129
tm.assert_series_equal(result, expected)
130+
131+
def test_clip_pos_args_deprecation(self):
132+
# https://github.com/pandas-dev/pandas/issues/41485
133+
ser = Series([1, 2, 3])
134+
msg = (
135+
r"In a future version of pandas all arguments of Series.clip except "
136+
r"for the arguments 'lower' and 'upper' will be keyword-only"
137+
)
138+
with tm.assert_produces_warning(FutureWarning, match=msg):
139+
result = ser.clip(0, 1, 0)
140+
expected = Series([1, 1, 1])
141+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)