Skip to content

Commit cd733ac

Browse files
committed
deprecate nonkeywordargs in clip
1 parent b2a36bd commit cd733ac

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

asv_bench/benchmarks/series_methods.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def setup(self, n):
124124
self.s = Series(np.random.randn(n))
125125

126126
def time_clip(self, n):
127-
self.s.clip(0, 1)
127+
self.s.clip(lower=0, upper=1)
128128

129129

130130
class ValueCounts:

doc/source/whatsnew/v1.3.0.rst

+4
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,10 @@ Deprecations
647647
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
648648
- 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`)
649649
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
650+
<<<<<<< HEAD
651+
=======
652+
- Deprecated passing arguments as positional in :meth:`DataFrame.clip` and :meth:`Series.clip` (:issue:`41485`)
653+
>>>>>>> 9240ea2561... deprecate nonkeywordargs in clip
650654

651655
.. ---------------------------------------------------------------------------
652656

pandas/core/generic.py

+1
Original file line numberDiff line numberDiff line change
@@ -7469,6 +7469,7 @@ def clip(
74697469
) -> FrameOrSeries | None:
74707470
...
74717471

7472+
@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self"])
74727473
@final
74737474
def clip(
74747475
self: FrameOrSeries,

pandas/tests/frame/methods/test_clip.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_dataframe_clip(self):
3333
df = DataFrame(np.random.randn(1000, 2))
3434

3535
for lb, ub in [(-1, 1), (1, -1)]:
36-
clipped_df = df.clip(lb, ub)
36+
clipped_df = df.clip(lower=lb, upper=ub)
3737

3838
lb, ub = min(lb, ub), max(ub, lb)
3939
lb_mask = df.values <= lb
@@ -48,7 +48,7 @@ def test_clip_mixed_numeric(self):
4848
# clip on mixed integer or floats
4949
# with integer clippers coerces to float
5050
df = DataFrame({"A": [1, 2, 3], "B": [1.0, np.nan, 3.0]})
51-
result = df.clip(1, 2)
51+
result = df.clip(lower=1, upper=2)
5252
expected = DataFrame({"A": [1, 2, 2], "B": [1.0, np.nan, 2.0]})
5353
tm.assert_frame_equal(result, expected, check_like=True)
5454

@@ -67,7 +67,7 @@ def test_clip_against_series(self, inplace):
6767
ub = lb + 1
6868

6969
original = df.copy()
70-
clipped_df = df.clip(lb, ub, axis=0, inplace=inplace)
70+
clipped_df = df.clip(lower=lb, upper=ub, axis=0, inplace=inplace)
7171

7272
if inplace:
7373
clipped_df = df
@@ -113,7 +113,7 @@ def test_clip_against_frame(self, axis):
113113
lb = DataFrame(np.random.randn(1000, 2))
114114
ub = lb + 1
115115

116-
clipped_df = df.clip(lb, ub, axis=axis)
116+
clipped_df = df.clip(lower=lb, upper=ub, axis=axis)
117117

118118
lb_mask = df <= lb
119119
ub_mask = df >= ub
@@ -166,3 +166,13 @@ 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"Starting with Pandas version 2\.0 all arguments of clip except "
175+
r"for the argument 'self' will be keyword-only"
176+
)
177+
with tm.assert_produces_warning(FutureWarning, match=msg):
178+
df.clip(0)

pandas/tests/series/methods/test_clip.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_clip(self, datetime_series):
1818
assert datetime_series.clip(lower=val).min() == val
1919
assert datetime_series.clip(upper=val).max() == val
2020

21-
result = datetime_series.clip(-0.5, 0.5)
21+
result = datetime_series.clip(lower=-0.5, upper=0.5)
2222
expected = np.clip(datetime_series, -0.5, 0.5)
2323
tm.assert_series_equal(result, expected)
2424
assert isinstance(expected, Series)
@@ -74,7 +74,7 @@ def test_clip_with_na_args(self):
7474

7575
# GH#40420
7676
s = Series([1, 2, 3])
77-
result = s.clip(0, [np.nan, np.nan, np.nan])
77+
result = s.clip(lower=0, upper=[np.nan, np.nan, np.nan])
7878
tm.assert_series_equal(s, result)
7979

8080
def test_clip_against_series(self):
@@ -85,8 +85,10 @@ def test_clip_against_series(self):
8585
lower = Series([1.0, 2.0, 3.0])
8686
upper = Series([1.5, 2.5, 3.5])
8787

88-
tm.assert_series_equal(s.clip(lower, upper), Series([1.0, 2.0, 3.5]))
89-
tm.assert_series_equal(s.clip(1.5, upper), Series([1.5, 1.5, 3.5]))
88+
tm.assert_series_equal(
89+
s.clip(lower=lower, upper=upper), Series([1.0, 2.0, 3.5])
90+
)
91+
tm.assert_series_equal(s.clip(lower=1.5, upper=upper), Series([1.5, 1.5, 3.5]))
9092

9193
@pytest.mark.parametrize("inplace", [True, False])
9294
@pytest.mark.parametrize("upper", [[1, 2, 3], np.asarray([1, 2, 3])])

0 commit comments

Comments
 (0)