From 287a4005ee42e91d5bde455ce2720fd5c756e052 Mon Sep 17 00:00:00 2001 From: makbigc Date: Sun, 15 Jul 2018 23:08:40 +0800 Subject: [PATCH 1/6] BUG:(GH19992) --- doc/source/whatsnew/v0.24.0.txt | 1 + pandas/core/generic.py | 6 ++++-- pandas/tests/frame/test_analytics.py | 15 +++++++++++---- pandas/tests/series/test_analytics.py | 8 ++++++-- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 9e3f7ec73f852..36bdc4e2bf28e 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -440,6 +440,7 @@ Missing - Bug in :func:`DataFrame.fillna` where a ``ValueError`` would raise when one column contained a ``datetime64[ns, tz]`` dtype (:issue:`15522`) - Bug in :func:`Series.hasnans` that could be incorrectly cached and return incorrect answers if null elements are introduced after an initial call (:issue:`19700`) +- Bug in :func:`Series.clip` and `DataFrame.clip` cannot accept list-like threshold containing nan (:issue:`19992`) MultiIndex ^^^^^^^^^^ diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8da678e0adec0..c3600a63c06fe 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6520,9 +6520,11 @@ def clip(self, lower=None, upper=None, axis=None, inplace=False, # GH 17276 # numpy doesn't like NaN as a clip value # so ignore - if np.any(pd.isnull(lower)): + # GH 19992 + # numpy doesn't drop a list-like bound containing NaN + if not is_list_like(lower) and np.any(pd.isnull(lower)): lower = None - if np.any(pd.isnull(upper)): + if not is_list_like(upper) and np.any(pd.isnull(upper)): upper = None # GH 2747 (arguments were reversed) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index c0e9b89c1877f..da41432f45d16 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -2237,13 +2237,20 @@ def test_clip_with_na_args(self): """Should process np.nan argument as None """ # GH # 17276 tm.assert_frame_equal(self.frame.clip(np.nan), self.frame) - tm.assert_frame_equal(self.frame.clip(upper=[1, 2, np.nan]), - self.frame) - tm.assert_frame_equal(self.frame.clip(lower=[1, np.nan, 3]), - self.frame) tm.assert_frame_equal(self.frame.clip(upper=np.nan, lower=np.nan), self.frame) + # GH #19992 + df = DataFrame({'col_0': [1, 2, 3], 'col_1': [4, 5, 6], + 'col_2': [7, 8, 9]}) + df1 = DataFrame({'col_0': [4, 5, np.nan], 'col_1': [4, 5, np.nan], + 'col_2': [7, 8, np.nan]}) + df2 = DataFrame({'col_0': [4, 4, 4], 'col_1': [5, 5, 6], + 'col_2': [np.nan, np.nan, np.nan]}) + + tm.assert_frame_equal(df.clip(lower=[4, 5, np.nan], axis=0), df1) + tm.assert_frame_equal(df.clip(lower=[4, 5, np.nan], axis=1), df2) + # Matrix-like def test_dot(self): a = DataFrame(np.random.randn(3, 4), index=['a', 'b', 'c'], diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index fd14118bd833f..612b00059b062 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -1080,11 +1080,15 @@ def test_clip_with_na_args(self): s = Series([1, 2, 3]) assert_series_equal(s.clip(np.nan), Series([1, 2, 3])) - assert_series_equal(s.clip(upper=[1, 1, np.nan]), Series([1, 2, 3])) - assert_series_equal(s.clip(lower=[1, np.nan, 1]), Series([1, 2, 3])) assert_series_equal(s.clip(upper=np.nan, lower=np.nan), Series([1, 2, 3])) + # GH #19992 + assert_series_equal(s.clip(lower=[0, 4, np.nan]), + Series([1, 4, np.nan])) + assert_series_equal(s.clip(upper=[1, np.nan, 1]), + Series([1, np.nan, 1])) + def test_clip_against_series(self): # GH #6966 From c5dfbfcc13c0864c3a8a6c7134557e0295328695 Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Mon, 16 Jul 2018 21:52:12 +0800 Subject: [PATCH 2/6] Update v0.24.0.txt --- doc/source/whatsnew/v0.24.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 36bdc4e2bf28e..35ce7d4ecadb0 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -440,7 +440,7 @@ Missing - Bug in :func:`DataFrame.fillna` where a ``ValueError`` would raise when one column contained a ``datetime64[ns, tz]`` dtype (:issue:`15522`) - Bug in :func:`Series.hasnans` that could be incorrectly cached and return incorrect answers if null elements are introduced after an initial call (:issue:`19700`) -- Bug in :func:`Series.clip` and `DataFrame.clip` cannot accept list-like threshold containing nan (:issue:`19992`) +- Bug in :func:`Series.clip` and :func:`DataFrame.clip` cannot accept list-like threshold containing ``NaN`` (:issue:`19992`) MultiIndex ^^^^^^^^^^ From 5c9f634842fcd473bfc496a2b80377381bdfb042 Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Mon, 16 Jul 2018 22:00:05 +0800 Subject: [PATCH 3/6] Update test_analytics.py --- pandas/tests/frame/test_analytics.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index da41432f45d16..d85e4e4d3087f 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -2243,13 +2243,16 @@ def test_clip_with_na_args(self): # GH #19992 df = DataFrame({'col_0': [1, 2, 3], 'col_1': [4, 5, 6], 'col_2': [7, 8, 9]}) - df1 = DataFrame({'col_0': [4, 5, np.nan], 'col_1': [4, 5, np.nan], - 'col_2': [7, 8, np.nan]}) - df2 = DataFrame({'col_0': [4, 4, 4], 'col_1': [5, 5, 6], - 'col_2': [np.nan, np.nan, np.nan]}) - tm.assert_frame_equal(df.clip(lower=[4, 5, np.nan], axis=0), df1) - tm.assert_frame_equal(df.clip(lower=[4, 5, np.nan], axis=1), df2) + result = df.clip(lower=[4, 5, np.nan], axis=0) + expected = DataFrame({'col_0': [4, 5, np.nan], 'col_1': [4, 5, np.nan], + 'col_2': [7, 8, np.nan]}) + tm.assert_frame_equal(result, expected) + + result = df.clip(lower=[4, 5, np.nan], axis=1) + expected = DataFrame({'col_0': [4, 4, 4], 'col_1': [5, 5, 6], + 'col_2': [np.nan, np.nan, np.nan]}) + tm.assert_frame_equal(result, expected) # Matrix-like def test_dot(self): From 4eea6245376467df4ea197d39e900a991b60b66c Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Tue, 17 Jul 2018 22:17:28 +0800 Subject: [PATCH 4/6] Update v0.24.0.txt --- doc/source/whatsnew/v0.24.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 35ce7d4ecadb0..636369b836eae 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -440,7 +440,7 @@ Missing - Bug in :func:`DataFrame.fillna` where a ``ValueError`` would raise when one column contained a ``datetime64[ns, tz]`` dtype (:issue:`15522`) - Bug in :func:`Series.hasnans` that could be incorrectly cached and return incorrect answers if null elements are introduced after an initial call (:issue:`19700`) -- Bug in :func:`Series.clip` and :func:`DataFrame.clip` cannot accept list-like threshold containing ``NaN`` (:issue:`19992`) + MultiIndex ^^^^^^^^^^ From 2f5055569a15a10d27d77a2ff959c2b814a0d624 Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Tue, 17 Jul 2018 22:23:44 +0800 Subject: [PATCH 5/6] Update v0.23.4.txt --- doc/source/whatsnew/v0.23.4.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/source/whatsnew/v0.23.4.txt b/doc/source/whatsnew/v0.23.4.txt index a88c22e3d01f7..5e19ab491647d 100644 --- a/doc/source/whatsnew/v0.23.4.txt +++ b/doc/source/whatsnew/v0.23.4.txt @@ -58,3 +58,7 @@ Bug Fixes - - + +**Missing** + +- Bug in :func:`Series.clip` and :func:`DataFrame.clip` cannot accept list-like threshold containing ``NaN`` (:issue:`19992`) From 0ef2e4d8d3a703f421f563b6a40f9748ab124825 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Wed, 18 Jul 2018 06:22:01 -0400 Subject: [PATCH 6/6] remove 0.24.0 ref --- doc/source/whatsnew/v0.24.0.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 9e5a719d16330..b015495b095b6 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -446,7 +446,6 @@ Missing - Bug in :func:`DataFrame.fillna` where a ``ValueError`` would raise when one column contained a ``datetime64[ns, tz]`` dtype (:issue:`15522`) - Bug in :func:`Series.hasnans` that could be incorrectly cached and return incorrect answers if null elements are introduced after an initial call (:issue:`19700`) - MultiIndex ^^^^^^^^^^