Skip to content

Commit c78ee39

Browse files
author
Andrew Archer
committed
BUG: Silence numpy warnings when broadcasting DataFrame to Series with comparison ops (GH16378, GH16306)
TST: test for fix of GH16378, GH16306
1 parent d5a681b commit c78ee39

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
@@ -42,6 +42,7 @@ Conversion
4242
^^^^^^^^^^
4343

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

4647

4748
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
@@ -2073,3 +2073,16 @@ def test_n_duplicate_index(self, df_duplicates, n, order):
20732073
result = df.nlargest(n, order)
20742074
expected = df.sort_values(order, ascending=False).head(n)
20752075
tm.assert_frame_equal(result, expected)
2076+
2077+
def test_series_broadcasting(self):
2078+
# smoke test for numpy warnings
2079+
# GH 16378, GH 16306
2080+
df = DataFrame([1.0, 1.0, 1.0])
2081+
df_nan = DataFrame({'A': [np.nan, 2.0, np.nan]})
2082+
s = Series([1, 1, 1])
2083+
s_nan = Series([np.nan, np.nan, 1])
2084+
2085+
with tm.assert_produces_warning(None):
2086+
df_nan.clip_lower(s, axis=0)
2087+
for op in ['lt', 'le', 'gt', 'ge', 'eq', 'ne']:
2088+
getattr(df, op)(s_nan, axis=0)

0 commit comments

Comments
 (0)