Skip to content

Commit 5c83562

Browse files
makbigcMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #21921: BUG:Clip with a list-like threshold with a nan is broken (GH19992)
1 parent b7a2cd4 commit 5c83562

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
@@ -58,3 +58,7 @@ Bug Fixes
5858

5959
-
6060
-
61+
62+
**Missing**
63+
64+
- 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
@@ -6433,9 +6433,11 @@ def clip(self, lower=None, upper=None, axis=None, inplace=False,
64336433
# GH 17276
64346434
# numpy doesn't like NaN as a clip value
64356435
# so ignore
6436-
if np.any(pd.isnull(lower)):
6436+
# GH 19992
6437+
# numpy doesn't drop a list-like bound containing NaN
6438+
if not is_list_like(lower) and np.any(pd.isnull(lower)):
64376439
lower = None
6438-
if np.any(pd.isnull(upper)):
6440+
if not is_list_like(upper) and np.any(pd.isnull(upper)):
64396441
upper = None
64406442

64416443
# GH 2747 (arguments were reversed)

pandas/tests/frame/test_analytics.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -2195,13 +2195,23 @@ def test_clip_with_na_args(self):
21952195
"""Should process np.nan argument as None """
21962196
# GH # 17276
21972197
tm.assert_frame_equal(self.frame.clip(np.nan), self.frame)
2198-
tm.assert_frame_equal(self.frame.clip(upper=[1, 2, np.nan]),
2199-
self.frame)
2200-
tm.assert_frame_equal(self.frame.clip(lower=[1, np.nan, 3]),
2201-
self.frame)
22022198
tm.assert_frame_equal(self.frame.clip(upper=np.nan, lower=np.nan),
22032199
self.frame)
22042200

2201+
# GH #19992
2202+
df = DataFrame({'col_0': [1, 2, 3], 'col_1': [4, 5, 6],
2203+
'col_2': [7, 8, 9]})
2204+
2205+
result = df.clip(lower=[4, 5, np.nan], axis=0)
2206+
expected = DataFrame({'col_0': [4, 5, np.nan], 'col_1': [4, 5, np.nan],
2207+
'col_2': [7, 8, np.nan]})
2208+
tm.assert_frame_equal(result, expected)
2209+
2210+
result = df.clip(lower=[4, 5, np.nan], axis=1)
2211+
expected = DataFrame({'col_0': [4, 4, 4], 'col_1': [5, 5, 6],
2212+
'col_2': [np.nan, np.nan, np.nan]})
2213+
tm.assert_frame_equal(result, expected)
2214+
22052215
# Matrix-like
22062216
def test_dot(self):
22072217
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
@@ -1140,11 +1140,15 @@ def test_clip_with_na_args(self):
11401140
s = Series([1, 2, 3])
11411141

11421142
assert_series_equal(s.clip(np.nan), Series([1, 2, 3]))
1143-
assert_series_equal(s.clip(upper=[1, 1, np.nan]), Series([1, 2, 3]))
1144-
assert_series_equal(s.clip(lower=[1, np.nan, 1]), Series([1, 2, 3]))
11451143
assert_series_equal(s.clip(upper=np.nan, lower=np.nan),
11461144
Series([1, 2, 3]))
11471145

1146+
# GH #19992
1147+
assert_series_equal(s.clip(lower=[0, 4, np.nan]),
1148+
Series([1, 4, np.nan]))
1149+
assert_series_equal(s.clip(upper=[1, np.nan, 1]),
1150+
Series([1, np.nan, 1]))
1151+
11481152
def test_clip_against_series(self):
11491153
# GH #6966
11501154

0 commit comments

Comments
 (0)