@@ -4119,8 +4119,7 @@ def isnull(self):
4119
4119
def notnull (self ):
4120
4120
return notnull (self ).__finalize__ (self )
4121
4121
4122
- def _clip_with_scalar (self , lower , upper ):
4123
-
4122
+ def _clip_with_scalar (self , lower , upper , inplace = False ):
4124
4123
if ((lower is not None and np .any (isnull (lower ))) or
4125
4124
(upper is not None and np .any (isnull (upper )))):
4126
4125
raise ValueError ("Cannot use an NA value as a clip threshold" )
@@ -4136,10 +4135,16 @@ def _clip_with_scalar(self, lower, upper):
4136
4135
if np .any (mask ):
4137
4136
result [mask ] = np .nan
4138
4137
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
4141
4145
4142
- def clip (self , lower = None , upper = None , axis = None , * args , ** kwargs ):
4146
+ def clip (self , lower = None , upper = None , axis = None , inplace = False ,
4147
+ * args , ** kwargs ):
4143
4148
"""
4144
4149
Trim values at input threshold(s).
4145
4150
@@ -4149,6 +4154,9 @@ def clip(self, lower=None, upper=None, axis=None, *args, **kwargs):
4149
4154
upper : float or array_like, default None
4150
4155
axis : int or string axis name, optional
4151
4156
Align object with lower and upper along the given axis.
4157
+ inplace : boolean, default False
4158
+ Whether to perform the operation in place on the data
4159
+ .. versionadded:: 0.21.0
4152
4160
4153
4161
Returns
4154
4162
-------
@@ -4191,6 +4199,8 @@ def clip(self, lower=None, upper=None, axis=None, *args, **kwargs):
4191
4199
if isinstance (self , ABCPanel ):
4192
4200
raise NotImplementedError ("clip is not supported yet for panels" )
4193
4201
4202
+ inplace = validate_bool_kwarg (inplace , 'inplace' )
4203
+
4194
4204
axis = nv .validate_clip_with_axis (axis , args , kwargs )
4195
4205
4196
4206
# GH 2747 (arguments were reversed)
@@ -4201,17 +4211,20 @@ def clip(self, lower=None, upper=None, axis=None, *args, **kwargs):
4201
4211
# fast-path for scalars
4202
4212
if ((lower is None or (is_scalar (lower ) and is_number (lower ))) and
4203
4213
(upper is None or (is_scalar (upper ) and is_number (upper )))):
4204
- return self ._clip_with_scalar (lower , upper )
4214
+ return self ._clip_with_scalar (lower , upper , inplace = inplace )
4205
4215
4206
4216
result = self
4207
4217
if lower is not None :
4208
- result = result .clip_lower (lower , axis )
4218
+ result = result .clip_lower (lower , axis , inplace = inplace )
4209
4219
if upper is not None :
4210
- result = result .clip_upper (upper , axis )
4220
+ if inplace :
4221
+ result = self
4222
+
4223
+ result = result .clip_upper (upper , axis , inplace = inplace )
4211
4224
4212
4225
return result
4213
4226
4214
- def clip_upper (self , threshold , axis = None ):
4227
+ def clip_upper (self , threshold , axis = None , inplace = False ):
4215
4228
"""
4216
4229
Return copy of input with values above given value(s) truncated.
4217
4230
@@ -4220,6 +4233,9 @@ def clip_upper(self, threshold, axis=None):
4220
4233
threshold : float or array_like
4221
4234
axis : int or string axis name, optional
4222
4235
Align object with threshold along the given axis.
4236
+ inplace : boolean, default False
4237
+ Whether to perform the operation in place on the data
4238
+ .. versionadded:: 0.21.0
4223
4239
4224
4240
See Also
4225
4241
--------
@@ -4233,12 +4249,14 @@ def clip_upper(self, threshold, axis=None):
4233
4249
raise ValueError ("Cannot use an NA value as a clip threshold" )
4234
4250
4235
4251
if is_scalar (threshold ) and is_number (threshold ):
4236
- return self ._clip_with_scalar (None , threshold )
4252
+ return self ._clip_with_scalar (None , threshold , inplace = inplace )
4253
+
4254
+ inplace = validate_bool_kwarg (inplace , 'inplace' )
4237
4255
4238
4256
subset = self .le (threshold , axis = axis ) | isnull (self )
4239
- return self .where (subset , threshold , axis = axis )
4257
+ return self .where (subset , threshold , axis = axis , inplace = inplace )
4240
4258
4241
- def clip_lower (self , threshold , axis = None ):
4259
+ def clip_lower (self , threshold , axis = None , inplace = False ):
4242
4260
"""
4243
4261
Return copy of the input with values below given value(s) truncated.
4244
4262
@@ -4247,6 +4265,9 @@ def clip_lower(self, threshold, axis=None):
4247
4265
threshold : float or array_like
4248
4266
axis : int or string axis name, optional
4249
4267
Align object with threshold along the given axis.
4268
+ inplace : boolean, default False
4269
+ Whether to perform the operation in place on the data
4270
+ .. versionadded:: 0.21.0
4250
4271
4251
4272
See Also
4252
4273
--------
@@ -4260,10 +4281,12 @@ def clip_lower(self, threshold, axis=None):
4260
4281
raise ValueError ("Cannot use an NA value as a clip threshold" )
4261
4282
4262
4283
if is_scalar (threshold ) and is_number (threshold ):
4263
- return self ._clip_with_scalar (threshold , None )
4284
+ return self ._clip_with_scalar (threshold , None , inplace = inplace )
4285
+
4286
+ inplace = validate_bool_kwarg (inplace , 'inplace' )
4264
4287
4265
4288
subset = self .ge (threshold , axis = axis ) | isnull (self )
4266
- return self .where (subset , threshold , axis = axis )
4289
+ return self .where (subset , threshold , axis = axis , inplace = inplace )
4267
4290
4268
4291
def groupby (self , by = None , axis = 0 , level = None , as_index = True , sort = True ,
4269
4292
group_keys = True , squeeze = False , ** kwargs ):
0 commit comments