Skip to content

Commit 0ffce83

Browse files
andrewarcherTomAugspurger
authored andcommitted
BUG: Silence numpy warnings when broadcasting comparison ops (GH16378, GH16306) (#16433)
TST: test for fix of GH16378, GH16306(cherry picked from commit 96f3e7c)
1 parent 8ae7fd5 commit 0ffce83

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v0.20.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Conversion
4444
^^^^^^^^^^
4545

4646
- Bug in ``pd.to_numeric()`` in which empty data inputs were causing Python to crash (:issue:`16302`)
47+
- Silence numpy warnings when broadcasting DataFrame to Series with comparison ops (:issue:`16378`, :issue:`16306`)
4748

4849

4950
Indexing

pandas/core/ops.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,8 @@ def _flex_comp_method_FRAME(op, name, str_rep=None, default_axis='columns',
12501250
masker=False):
12511251
def na_op(x, y):
12521252
try:
1253-
result = op(x, y)
1253+
with np.errstate(invalid='ignore'):
1254+
result = op(x, y)
12541255
except TypeError:
12551256
xrav = x.ravel()
12561257
result = np.empty(x.size, dtype=bool)

pandas/tests/frame/test_analytics.py

+13
Original file line numberDiff line numberDiff line change
@@ -2056,3 +2056,16 @@ def test_n_duplicate_index(self, df_duplicates, n, order):
20562056
result = df.nlargest(n, order)
20572057
expected = df.sort_values(order, ascending=False).head(n)
20582058
tm.assert_frame_equal(result, expected)
2059+
2060+
def test_series_broadcasting(self):
2061+
# smoke test for numpy warnings
2062+
# GH 16378, GH 16306
2063+
df = DataFrame([1.0, 1.0, 1.0])
2064+
df_nan = DataFrame({'A': [np.nan, 2.0, np.nan]})
2065+
s = Series([1, 1, 1])
2066+
s_nan = Series([np.nan, np.nan, 1])
2067+
2068+
with tm.assert_produces_warning(None):
2069+
df_nan.clip_lower(s, axis=0)
2070+
for op in ['lt', 'le', 'gt', 'ge', 'eq', 'ne']:
2071+
getattr(df, op)(s_nan, axis=0)

0 commit comments

Comments
 (0)