From c78ee39edaf428ced39e5792464cf5b2909f257d Mon Sep 17 00:00:00 2001 From: Andrew Archer Date: Mon, 22 May 2017 12:26:20 -0700 Subject: [PATCH] BUG: Silence numpy warnings when broadcasting DataFrame to Series with comparison ops (GH16378, GH16306) TST: test for fix of GH16378, GH16306 --- doc/source/whatsnew/v0.20.2.txt | 1 + pandas/core/ops.py | 3 ++- pandas/tests/frame/test_analytics.py | 13 +++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.20.2.txt b/doc/source/whatsnew/v0.20.2.txt index e0857019d2fd4..04a63f98e0f7a 100644 --- a/doc/source/whatsnew/v0.20.2.txt +++ b/doc/source/whatsnew/v0.20.2.txt @@ -42,6 +42,7 @@ Conversion ^^^^^^^^^^ - Bug in ``pd.to_numeric()`` in which empty data inputs were causing Python to crash (:issue:`16302`) +- Silence numpy warnings when broadcasting DataFrame to Series with comparison ops (:issue:`16378`, :issue:`16306`) Indexing diff --git a/pandas/core/ops.py b/pandas/core/ops.py index e7cfbdb0fc9c6..55473ec8d7cad 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -1250,7 +1250,8 @@ def _flex_comp_method_FRAME(op, name, str_rep=None, default_axis='columns', masker=False): def na_op(x, y): try: - result = op(x, y) + with np.errstate(invalid='ignore'): + result = op(x, y) except TypeError: xrav = x.ravel() result = np.empty(x.size, dtype=bool) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index fa9823bf000a2..aa3604b86f3cb 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -2073,3 +2073,16 @@ def test_n_duplicate_index(self, df_duplicates, n, order): result = df.nlargest(n, order) expected = df.sort_values(order, ascending=False).head(n) tm.assert_frame_equal(result, expected) + + def test_series_broadcasting(self): + # smoke test for numpy warnings + # GH 16378, GH 16306 + df = DataFrame([1.0, 1.0, 1.0]) + df_nan = DataFrame({'A': [np.nan, 2.0, np.nan]}) + s = Series([1, 1, 1]) + s_nan = Series([np.nan, np.nan, 1]) + + with tm.assert_produces_warning(None): + df_nan.clip_lower(s, axis=0) + for op in ['lt', 'le', 'gt', 'ge', 'eq', 'ne']: + getattr(df, op)(s_nan, axis=0)