Skip to content

Commit e1e8783

Browse files
committed
ENH: Support inplace clip (pandas-dev#15388)
1 parent a8a497f commit e1e8783

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

pandas/core/generic.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -4119,8 +4119,7 @@ def isnull(self):
41194119
def notnull(self):
41204120
return notnull(self).__finalize__(self)
41214121

4122-
def _clip_with_scalar(self, lower, upper):
4123-
4122+
def _clip_with_scalar(self, lower, upper, inplace=False):
41244123
if ((lower is not None and np.any(isnull(lower))) or
41254124
(upper is not None and np.any(isnull(upper)))):
41264125
raise ValueError("Cannot use an NA value as a clip threshold")
@@ -4136,10 +4135,15 @@ def _clip_with_scalar(self, lower, upper):
41364135
if np.any(mask):
41374136
result[mask] = np.nan
41384137

4139-
return self._constructor(
4140-
result, **self._construct_axes_dict()).__finalize__(self)
4138+
axes_dict = self._construct_axes_dict()
4139+
result = self._constructor(result, **axes_dict).__finalize__(self)
4140+
4141+
if inplace:
4142+
self._update_inplace(result)
4143+
else:
4144+
return result
41414145

4142-
def clip(self, lower=None, upper=None, axis=None, *args, **kwargs):
4146+
def clip(self, lower=None, upper=None, axis=None, inplace=False, *args, **kwargs):
41434147
"""
41444148
Trim values at input threshold(s).
41454149
@@ -4201,17 +4205,17 @@ def clip(self, lower=None, upper=None, axis=None, *args, **kwargs):
42014205
# fast-path for scalars
42024206
if ((lower is None or (is_scalar(lower) and is_number(lower))) and
42034207
(upper is None or (is_scalar(upper) and is_number(upper)))):
4204-
return self._clip_with_scalar(lower, upper)
4208+
return self._clip_with_scalar(lower, upper, inplace=inplace)
42054209

42064210
result = self
42074211
if lower is not None:
4208-
result = result.clip_lower(lower, axis)
4212+
result = result.clip_lower(lower, axis, inplace=inplace)
42094213
if upper is not None:
4210-
result = result.clip_upper(upper, axis)
4214+
result = result.clip_upper(upper, axis, inplace=inplace)
42114215

42124216
return result
42134217

4214-
def clip_upper(self, threshold, axis=None):
4218+
def clip_upper(self, threshold, axis=None, inplace=False):
42154219
"""
42164220
Return copy of input with values above given value(s) truncated.
42174221
@@ -4233,12 +4237,12 @@ def clip_upper(self, threshold, axis=None):
42334237
raise ValueError("Cannot use an NA value as a clip threshold")
42344238

42354239
if is_scalar(threshold) and is_number(threshold):
4236-
return self._clip_with_scalar(None, threshold)
4240+
return self._clip_with_scalar(None, threshold, inplace=inplace)
42374241

42384242
subset = self.le(threshold, axis=axis) | isnull(self)
4239-
return self.where(subset, threshold, axis=axis)
4243+
return self.where(subset, threshold, axis=axis, inplace=inplace)
42404244

4241-
def clip_lower(self, threshold, axis=None):
4245+
def clip_lower(self, threshold, axis=None, inplace=False):
42424246
"""
42434247
Return copy of the input with values below given value(s) truncated.
42444248
@@ -4260,10 +4264,10 @@ def clip_lower(self, threshold, axis=None):
42604264
raise ValueError("Cannot use an NA value as a clip threshold")
42614265

42624266
if is_scalar(threshold) and is_number(threshold):
4263-
return self._clip_with_scalar(threshold, None)
4267+
return self._clip_with_scalar(threshold, None, inplace=inplace)
42644268

42654269
subset = self.ge(threshold, axis=axis) | isnull(self)
4266-
return self.where(subset, threshold, axis=axis)
4270+
return self.where(subset, threshold, axis=axis, inplace=inplace)
42674271

42684272
def groupby(self, by=None, axis=0, level=None, as_index=True, sort=True,
42694273
group_keys=True, squeeze=False, **kwargs):

0 commit comments

Comments
 (0)