Skip to content

Commit 7e192de

Browse files
datapythonistaPingviinituutti
authored andcommitted
DEPR: Deprecating .clip_lower and .clip_upper (pandas-dev#24203)
1 parent 3df8a6a commit 7e192de

File tree

4 files changed

+47
-30
lines changed

4 files changed

+47
-30
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,7 @@ Deprecations
11151115
- Creating a :class:`TimedeltaIndex` or :class:`DatetimeIndex` by passing range arguments `start`, `end`, and `periods` is deprecated in favor of :func:`timedelta_range` and :func:`date_range` (:issue:`23919`)
11161116
- Passing a string alias like ``'datetime64[ns, UTC]'`` as the `unit` parameter to :class:`DatetimeTZDtype` is deprecated. Use :class:`DatetimeTZDtype.construct_from_string` instead (:issue:`23990`).
11171117
- In :meth:`Series.where` with Categorical data, providing an ``other`` that is not present in the categories is deprecated. Convert the categorical to a different dtype or add the ``other`` to the categories first (:issue:`24077`).
1118+
- :meth:`Series.clip_lower`, :meth:`Series.clip_upper`, :meth:`DataFrame.clip_lower` and :meth:`DataFrame.clip_upper` are deprecated and will be removed in a future version. Use ``Series.clip(lower=threshold)``, ``Series.clip(upper=threshold)`` and the equivalent ``DataFrame`` methods (:issue:`24203`)
11181119

11191120

11201121
.. _whatsnew_0240.deprecations.datetimelike_int_ops:

pandas/core/generic.py

+22-19
Original file line numberDiff line numberDiff line change
@@ -7204,11 +7204,6 @@ def clip(self, lower=None, upper=None, axis=None, inplace=False,
72047204
Same type as calling object with the values outside the
72057205
clip boundaries replaced
72067206
7207-
See Also
7208-
--------
7209-
clip_lower : Clip values below specified threshold(s).
7210-
clip_upper : Clip values above specified threshold(s).
7211-
72127207
Examples
72137208
--------
72147209
>>> data = {'col_0': [9, -3, 0, -1, 5], 'col_1': [-2, -7, 6, 8, -5]}
@@ -7281,18 +7276,23 @@ def clip(self, lower=None, upper=None, axis=None, inplace=False,
72817276

72827277
result = self
72837278
if lower is not None:
7284-
result = result.clip_lower(lower, axis, inplace=inplace)
7279+
result = result._clip_with_one_bound(lower, method=self.ge,
7280+
axis=axis, inplace=inplace)
72857281
if upper is not None:
72867282
if inplace:
72877283
result = self
7288-
result = result.clip_upper(upper, axis, inplace=inplace)
7284+
result = result._clip_with_one_bound(upper, method=self.le,
7285+
axis=axis, inplace=inplace)
72897286

72907287
return result
72917288

72927289
def clip_upper(self, threshold, axis=None, inplace=False):
72937290
"""
72947291
Trim values above a given threshold.
72957292
7293+
.. deprecated:: 0.24.0
7294+
Use clip(upper=threshold) instead.
7295+
72967296
Elements above the `threshold` will be changed to match the
72977297
`threshold` value(s). Threshold can be a single value or an array,
72987298
in the latter case it performs the truncation element-wise.
@@ -7319,18 +7319,15 @@ def clip_upper(self, threshold, axis=None, inplace=False):
73197319
73207320
Returns
73217321
-------
7322-
clipped
7322+
Series or DataFrame
73237323
Original data with values trimmed.
73247324
73257325
See Also
73267326
--------
7327-
DataFrame.clip : General purpose method to trim DataFrame values to
7328-
given threshold(s).
7329-
DataFrame.clip_lower : Trim DataFrame values below given
7330-
threshold(s).
73317327
Series.clip : General purpose method to trim Series values to given
73327328
threshold(s).
7333-
Series.clip_lower : Trim Series values below given threshold(s).
7329+
DataFrame.clip : General purpose method to trim DataFrame values to
7330+
given threshold(s).
73347331
73357332
Examples
73367333
--------
@@ -7363,13 +7360,19 @@ def clip_upper(self, threshold, axis=None, inplace=False):
73637360
4 1
73647361
dtype: int64
73657362
"""
7363+
warnings.warn('clip_upper(threshold) is deprecated, '
7364+
'use clip(upper=threshold) instead',
7365+
FutureWarning, stacklevel=2)
73667366
return self._clip_with_one_bound(threshold, method=self.le,
73677367
axis=axis, inplace=inplace)
73687368

73697369
def clip_lower(self, threshold, axis=None, inplace=False):
73707370
"""
73717371
Trim values below a given threshold.
73727372
7373+
.. deprecated:: 0.24.0
7374+
Use clip(lower=threshold) instead.
7375+
73737376
Elements below the `threshold` will be changed to match the
73747377
`threshold` value(s). Threshold can be a single value or an array,
73757378
in the latter case it performs the truncation element-wise.
@@ -7397,18 +7400,15 @@ def clip_lower(self, threshold, axis=None, inplace=False):
73977400
73987401
Returns
73997402
-------
7400-
clipped
7403+
Series or DataFrame
74017404
Original data with values trimmed.
74027405
74037406
See Also
74047407
--------
7405-
DataFrame.clip : General purpose method to trim DataFrame values to
7406-
given threshold(s).
7407-
DataFrame.clip_upper : Trim DataFrame values above given
7408-
threshold(s).
74097408
Series.clip : General purpose method to trim Series values to given
74107409
threshold(s).
7411-
Series.clip_upper : Trim Series values above given threshold(s).
7410+
DataFrame.clip : General purpose method to trim DataFrame values to
7411+
given threshold(s).
74127412
74137413
Examples
74147414
--------
@@ -7476,6 +7476,9 @@ def clip_lower(self, threshold, axis=None, inplace=False):
74767476
1 4 5
74777477
2 5 6
74787478
"""
7479+
warnings.warn('clip_lower(threshold) is deprecated, '
7480+
'use clip(lower=threshold) instead',
7481+
FutureWarning, stacklevel=2)
74797482
return self._clip_with_one_bound(threshold, method=self.ge,
74807483
axis=axis, inplace=inplace)
74817484

pandas/tests/frame/test_analytics.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1841,10 +1841,12 @@ def test_clip(self, float_frame):
18411841
median = float_frame.median().median()
18421842
original = float_frame.copy()
18431843

1844-
capped = float_frame.clip_upper(median)
1844+
with tm.assert_produces_warning(FutureWarning):
1845+
capped = float_frame.clip_upper(median)
18451846
assert not (capped.values > median).any()
18461847

1847-
floored = float_frame.clip_lower(median)
1848+
with tm.assert_produces_warning(FutureWarning):
1849+
floored = float_frame.clip_lower(median)
18481850
assert not (floored.values < median).any()
18491851

18501852
double = float_frame.clip(upper=median, lower=median)
@@ -1858,11 +1860,13 @@ def test_inplace_clip(self, float_frame):
18581860
median = float_frame.median().median()
18591861
frame_copy = float_frame.copy()
18601862

1861-
frame_copy.clip_upper(median, inplace=True)
1863+
with tm.assert_produces_warning(FutureWarning):
1864+
frame_copy.clip_upper(median, inplace=True)
18621865
assert not (frame_copy.values > median).any()
18631866
frame_copy = float_frame.copy()
18641867

1865-
frame_copy.clip_lower(median, inplace=True)
1868+
with tm.assert_produces_warning(FutureWarning):
1869+
frame_copy.clip_lower(median, inplace=True)
18661870
assert not (frame_copy.values < median).any()
18671871
frame_copy = float_frame.copy()
18681872

@@ -2263,7 +2267,8 @@ def test_series_broadcasting(self):
22632267
s_nan = Series([np.nan, np.nan, 1])
22642268

22652269
with tm.assert_produces_warning(None):
2266-
df_nan.clip_lower(s, axis=0)
2270+
with tm.assert_produces_warning(FutureWarning):
2271+
df_nan.clip_lower(s, axis=0)
22672272
for op in ['lt', 'le', 'gt', 'ge', 'eq', 'ne']:
22682273
getattr(df, op)(s_nan, axis=0)
22692274

pandas/tests/series/test_analytics.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,10 @@ def test_matmul(self):
924924
def test_clip(self, datetime_series):
925925
val = datetime_series.median()
926926

927-
assert datetime_series.clip_lower(val).min() == val
928-
assert datetime_series.clip_upper(val).max() == val
927+
with tm.assert_produces_warning(FutureWarning):
928+
assert datetime_series.clip_lower(val).min() == val
929+
with tm.assert_produces_warning(FutureWarning):
930+
assert datetime_series.clip_upper(val).max() == val
929931

930932
assert datetime_series.clip(lower=val).min() == val
931933
assert datetime_series.clip(upper=val).max() == val
@@ -943,8 +945,10 @@ def test_clip_types_and_nulls(self):
943945

944946
for s in sers:
945947
thresh = s[2]
946-
lower = s.clip_lower(thresh)
947-
upper = s.clip_upper(thresh)
948+
with tm.assert_produces_warning(FutureWarning):
949+
lower = s.clip_lower(thresh)
950+
with tm.assert_produces_warning(FutureWarning):
951+
upper = s.clip_upper(thresh)
948952
assert lower[notna(lower)].min() == thresh
949953
assert upper[notna(upper)].max() == thresh
950954
assert list(isna(s)) == list(isna(lower))
@@ -971,8 +975,12 @@ def test_clip_against_series(self):
971975
s = Series([1.0, 1.0, 4.0])
972976
threshold = Series([1.0, 2.0, 3.0])
973977

974-
assert_series_equal(s.clip_lower(threshold), Series([1.0, 2.0, 4.0]))
975-
assert_series_equal(s.clip_upper(threshold), Series([1.0, 1.0, 3.0]))
978+
with tm.assert_produces_warning(FutureWarning):
979+
assert_series_equal(s.clip_lower(threshold),
980+
Series([1.0, 2.0, 4.0]))
981+
with tm.assert_produces_warning(FutureWarning):
982+
assert_series_equal(s.clip_upper(threshold),
983+
Series([1.0, 1.0, 3.0]))
976984

977985
lower = Series([1.0, 2.0, 3.0])
978986
upper = Series([1.5, 2.5, 3.5])

0 commit comments

Comments
 (0)