Skip to content

Commit c859bb7

Browse files
yl2526jreback
authored andcommitted
fix transform to series logic
1 parent f66a108 commit c859bb7

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

pandas/core/generic.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
from pandas.compat.numpy import function as nv
5353
from pandas.compat import (map, zip, lzip, lrange, string_types,
5454
isidentifier, set_function_name, cPickle as pkl)
55+
from pandas.core.ops import _align_method_FRAME
5556
import pandas.core.nanops as nanops
5657
from pandas.util._decorators import Appender, Substitution, deprecate_kwarg
5758
from pandas.util._validators import validate_bool_kwarg
@@ -4416,6 +4417,8 @@ def _clip_with_scalar(self, lower, upper, inplace=False):
44164417
def _clip_with_one_bound(self, threshold, method, axis, inplace):
44174418

44184419
inplace = validate_bool_kwarg(inplace, 'inplace')
4420+
if axis is not None:
4421+
axis = self._get_axis_number(axis)
44194422

44204423
if np.any(isnull(threshold)):
44214424
raise ValueError("Cannot use an NA value as a clip threshold")
@@ -4429,11 +4432,14 @@ def _clip_with_one_bound(self, threshold, method, axis, inplace):
44294432
subset = method(threshold, axis=axis) | isnull(self)
44304433

44314434
# GH #15390
4435+
# In order for where method to work, the threshold must
4436+
# be transformed to NDFrame from other array like structure.
44324437
if (not isinstance(threshold, ABCSeries)) and is_list_like(threshold):
4433-
if isinstance(self, ABCSeries) or axis == 0:
4438+
if isinstance(self, ABCSeries):
44344439
threshold = pd.Series(threshold, index=self.index)
4435-
elif axis == 1:
4436-
threshold = pd.Series(threshold, index=self.columns)
4440+
else:
4441+
threshold = _align_method_FRAME(self, np.asarray(threshold),
4442+
axis)
44374443
return self.where(subset, threshold, axis=axis, inplace=inplace)
44384444

44394445
def clip(self, lower=None, upper=None, axis=None, inplace=False,

pandas/tests/frame/test_analytics.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,12 +1912,13 @@ def test_clip_against_list_like(self, inplace, lower, axis, res):
19121912
result = original
19131913
tm.assert_frame_equal(result, expected, check_exact=True)
19141914

1915-
def test_clip_against_frame(self):
1915+
@pytest.mark.parametrize("axis", [0, 1, None])
1916+
def test_clip_against_frame(self, axis):
19161917
df = DataFrame(np.random.randn(1000, 2))
19171918
lb = DataFrame(np.random.randn(1000, 2))
19181919
ub = lb + 1
19191920

1920-
clipped_df = df.clip(lb, ub)
1921+
clipped_df = df.clip(lb, ub, axis=axis)
19211922

19221923
lb_mask = df <= lb
19231924
ub_mask = df >= ub

0 commit comments

Comments
 (0)