Skip to content

Commit 3cb64bd

Browse files
makbigcjreback
authored andcommitted
BUG:Clip with a list-like threshold with a nan is broken (GH19992) (#21921)
1 parent 1ce2473 commit 3cb64bd

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

doc/source/whatsnew/v0.23.4.txt

+4
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,7 @@ Bug Fixes
6262

6363
-
6464
-
65+
66+
**Missing**
67+
68+
- Bug in :func:`Series.clip` and :func:`DataFrame.clip` cannot accept list-like threshold containing ``NaN`` (:issue:`19992`)

pandas/core/generic.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -6520,9 +6520,11 @@ def clip(self, lower=None, upper=None, axis=None, inplace=False,
65206520
# GH 17276
65216521
# numpy doesn't like NaN as a clip value
65226522
# so ignore
6523-
if np.any(pd.isnull(lower)):
6523+
# GH 19992
6524+
# numpy doesn't drop a list-like bound containing NaN
6525+
if not is_list_like(lower) and np.any(pd.isnull(lower)):
65246526
lower = None
6525-
if np.any(pd.isnull(upper)):
6527+
if not is_list_like(upper) and np.any(pd.isnull(upper)):
65266528
upper = None
65276529

65286530
# GH 2747 (arguments were reversed)

pandas/tests/frame/test_analytics.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -1859,13 +1859,23 @@ def test_clip_with_na_args(self):
18591859
"""Should process np.nan argument as None """
18601860
# GH # 17276
18611861
tm.assert_frame_equal(self.frame.clip(np.nan), self.frame)
1862-
tm.assert_frame_equal(self.frame.clip(upper=[1, 2, np.nan]),
1863-
self.frame)
1864-
tm.assert_frame_equal(self.frame.clip(lower=[1, np.nan, 3]),
1865-
self.frame)
18661862
tm.assert_frame_equal(self.frame.clip(upper=np.nan, lower=np.nan),
18671863
self.frame)
18681864

1865+
# GH #19992
1866+
df = DataFrame({'col_0': [1, 2, 3], 'col_1': [4, 5, 6],
1867+
'col_2': [7, 8, 9]})
1868+
1869+
result = df.clip(lower=[4, 5, np.nan], axis=0)
1870+
expected = DataFrame({'col_0': [4, 5, np.nan], 'col_1': [4, 5, np.nan],
1871+
'col_2': [7, 8, np.nan]})
1872+
tm.assert_frame_equal(result, expected)
1873+
1874+
result = df.clip(lower=[4, 5, np.nan], axis=1)
1875+
expected = DataFrame({'col_0': [4, 4, 4], 'col_1': [5, 5, 6],
1876+
'col_2': [np.nan, np.nan, np.nan]})
1877+
tm.assert_frame_equal(result, expected)
1878+
18691879
# Matrix-like
18701880
def test_dot(self):
18711881
a = DataFrame(np.random.randn(3, 4), index=['a', 'b', 'c'],

pandas/tests/series/test_analytics.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -942,11 +942,15 @@ def test_clip_with_na_args(self):
942942
s = Series([1, 2, 3])
943943

944944
assert_series_equal(s.clip(np.nan), Series([1, 2, 3]))
945-
assert_series_equal(s.clip(upper=[1, 1, np.nan]), Series([1, 2, 3]))
946-
assert_series_equal(s.clip(lower=[1, np.nan, 1]), Series([1, 2, 3]))
947945
assert_series_equal(s.clip(upper=np.nan, lower=np.nan),
948946
Series([1, 2, 3]))
949947

948+
# GH #19992
949+
assert_series_equal(s.clip(lower=[0, 4, np.nan]),
950+
Series([1, 4, np.nan]))
951+
assert_series_equal(s.clip(upper=[1, np.nan, 1]),
952+
Series([1, np.nan, 1]))
953+
950954
def test_clip_against_series(self):
951955
# GH #6966
952956

0 commit comments

Comments
 (0)