@@ -4120,8 +4120,7 @@ def isnull(self):
41204120 def notnull (self ):
41214121 return notnull (self ).__finalize__ (self )
41224122
4123- def _clip_with_scalar (self , lower , upper ):
4124-
4123+ def _clip_with_scalar (self , lower , upper , inplace = False ):
41254124 if ((lower is not None and np .any (isnull (lower ))) or
41264125 (upper is not None and np .any (isnull (upper )))):
41274126 raise ValueError ("Cannot use an NA value as a clip threshold" )
@@ -4137,10 +4136,16 @@ def _clip_with_scalar(self, lower, upper):
41374136 if np .any (mask ):
41384137 result [mask ] = np .nan
41394138
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
41424146
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 ):
41444149 """
41454150 Trim values at input threshold(s).
41464151
@@ -4150,6 +4155,9 @@ def clip(self, lower=None, upper=None, axis=None, *args, **kwargs):
41504155 upper : float or array_like, default None
41514156 axis : int or string axis name, optional
41524157 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
41534161
41544162 Returns
41554163 -------
@@ -4192,6 +4200,8 @@ def clip(self, lower=None, upper=None, axis=None, *args, **kwargs):
41924200 if isinstance (self , ABCPanel ):
41934201 raise NotImplementedError ("clip is not supported yet for panels" )
41944202
4203+ inplace = validate_bool_kwarg (inplace , 'inplace' )
4204+
41954205 axis = nv .validate_clip_with_axis (axis , args , kwargs )
41964206
41974207 # GH 2747 (arguments were reversed)
@@ -4202,17 +4212,20 @@ def clip(self, lower=None, upper=None, axis=None, *args, **kwargs):
42024212 # fast-path for scalars
42034213 if ((lower is None or (is_scalar (lower ) and is_number (lower ))) and
42044214 (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 )
42064216
42074217 result = self
42084218 if lower is not None :
4209- result = result .clip_lower (lower , axis )
4219+ result = result .clip_lower (lower , axis , inplace = inplace )
42104220 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 )
42124225
42134226 return result
42144227
4215- def clip_upper (self , threshold , axis = None ):
4228+ def clip_upper (self , threshold , axis = None , inplace = False ):
42164229 """
42174230 Return copy of input with values above given value(s) truncated.
42184231
@@ -4221,6 +4234,9 @@ def clip_upper(self, threshold, axis=None):
42214234 threshold : float or array_like
42224235 axis : int or string axis name, optional
42234236 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
42244240
42254241 See Also
42264242 --------
@@ -4234,12 +4250,14 @@ def clip_upper(self, threshold, axis=None):
42344250 raise ValueError ("Cannot use an NA value as a clip threshold" )
42354251
42364252 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' )
42384256
42394257 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 )
42414259
4242- def clip_lower (self , threshold , axis = None ):
4260+ def clip_lower (self , threshold , axis = None , inplace = False ):
42434261 """
42444262 Return copy of the input with values below given value(s) truncated.
42454263
@@ -4248,6 +4266,9 @@ def clip_lower(self, threshold, axis=None):
42484266 threshold : float or array_like
42494267 axis : int or string axis name, optional
42504268 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
42514272
42524273 See Also
42534274 --------
@@ -4261,10 +4282,12 @@ def clip_lower(self, threshold, axis=None):
42614282 raise ValueError ("Cannot use an NA value as a clip threshold" )
42624283
42634284 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' )
42654288
42664289 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 )
42684291
42694292 def groupby (self , by = None , axis = 0 , level = None , as_index = True , sort = True ,
42704293 group_keys = True , squeeze = False , ** kwargs ):
0 commit comments