Skip to content

Commit 91c2874

Browse files
yl2526jreback
authored andcommitted
fix conflict from clip inplace
1 parent 9462379 commit 91c2874

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-20
lines changed

pandas/core/generic.py

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4413,6 +4413,39 @@ def _clip_with_scalar(self, lower, upper, inplace=False):
44134413
else:
44144414
return result
44154415

4416+
def _clip_with_one_bound(self, threshold, method, axis, inplace):
4417+
4418+
if np.any(isnull(threshold)):
4419+
raise ValueError("Cannot use an NA value as a clip threshold")
4420+
4421+
# method is self.le for upper bound and self.ge for lower bound
4422+
if is_scalar(threshold) and is_number(threshold):
4423+
if method.__name__ == 'le':
4424+
return self._clip_with_scalar(None, threshold, inplace=inplace)
4425+
else:
4426+
return self._clip_with_scalar(threshold, None, inplace=inplace)
4427+
4428+
inplace = validate_bool_kwarg(inplace, 'inplace')
4429+
4430+
subset = method(threshold, axis=axis) | isnull(self)
4431+
4432+
# GH #15390
4433+
if is_scalar(threshold) or is_number(threshold):
4434+
return self.where(subset, threshold, axis=axis, inplace=inplace)
4435+
4436+
# For arry_like threshold, convet it to Series with corret index
4437+
# `where` only takes
4438+
try:
4439+
if isinstance(subset, ABCSeries):
4440+
threshold = pd.Series(threshold, index=subset.index)
4441+
elif axis == 0:
4442+
threshold = pd.Series(threshold, index=subset.index)
4443+
else:
4444+
threshold = pd.Series(threshold, index=subset.columns)
4445+
finally:
4446+
return self.where(subset, threshold, axis=axis, inplace=inplace)
4447+
4448+
44164449
def clip(self, lower=None, upper=None, axis=None, inplace=False,
44174450
*args, **kwargs):
44184451
"""
@@ -4515,16 +4548,8 @@ def clip_upper(self, threshold, axis=None, inplace=False):
45154548
-------
45164549
clipped : same type as input
45174550
"""
4518-
if np.any(isnull(threshold)):
4519-
raise ValueError("Cannot use an NA value as a clip threshold")
4520-
4521-
if is_scalar(threshold) and is_number(threshold):
4522-
return self._clip_with_scalar(None, threshold, inplace=inplace)
4523-
4524-
inplace = validate_bool_kwarg(inplace, 'inplace')
4525-
4526-
subset = self.le(threshold, axis=axis) | isnull(self)
4527-
return self.where(subset, threshold, axis=axis, inplace=inplace)
4551+
return self._clip_with_one_bound(threshold, method=self.le,
4552+
axis=axis, inplace=inplace)
45284553

45294554
def clip_lower(self, threshold, axis=None, inplace=False):
45304555
"""
@@ -4547,16 +4572,8 @@ def clip_lower(self, threshold, axis=None, inplace=False):
45474572
-------
45484573
clipped : same type as input
45494574
"""
4550-
if np.any(isnull(threshold)):
4551-
raise ValueError("Cannot use an NA value as a clip threshold")
4552-
4553-
if is_scalar(threshold) and is_number(threshold):
4554-
return self._clip_with_scalar(threshold, None, inplace=inplace)
4555-
4556-
inplace = validate_bool_kwarg(inplace, 'inplace')
4557-
4558-
subset = self.ge(threshold, axis=axis) | isnull(self)
4559-
return self.where(subset, threshold, axis=axis, inplace=inplace)
4575+
return self._clip_with_one_bound(threshold, method=self.ge,
4576+
axis=axis, inplace=inplace)
45604577

45614578
def groupby(self, by=None, axis=0, level=None, as_index=True, sort=True,
45624579
group_keys=True, squeeze=False, **kwargs):

pandas/tests/frame/test_analytics.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,25 @@ def test_clip_against_series(self, inplace):
18921892

18931893
tm.assert_series_equal(clipped_df.loc[mask, i], df.loc[mask, i])
18941894

1895+
@pytest.mark.parametrize("inplace", [True, False])
1896+
def test_clip_against_list(self, inplace):
1897+
# GH #15390
1898+
original = self.simple
1899+
1900+
result = original.clip(lower=[2, 3, 4], upper=[5, 6, 7],
1901+
axis=1, inplace=inplace)
1902+
1903+
arr = np.array([[2., 3., 4.],
1904+
[4., 5., 6.],
1905+
[5., 6., 7.]])
1906+
expected = pd.DataFrame(arr,
1907+
columns=original.columns,
1908+
index=original.index)
1909+
if inplace:
1910+
tm.assert_frame_equal(original, expected, check_exact=True)
1911+
else:
1912+
tm.assert_frame_equal(result, expected, check_exact=True)
1913+
18951914
def test_clip_against_frame(self):
18961915
df = DataFrame(np.random.randn(1000, 2))
18971916
lb = DataFrame(np.random.randn(1000, 2))

pandas/tests/series/test_analytics.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,18 @@ def test_clip_against_series(self):
10151015
assert_series_equal(s.clip(lower, upper), Series([1.0, 2.0, 3.5]))
10161016
assert_series_equal(s.clip(1.5, upper), Series([1.5, 1.5, 3.5]))
10171017

1018+
@pytest.mark.parametrize("inplace", [True, False])
1019+
def test_clip_against_list(self, inplace):
1020+
# GH #15390
1021+
original = pd.Series([5, 6, 7])
1022+
result = original.clip(upper=[1, 2, 3], inplace=inplace)
1023+
expected = pd.Series([1, 2, 3])
1024+
1025+
if inplace:
1026+
tm.assert_series_equal(original, expected, check_exact=True)
1027+
else:
1028+
tm.assert_series_equal(result, expected, check_exact=True)
1029+
10181030
def test_clip_with_datetimes(self):
10191031

10201032
# GH 11838

0 commit comments

Comments
 (0)