Skip to content

Commit 3b12f08

Browse files
committed
ENH: Silence numpy warnings from certain expressions computed during tests.
1 parent eca512c commit 3b12f08

File tree

14 files changed

+176
-150
lines changed

14 files changed

+176
-150
lines changed

pandas/computation/tests/test_eval.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1613,7 +1613,8 @@ def test_unary_functions(self):
16131613
for fn in self.unary_fns:
16141614
expr = "{0}(a)".format(fn)
16151615
got = self.eval(expr)
1616-
expect = getattr(np, fn)(a)
1616+
with np.errstate(all='ignore'):
1617+
expect = getattr(np, fn)(a)
16171618
tm.assert_series_equal(got, expect, check_names=False)
16181619

16191620
def test_binary_functions(self):
@@ -1624,7 +1625,8 @@ def test_binary_functions(self):
16241625
for fn in self.binary_fns:
16251626
expr = "{0}(a, b)".format(fn)
16261627
got = self.eval(expr)
1627-
expect = getattr(np, fn)(a, b)
1628+
with np.errstate(all='ignore'):
1629+
expect = getattr(np, fn)(a, b)
16281630
tm.assert_almost_equal(got, expect, check_names=False)
16291631

16301632
def test_df_use_case(self):

pandas/sparse/tests/test_array.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -516,15 +516,17 @@ def _check_inplace_op(op):
516516
tmp = arr1.copy()
517517
self.assertRaises(NotImplementedError, op, tmp, arr2)
518518

519-
bin_ops = [operator.add, operator.sub, operator.mul, operator.truediv,
520-
operator.floordiv, operator.pow]
521-
for op in bin_ops:
522-
_check_op(op, arr1, arr2)
523-
_check_op(op, farr1, farr2)
524-
525-
inplace_ops = ['iadd', 'isub', 'imul', 'itruediv', 'ifloordiv', 'ipow']
526-
for op in inplace_ops:
527-
_check_inplace_op(getattr(operator, op))
519+
with np.errstate(all='ignore'):
520+
bin_ops = [operator.add, operator.sub, operator.mul,
521+
operator.truediv, operator.floordiv, operator.pow]
522+
for op in bin_ops:
523+
_check_op(op, arr1, arr2)
524+
_check_op(op, farr1, farr2)
525+
526+
inplace_ops = ['iadd', 'isub', 'imul', 'itruediv', 'ifloordiv',
527+
'ipow']
528+
for op in inplace_ops:
529+
_check_inplace_op(getattr(operator, op))
528530

529531
def test_pickle(self):
530532
def _check_roundtrip(obj):

pandas/tests/formats/test_format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ def test_string_repr_encoding(self):
16681668

16691669
def test_repr_corner(self):
16701670
# representing infs poses no problems
1671-
df = DataFrame({'foo': np.inf * np.empty(10)})
1671+
df = DataFrame({'foo': [-np.inf, np.inf]})
16721672
repr(df)
16731673

16741674
def test_frame_info_encoding(self):

pandas/tests/frame/test_apply.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@ class TestDataFrameApply(tm.TestCase, TestData):
2222
_multiprocess_can_split_ = True
2323

2424
def test_apply(self):
25-
# ufunc
26-
applied = self.frame.apply(np.sqrt)
27-
assert_series_equal(np.sqrt(self.frame['A']), applied['A'])
25+
with np.errstate(all='ignore'):
26+
# ufunc
27+
applied = self.frame.apply(np.sqrt)
28+
assert_series_equal(np.sqrt(self.frame['A']), applied['A'])
2829

29-
# aggregator
30-
applied = self.frame.apply(np.mean)
31-
self.assertEqual(applied['A'], np.mean(self.frame['A']))
30+
# aggregator
31+
applied = self.frame.apply(np.mean)
32+
self.assertEqual(applied['A'], np.mean(self.frame['A']))
3233

33-
d = self.frame.index[0]
34-
applied = self.frame.apply(np.mean, axis=1)
35-
self.assertEqual(applied[d], np.mean(self.frame.xs(d)))
36-
self.assertIs(applied.index, self.frame.index) # want this
34+
d = self.frame.index[0]
35+
applied = self.frame.apply(np.mean, axis=1)
36+
self.assertEqual(applied[d], np.mean(self.frame.xs(d)))
37+
self.assertIs(applied.index, self.frame.index) # want this
3738

3839
# invalid axis
3940
df = DataFrame(
@@ -187,10 +188,11 @@ def _checkit(axis=0, raw=False):
187188
_checkit(raw=True)
188189
_checkit(axis=0, raw=True)
189190

190-
_check(no_cols, lambda x: x)
191-
_check(no_cols, lambda x: x.mean())
192-
_check(no_index, lambda x: x)
193-
_check(no_index, lambda x: x.mean())
191+
with np.errstate(all='ignore'):
192+
_check(no_cols, lambda x: x)
193+
_check(no_cols, lambda x: x.mean())
194+
_check(no_index, lambda x: x)
195+
_check(no_index, lambda x: x.mean())
194196

195197
result = no_cols.apply(lambda x: x.mean(), broadcast=True)
196198
tm.assertIsInstance(result, DataFrame)

pandas/tests/frame/test_misc_api.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ def test_new_empty_index(self):
207207
self.assertIsNone(df2.index.name)
208208

209209
def test_array_interface(self):
210-
result = np.sqrt(self.frame)
210+
with np.errstate(all='ignore'):
211+
result = np.sqrt(self.frame)
211212
tm.assertIsInstance(result, type(self.frame))
212213
self.assertIs(result.index, self.frame.index)
213214
self.assertIs(result.columns, self.frame.columns)

pandas/tests/frame/test_operators.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ def test_modulo(self):
217217
assert_frame_equal(result, expected)
218218

219219
# numpy has a slightly different (wrong) treatement
220-
result2 = DataFrame(p.values % p.values, index=p.index,
220+
with np.errstate(all='ignore'):
221+
arr = p.values % p.values
222+
result2 = DataFrame(arr, index=p.index,
221223
columns=p.columns, dtype='float64')
222224
result2.iloc[0:3, 1] = np.nan
223225
assert_frame_equal(result2, expected)
@@ -227,8 +229,9 @@ def test_modulo(self):
227229
assert_frame_equal(result, expected)
228230

229231
# numpy has a slightly different (wrong) treatement
230-
result2 = DataFrame(p.values.astype('float64') %
231-
0, index=p.index, columns=p.columns)
232+
with np.errstate(all='ignore'):
233+
arr = p.values.astype('float64') % 0
234+
result2 = DataFrame(arr, index=p.index, columns=p.columns)
232235
assert_frame_equal(result2, expected)
233236

234237
# not commutative with series
@@ -248,7 +251,9 @@ def test_div(self):
248251
'second': Series([nan, nan, nan, 1])})
249252
assert_frame_equal(result, expected)
250253

251-
result2 = DataFrame(p.values.astype('float') / p.values, index=p.index,
254+
with np.errstate(all='ignore'):
255+
arr = p.values.astype('float') / p.values
256+
result2 = DataFrame(arr, index=p.index,
252257
columns=p.columns)
253258
assert_frame_equal(result2, expected)
254259

@@ -258,7 +263,9 @@ def test_div(self):
258263
assert_frame_equal(result, expected)
259264

260265
# numpy has a slightly different (wrong) treatement
261-
result2 = DataFrame(p.values.astype('float64') / 0, index=p.index,
266+
with np.errstate(all='ignore'):
267+
arr = p.values.astype('float64') / 0
268+
result2 = DataFrame(arr, index=p.index,
262269
columns=p.columns)
263270
assert_frame_equal(result2, expected)
264271

pandas/tests/indexes/common.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,9 @@ def test_numpy_ufuncs(self):
712712
func(idx)
713713
elif isinstance(idx, (Float64Index, Int64Index)):
714714
# coerces to float (e.g. np.sin)
715-
result = func(idx)
716-
exp = Index(func(idx.values), name=idx.name)
715+
with np.errstate(all='ignore'):
716+
result = func(idx)
717+
exp = Index(func(idx.values), name=idx.name)
717718
self.assert_index_equal(result, exp)
718719
self.assertIsInstance(result, pd.Float64Index)
719720
else:

pandas/tests/series/test_analytics.py

+28-27
Original file line numberDiff line numberDiff line change
@@ -622,39 +622,40 @@ def test_all_any_params(self):
622622
self.assertRaises(NotImplementedError, s.all, bool_only=True)
623623

624624
def test_modulo(self):
625+
with np.errstate(all='ignore'):
626+
627+
# GH3590, modulo as ints
628+
p = DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]})
629+
result = p['first'] % p['second']
630+
expected = Series(p['first'].values % p['second'].values,
631+
dtype='float64')
632+
expected.iloc[0:3] = np.nan
633+
assert_series_equal(result, expected)
625634

626-
# GH3590, modulo as ints
627-
p = DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]})
628-
result = p['first'] % p['second']
629-
expected = Series(p['first'].values % p['second'].values,
630-
dtype='float64')
631-
expected.iloc[0:3] = np.nan
632-
assert_series_equal(result, expected)
633-
634-
result = p['first'] % 0
635-
expected = Series(np.nan, index=p.index, name='first')
636-
assert_series_equal(result, expected)
635+
result = p['first'] % 0
636+
expected = Series(np.nan, index=p.index, name='first')
637+
assert_series_equal(result, expected)
637638

638-
p = p.astype('float64')
639-
result = p['first'] % p['second']
640-
expected = Series(p['first'].values % p['second'].values)
641-
assert_series_equal(result, expected)
639+
p = p.astype('float64')
640+
result = p['first'] % p['second']
641+
expected = Series(p['first'].values % p['second'].values)
642+
assert_series_equal(result, expected)
642643

643-
p = p.astype('float64')
644-
result = p['first'] % p['second']
645-
result2 = p['second'] % p['first']
646-
self.assertFalse(np.array_equal(result, result2))
644+
p = p.astype('float64')
645+
result = p['first'] % p['second']
646+
result2 = p['second'] % p['first']
647+
self.assertFalse(np.array_equal(result, result2))
647648

648-
# GH 9144
649-
s = Series([0, 1])
649+
# GH 9144
650+
s = Series([0, 1])
650651

651-
result = s % 0
652-
expected = Series([nan, nan])
653-
assert_series_equal(result, expected)
652+
result = s % 0
653+
expected = Series([nan, nan])
654+
assert_series_equal(result, expected)
654655

655-
result = 0 % s
656-
expected = Series([nan, 0.0])
657-
assert_series_equal(result, expected)
656+
result = 0 % s
657+
expected = Series([nan, 0.0])
658+
assert_series_equal(result, expected)
658659

659660
def test_ops_consistency_on_empty(self):
660661

pandas/tests/series/test_apply.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ class TestSeriesApply(TestData, tm.TestCase):
1818
_multiprocess_can_split_ = True
1919

2020
def test_apply(self):
21-
assert_series_equal(self.ts.apply(np.sqrt), np.sqrt(self.ts))
22-
23-
# elementwise-apply
24-
import math
25-
assert_series_equal(self.ts.apply(math.exp), np.exp(self.ts))
26-
27-
# how to handle Series result, #2316
28-
result = self.ts.apply(lambda x: Series(
29-
[x, x ** 2], index=['x', 'x^2']))
30-
expected = DataFrame({'x': self.ts, 'x^2': self.ts ** 2})
31-
tm.assert_frame_equal(result, expected)
21+
with np.errstate(all='ignore'):
22+
assert_series_equal(self.ts.apply(np.sqrt), np.sqrt(self.ts))
23+
24+
# elementwise-apply
25+
import math
26+
assert_series_equal(self.ts.apply(math.exp), np.exp(self.ts))
27+
28+
# how to handle Series result, #2316
29+
result = self.ts.apply(lambda x: Series(
30+
[x, x ** 2], index=['x', 'x^2']))
31+
expected = DataFrame({'x': self.ts, 'x^2': self.ts ** 2})
32+
tm.assert_frame_equal(result, expected)
3233

3334
# empty series
3435
s = Series(dtype=object, name='foo', index=pd.Index([], name='bar'))

0 commit comments

Comments
 (0)