Skip to content

Commit eac8b61

Browse files
committed
Add axis parameter to clip, clip_lower, and clip_upper. Fix the broken frame-clipping test.
1 parent a07ccc5 commit eac8b61

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

pandas/core/generic.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -2801,14 +2801,16 @@ def notnull(self):
28012801
"""
28022802
return notnull(self).__finalize__(self)
28032803

2804-
def clip(self, lower=None, upper=None, out=None):
2804+
def clip(self, lower=None, upper=None, out=None, axis=None):
28052805
"""
28062806
Trim values at input threshold(s)
28072807
28082808
Parameters
28092809
----------
28102810
lower : float or array_like, default None
28112811
upper : float or array_like, default None
2812+
axis : int, optional
2813+
Align object with lower and upper along the given axis.
28122814
28132815
Returns
28142816
-------
@@ -2824,19 +2826,21 @@ def clip(self, lower=None, upper=None, out=None):
28242826

28252827
result = self
28262828
if lower is not None:
2827-
result = result.clip_lower(lower)
2829+
result = result.clip_lower(lower, axis)
28282830
if upper is not None:
2829-
result = result.clip_upper(upper)
2831+
result = result.clip_upper(upper, axis)
28302832

28312833
return result
28322834

2833-
def clip_upper(self, threshold):
2835+
def clip_upper(self, threshold, axis=None):
28342836
"""
28352837
Return copy of input with values above given value(s) truncated
28362838
28372839
Parameters
28382840
----------
28392841
threshold : float or array_like
2842+
axis : int, optional
2843+
Align object with threshold along the given axis.
28402844
28412845
See also
28422846
--------
@@ -2849,15 +2853,22 @@ def clip_upper(self, threshold):
28492853
if np.any(isnull(threshold)):
28502854
raise ValueError("Cannot use an NA value as a clip threshold")
28512855

2852-
return self.where(self.le(threshold, axis=0) | isnull(self), threshold, axis=0)
2856+
if axis is None:
2857+
return self.where(self.le(threshold) | isnull(self), threshold)
2858+
else:
2859+
subset = self.le(threshold, axis=axis) | isnull(self)
2860+
return self.where(subset, threshold, axis=axis)
2861+
28532862

2854-
def clip_lower(self, threshold):
2863+
def clip_lower(self, threshold, axis=None):
28552864
"""
28562865
Return copy of the input with values below given value(s) truncated
28572866
28582867
Parameters
28592868
----------
28602869
threshold : float or array_like
2870+
axis : int, optional
2871+
Align object with threshold along the given axis.
28612872
28622873
See also
28632874
--------
@@ -2870,7 +2881,12 @@ def clip_lower(self, threshold):
28702881
if np.any(isnull(threshold)):
28712882
raise ValueError("Cannot use an NA value as a clip threshold")
28722883

2873-
return self.where(self.ge(threshold, axis=0) | isnull(self), threshold, axis=0)
2884+
if axis is None:
2885+
return self.where(self.ge(threshold) | isnull(self), threshold)
2886+
else:
2887+
subset = self.ge(threshold, axis=axis) | isnull(self)
2888+
return self.where(subset, threshold, axis=axis)
2889+
28742890

28752891
def groupby(self, by=None, axis=0, level=None, as_index=True, sort=True,
28762892
group_keys=True, squeeze=False):

pandas/tests/test_frame.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -11258,15 +11258,12 @@ def test_clip_against_series(self):
1125811258
lb = Series(np.random.randn(1000))
1125911259
ub = lb + 1
1126011260

11261-
clipped_df = df.clip(lb, ub)
11261+
clipped_df = df.clip(lb, ub, axis=0)
1126211262

1126311263
for i in xrange(2):
1126411264
lb_mask = df.iloc[:, i] <= lb
1126511265
ub_mask = df.iloc[:, i] >= ub
1126611266
mask = ~lb_mask & ~ub_mask
11267-
#self.assertTrue((clipped_df.loc[lb_mask, i] == lb.loc[lb_mask]).all())
11268-
#self.assertTrue((clipped_df.loc[ub_mask, i] == ub.loc[ub_mask]).all())
11269-
#self.assertTrue((clipped_df.loc[mask, i] == df.loc[mask, i]).all())
1127011267

1127111268
assert_series_equal(clipped_df.loc[lb_mask, i], lb[lb_mask])
1127211269
assert_series_equal(clipped_df.loc[ub_mask, i], ub[ub_mask])

0 commit comments

Comments
 (0)