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