@@ -4287,6 +4287,39 @@ def _clip_with_scalar(self, lower, upper, inplace=False):
4287
4287
else :
4288
4288
return result
4289
4289
4290
+ def _clip_with_one_bound (self , threshold , method , axis , inplace ):
4291
+
4292
+ if np .any (isnull (threshold )):
4293
+ raise ValueError ("Cannot use an NA value as a clip threshold" )
4294
+
4295
+ # method is self.le for upper bound and self.ge for lower bound
4296
+ if is_scalar (threshold ) and is_number (threshold ):
4297
+ if method .__name__ == 'le' :
4298
+ return self ._clip_with_scalar (None , threshold , inplace = inplace )
4299
+ else :
4300
+ return self ._clip_with_scalar (threshold , None , inplace = inplace )
4301
+
4302
+ inplace = validate_bool_kwarg (inplace , 'inplace' )
4303
+
4304
+ subset = method (threshold , axis = axis ) | isnull (self )
4305
+
4306
+ # GH #15390
4307
+ if is_scalar (threshold ) or is_number (threshold ):
4308
+ return self .where (subset , threshold , axis = axis , inplace = inplace )
4309
+
4310
+ # For arry_like threshold, convet it to Series with corret index
4311
+ # `where` only takes
4312
+ try :
4313
+ if isinstance (subset , ABCSeries ):
4314
+ threshold = pd .Series (threshold , index = subset .index )
4315
+ elif axis == 0 :
4316
+ threshold = pd .Series (threshold , index = subset .index )
4317
+ else :
4318
+ threshold = pd .Series (threshold , index = subset .columns )
4319
+ finally :
4320
+ return self .where (subset , threshold , axis = axis , inplace = inplace )
4321
+
4322
+
4290
4323
def clip (self , lower = None , upper = None , axis = None , inplace = False ,
4291
4324
* args , ** kwargs ):
4292
4325
"""
@@ -4389,16 +4422,8 @@ def clip_upper(self, threshold, axis=None, inplace=False):
4389
4422
-------
4390
4423
clipped : same type as input
4391
4424
"""
4392
- if np .any (isnull (threshold )):
4393
- raise ValueError ("Cannot use an NA value as a clip threshold" )
4394
-
4395
- if is_scalar (threshold ) and is_number (threshold ):
4396
- return self ._clip_with_scalar (None , threshold , inplace = inplace )
4397
-
4398
- inplace = validate_bool_kwarg (inplace , 'inplace' )
4399
-
4400
- subset = self .le (threshold , axis = axis ) | isnull (self )
4401
- return self .where (subset , threshold , axis = axis , inplace = inplace )
4425
+ return self ._clip_with_one_bound (threshold , method = self .le ,
4426
+ axis = axis , inplace = inplace )
4402
4427
4403
4428
def clip_lower (self , threshold , axis = None , inplace = False ):
4404
4429
"""
@@ -4421,16 +4446,8 @@ def clip_lower(self, threshold, axis=None, inplace=False):
4421
4446
-------
4422
4447
clipped : same type as input
4423
4448
"""
4424
- if np .any (isnull (threshold )):
4425
- raise ValueError ("Cannot use an NA value as a clip threshold" )
4426
-
4427
- if is_scalar (threshold ) and is_number (threshold ):
4428
- return self ._clip_with_scalar (threshold , None , inplace = inplace )
4429
-
4430
- inplace = validate_bool_kwarg (inplace , 'inplace' )
4431
-
4432
- subset = self .ge (threshold , axis = axis ) | isnull (self )
4433
- return self .where (subset , threshold , axis = axis , inplace = inplace )
4449
+ return self ._clip_with_one_bound (threshold , method = self .ge ,
4450
+ axis = axis , inplace = inplace )
4434
4451
4435
4452
def groupby (self , by = None , axis = 0 , level = None , as_index = True , sort = True ,
4436
4453
group_keys = True , squeeze = False , ** kwargs ):
0 commit comments