@@ -4413,6 +4413,39 @@ def _clip_with_scalar(self, lower, upper, inplace=False):
4413
4413
else :
4414
4414
return result
4415
4415
4416
+ def _clip_with_one_bound (self , threshold , method , axis , inplace ):
4417
+
4418
+ if np .any (isnull (threshold )):
4419
+ raise ValueError ("Cannot use an NA value as a clip threshold" )
4420
+
4421
+ # method is self.le for upper bound and self.ge for lower bound
4422
+ if is_scalar (threshold ) and is_number (threshold ):
4423
+ if method .__name__ == 'le' :
4424
+ return self ._clip_with_scalar (None , threshold , inplace = inplace )
4425
+ else :
4426
+ return self ._clip_with_scalar (threshold , None , inplace = inplace )
4427
+
4428
+ inplace = validate_bool_kwarg (inplace , 'inplace' )
4429
+
4430
+ subset = method (threshold , axis = axis ) | isnull (self )
4431
+
4432
+ # GH #15390
4433
+ if is_scalar (threshold ) or is_number (threshold ):
4434
+ return self .where (subset , threshold , axis = axis , inplace = inplace )
4435
+
4436
+ # For arry_like threshold, convet it to Series with corret index
4437
+ # `where` only takes
4438
+ try :
4439
+ if isinstance (subset , ABCSeries ):
4440
+ threshold = pd .Series (threshold , index = subset .index )
4441
+ elif axis == 0 :
4442
+ threshold = pd .Series (threshold , index = subset .index )
4443
+ else :
4444
+ threshold = pd .Series (threshold , index = subset .columns )
4445
+ finally :
4446
+ return self .where (subset , threshold , axis = axis , inplace = inplace )
4447
+
4448
+
4416
4449
def clip (self , lower = None , upper = None , axis = None , inplace = False ,
4417
4450
* args , ** kwargs ):
4418
4451
"""
@@ -4515,16 +4548,8 @@ def clip_upper(self, threshold, axis=None, inplace=False):
4515
4548
-------
4516
4549
clipped : same type as input
4517
4550
"""
4518
- if np .any (isnull (threshold )):
4519
- raise ValueError ("Cannot use an NA value as a clip threshold" )
4520
-
4521
- if is_scalar (threshold ) and is_number (threshold ):
4522
- return self ._clip_with_scalar (None , threshold , inplace = inplace )
4523
-
4524
- inplace = validate_bool_kwarg (inplace , 'inplace' )
4525
-
4526
- subset = self .le (threshold , axis = axis ) | isnull (self )
4527
- return self .where (subset , threshold , axis = axis , inplace = inplace )
4551
+ return self ._clip_with_one_bound (threshold , method = self .le ,
4552
+ axis = axis , inplace = inplace )
4528
4553
4529
4554
def clip_lower (self , threshold , axis = None , inplace = False ):
4530
4555
"""
@@ -4547,16 +4572,8 @@ def clip_lower(self, threshold, axis=None, inplace=False):
4547
4572
-------
4548
4573
clipped : same type as input
4549
4574
"""
4550
- if np .any (isnull (threshold )):
4551
- raise ValueError ("Cannot use an NA value as a clip threshold" )
4552
-
4553
- if is_scalar (threshold ) and is_number (threshold ):
4554
- return self ._clip_with_scalar (threshold , None , inplace = inplace )
4555
-
4556
- inplace = validate_bool_kwarg (inplace , 'inplace' )
4557
-
4558
- subset = self .ge (threshold , axis = axis ) | isnull (self )
4559
- return self .where (subset , threshold , axis = axis , inplace = inplace )
4575
+ return self ._clip_with_one_bound (threshold , method = self .ge ,
4576
+ axis = axis , inplace = inplace )
4560
4577
4561
4578
def groupby (self , by = None , axis = 0 , level = None , as_index = True , sort = True ,
4562
4579
group_keys = True , squeeze = False , ** kwargs ):
0 commit comments