52
52
from pandas .compat .numpy import function as nv
53
53
from pandas .compat import (map , zip , lzip , lrange , string_types ,
54
54
isidentifier , set_function_name , cPickle as pkl )
55
+ from pandas .core .ops import _align_method_FRAME
55
56
import pandas .core .nanops as nanops
56
57
from pandas .util ._decorators import Appender , Substitution , deprecate_kwarg
57
58
from pandas .util ._validators import validate_bool_kwarg
@@ -4413,6 +4414,34 @@ def _clip_with_scalar(self, lower, upper, inplace=False):
4413
4414
else :
4414
4415
return result
4415
4416
4417
+ def _clip_with_one_bound (self , threshold , method , axis , inplace ):
4418
+
4419
+ inplace = validate_bool_kwarg (inplace , 'inplace' )
4420
+ if axis is not None :
4421
+ axis = self ._get_axis_number (axis )
4422
+
4423
+ if np .any (isnull (threshold )):
4424
+ raise ValueError ("Cannot use an NA value as a clip threshold" )
4425
+
4426
+ # method is self.le for upper bound and self.ge for lower bound
4427
+ if is_scalar (threshold ) and is_number (threshold ):
4428
+ if method .__name__ == 'le' :
4429
+ return self ._clip_with_scalar (None , threshold , inplace = inplace )
4430
+ return self ._clip_with_scalar (threshold , None , inplace = inplace )
4431
+
4432
+ subset = method (threshold , axis = axis ) | isnull (self )
4433
+
4434
+ # GH #15390
4435
+ # In order for where method to work, the threshold must
4436
+ # be transformed to NDFrame from other array like structure.
4437
+ if (not isinstance (threshold , ABCSeries )) and is_list_like (threshold ):
4438
+ if isinstance (self , ABCSeries ):
4439
+ threshold = pd .Series (threshold , index = self .index )
4440
+ else :
4441
+ threshold = _align_method_FRAME (self , np .asarray (threshold ),
4442
+ axis )
4443
+ return self .where (subset , threshold , axis = axis , inplace = inplace )
4444
+
4416
4445
def clip (self , lower = None , upper = None , axis = None , inplace = False ,
4417
4446
* args , ** kwargs ):
4418
4447
"""
@@ -4515,16 +4544,8 @@ def clip_upper(self, threshold, axis=None, inplace=False):
4515
4544
-------
4516
4545
clipped : same type as input
4517
4546
"""
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 )
4547
+ return self ._clip_with_one_bound (threshold , method = self .le ,
4548
+ axis = axis , inplace = inplace )
4528
4549
4529
4550
def clip_lower (self , threshold , axis = None , inplace = False ):
4530
4551
"""
@@ -4547,16 +4568,8 @@ def clip_lower(self, threshold, axis=None, inplace=False):
4547
4568
-------
4548
4569
clipped : same type as input
4549
4570
"""
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 )
4571
+ return self ._clip_with_one_bound (threshold , method = self .ge ,
4572
+ axis = axis , inplace = inplace )
4560
4573
4561
4574
def groupby (self , by = None , axis = 0 , level = None , as_index = True , sort = True ,
4562
4575
group_keys = True , squeeze = False , ** kwargs ):
0 commit comments