Skip to content

Commit 96f3e7c

Browse files
andrewarcherTomAugspurger
authored andcommitted
BUG: Silence numpy warnings when broadcasting comparison ops (GH16378, GH16306) (pandas-dev#16433)
TST: test for fix of GH16378, GH16306
1 parent b0a51df commit 96f3e7c

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
@@ -43,6 +43,7 @@ Conversion
4343
^^^^^^^^^^
4444

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

4748

4849
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
@@ -2081,3 +2081,16 @@ def test_n_duplicate_index(self, df_duplicates, n, order):
20812081
result = df.nlargest(n, order)
20822082
expected = df.sort_values(order, ascending=False).head(n)
20832083
tm.assert_frame_equal(result, expected)
2084+
2085+
def test_series_broadcasting(self):
2086+
# smoke test for numpy warnings
2087+
# GH 16378, GH 16306
2088+
df = DataFrame([1.0, 1.0, 1.0])
2089+
df_nan = DataFrame({'A': [np.nan, 2.0, np.nan]})
2090+
s = Series([1, 1, 1])
2091+
s_nan = Series([np.nan, np.nan, 1])
2092+
2093+
with tm.assert_produces_warning(None):
2094+
df_nan.clip_lower(s, axis=0)
2095+
for op in ['lt', 'le', 'gt', 'ge', 'eq', 'ne']:
2096+
getattr(df, op)(s_nan, axis=0)

0 commit comments

Comments
 (0)