Skip to content

Commit f66a108

Browse files
yl2526jreback
authored andcommitted
fix clip by casting to Series
1 parent e54ca22 commit f66a108

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

pandas/core/generic.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4430,7 +4430,10 @@ def _clip_with_one_bound(self, threshold, method, axis, inplace):
44304430

44314431
# GH #15390
44324432
if (not isinstance(threshold, ABCSeries)) and is_list_like(threshold):
4433-
threshold = np.asarray(threshold)
4433+
if isinstance(self, ABCSeries) or axis == 0:
4434+
threshold = pd.Series(threshold, index=self.index)
4435+
elif axis == 1:
4436+
threshold = pd.Series(threshold, index=self.columns)
44344437
return self.where(subset, threshold, axis=axis, inplace=inplace)
44354438

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

pandas/tests/frame/test_analytics.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,17 +1893,19 @@ def test_clip_against_series(self, inplace):
18931893
tm.assert_series_equal(clipped_df.loc[mask, i], df.loc[mask, i])
18941894

18951895
@pytest.mark.parametrize("inplace", [True, False])
1896-
def test_clip_against_list(self, inplace):
1896+
@pytest.mark.parametrize("lower", [[2, 3, 4], np.asarray([2, 3, 4])])
1897+
@pytest.mark.parametrize("axis,res", [
1898+
(0, [[2., 2., 3.], [4., 5., 6.], [7., 7., 7.]]),
1899+
(1, [[2., 3., 4.], [4., 5., 6.], [5., 6., 7.]])
1900+
])
1901+
def test_clip_against_list_like(self, inplace, lower, axis, res):
18971902
# GH #15390
18981903
original = self.simple.copy(deep=True)
18991904

1900-
result = original.clip(lower=[2, 3, 4], upper=[5, 6, 7],
1901-
axis=1, inplace=inplace)
1905+
result = original.clip(lower=lower, upper=[5, 6, 7],
1906+
axis=axis, inplace=inplace)
19021907

1903-
arr = np.array([[2., 3., 4.],
1904-
[4., 5., 6.],
1905-
[5., 6., 7.]])
1906-
expected = pd.DataFrame(arr,
1908+
expected = pd.DataFrame(res,
19071909
columns=original.columns,
19081910
index=original.index)
19091911
if inplace:

pandas/tests/series/test_analytics.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,10 +1016,11 @@ def test_clip_against_series(self):
10161016
assert_series_equal(s.clip(1.5, upper), Series([1.5, 1.5, 3.5]))
10171017

10181018
@pytest.mark.parametrize("inplace", [True, False])
1019-
def test_clip_against_list(self, inplace):
1019+
@pytest.mark.parametrize("upper", [[1, 2, 3], np.asarray([1, 2, 3])])
1020+
def test_clip_against_list_like(self, inplace, upper):
10201021
# GH #15390
10211022
original = pd.Series([5, 6, 7])
1022-
result = original.clip(upper=[1, 2, 3], inplace=inplace)
1023+
result = original.clip(upper=upper, inplace=inplace)
10231024
expected = pd.Series([1, 2, 3])
10241025

10251026
if inplace:

0 commit comments

Comments
 (0)