Skip to content

Commit 63b5158

Browse files
committed
Add an 'axis' parameter to Series flexible comparisons. Use it to simplify clip_lower and clip_upper
1 parent 1a5c813 commit 63b5158

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

pandas/core/generic.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -2853,11 +2853,8 @@ def clip_upper(self, threshold, axis=None):
28532853
if np.any(isnull(threshold)):
28542854
raise ValueError("Cannot use an NA value as a clip threshold")
28552855

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)
2856+
subset = self.le(threshold, axis=axis) | isnull(self)
2857+
return self.where(subset, threshold, axis=axis)
28612858

28622859

28632860
def clip_lower(self, threshold, axis=None):
@@ -2881,11 +2878,8 @@ def clip_lower(self, threshold, axis=None):
28812878
if np.any(isnull(threshold)):
28822879
raise ValueError("Cannot use an NA value as a clip threshold")
28832880

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)
2881+
subset = self.ge(threshold, axis=axis) | isnull(self)
2882+
return self.where(subset, threshold, axis=axis)
28892883

28902884

28912885
def groupby(self, by=None, axis=0, level=None, as_index=True, sort=True,

pandas/core/ops.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,11 @@ def na_op(x, y):
571571

572572
return result
573573

574-
def wrapper(self, other):
574+
def wrapper(self, other, axis=None):
575+
# Validate the axis parameter
576+
if axis is not None:
577+
self._get_axis_number(axis)
578+
575579
if isinstance(other, pd.Series):
576580
name = _maybe_match_name(self, other)
577581
if len(self) != len(other):

0 commit comments

Comments
 (0)