Skip to content

Commit b50163f

Browse files
committed
fix transform to series logic
1 parent 3ecfba1 commit b50163f

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
@@ -4415,6 +4416,8 @@ def _clip_with_scalar(self, lower, upper, inplace=False):
44154416
def _clip_with_one_bound(self, threshold, method, axis, inplace):
44164417

44174418
inplace = validate_bool_kwarg(inplace, 'inplace')
4419+
if axis is not None:
4420+
axis = self._get_axis_number(axis)
44184421

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

44304433
# GH #15390
4434+
# In order for where method to work, the threshold must
4435+
# be transformed to NDFrame from other array like structure.
44314436
if (not isinstance(threshold, ABCSeries)) and is_list_like(threshold):
4432-
if isinstance(self, ABCSeries) or axis == 0:
4437+
if isinstance(self, ABCSeries):
44334438
threshold = pd.Series(threshold, index=self.index)
4434-
elif axis == 1:
4435-
threshold = pd.Series(threshold, index=self.columns)
4439+
else:
4440+
threshold = _align_method_FRAME(self, np.asarray(threshold),
4441+
axis)
44364442
return self.where(subset, threshold, axis=axis, inplace=inplace)
44374443

44384444
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)