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