Skip to content

Commit 6fbc9ce

Browse files
committed
BUG: First pass at fine-grained errstate.
1 parent 453bc26 commit 6fbc9ce

File tree

17 files changed

+132
-81
lines changed

17 files changed

+132
-81
lines changed

pandas/compat/numpy/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
from distutils.version import LooseVersion
66
from pandas.compat import string_types, string_and_binary_types
77

8-
# turn off all numpy warnings
9-
np.seterr(all='ignore')
108

119
# numpy versioning
1210
_np_version = np.version.short_version

pandas/computation/align.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ def _align_core(terms):
9595
term_axis_size = len(ti.axes[axis])
9696
reindexer_size = len(reindexer)
9797

98-
ordm = np.log10(abs(reindexer_size - term_axis_size))
98+
with np.errstate(divide='ignore'):
99+
ordm = np.log10(abs(reindexer_size - term_axis_size))
99100
if ordm >= 1 and reindexer_size >= 10000:
100101
warnings.warn('Alignment difference on axis {0} is larger '
101102
'than an order of magnitude on term {1!r}, '

pandas/computation/expressions.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ def _evaluate_standard(op, op_str, a, b, raise_on_error=True, **eval_kwargs):
5959
""" standard evaluation """
6060
if _TEST_MODE:
6161
_store_test_result(False)
62-
return op(a, b)
62+
with np.errstate(all='ignore'):
63+
return op(a, b)
6364

6465

6566
def _can_use_numexpr(op, op_str, a, b, dtype_check):

pandas/computation/ops.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ def __init__(self, func, args):
523523

524524
def __call__(self, env):
525525
operands = [op(env) for op in self.operands]
526-
return self.func.func(*operands)
526+
with np.errstate(all='ignore'):
527+
return self.func.func(*operands)
527528

528529
def __unicode__(self):
529530
operands = map(str, self.operands)

pandas/core/frame.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -3813,7 +3813,8 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
38133813
this = self[col].values
38143814
that = other[col].values
38153815
if filter_func is not None:
3816-
mask = ~filter_func(this) | isnull(that)
3816+
with np.errstate(all='ignore'):
3817+
mask = ~filter_func(this) | isnull(that)
38173818
else:
38183819
if raise_conflict:
38193820
mask_this = notnull(that)
@@ -4108,7 +4109,8 @@ def f(x):
41084109
return self._apply_empty_result(func, axis, reduce, *args, **kwds)
41094110

41104111
if isinstance(f, np.ufunc):
4111-
results = f(self.values)
4112+
with np.errstate(all='ignore'):
4113+
results = f(self.values)
41124114
return self._constructor(data=results, index=self.index,
41134115
columns=self.columns, copy=False)
41144116
else:
@@ -4934,7 +4936,8 @@ def f(x):
49344936
"type %s not implemented." %
49354937
filter_type)
49364938
raise_with_traceback(e)
4937-
result = f(data.values)
4939+
with np.errstate(all='ignore'):
4940+
result = f(data.values)
49384941
labels = data._get_agg_axis(axis)
49394942
else:
49404943
if numeric_only:

pandas/core/groupby.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,8 @@ def apply(self, func, *args, **kwargs):
678678

679679
@wraps(func)
680680
def f(g):
681-
return func(g, *args, **kwargs)
681+
with np.errstate(all='ignore'):
682+
return func(g, *args, **kwargs)
682683
else:
683684
raise ValueError('func must be a callable if args or '
684685
'kwargs are supplied')
@@ -4126,7 +4127,10 @@ def loop(labels, shape):
41264127
out = stride * labels[0].astype('i8', subok=False, copy=False)
41274128

41284129
for i in range(1, nlev):
4129-
stride //= shape[i]
4130+
if shape[i] == 0:
4131+
stride = 0
4132+
else:
4133+
stride //= shape[i]
41304134
out += labels[i] * stride
41314135

41324136
if xnull: # exclude nulls
@@ -4365,7 +4369,9 @@ def _get_group_index_sorter(group_index, ngroups):
43654369
count = len(group_index)
43664370
alpha = 0.0 # taking complexities literally; there may be
43674371
beta = 1.0 # some room for fine-tuning these parameters
4368-
if alpha + beta * ngroups < count * np.log(count):
4372+
with np.errstate(divide='ignore', invalid='ignore'):
4373+
do_groupsort = alpha + beta * ngroups < count * np.log(count)
4374+
if do_groupsort:
43694375
sorter, _ = _algos.groupsort_indexer(_ensure_int64(group_index),
43704376
ngroups)
43714377
return _ensure_platform_int(sorter)

pandas/core/internals.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ def apply(self, func, mgr=None, **kwargs):
348348
""" apply the function to my values; return a block if we are not
349349
one
350350
"""
351-
result = func(self.values, **kwargs)
351+
with np.errstate(all='ignore'):
352+
result = func(self.values, **kwargs)
352353
if not isinstance(result, Block):
353354
result = self.make_block(values=_block_shape(result,
354355
ndim=self.ndim))
@@ -1156,7 +1157,8 @@ def handle_error():
11561157

11571158
# get the result
11581159
try:
1159-
result = get_result(other)
1160+
with np.errstate(all='ignore'):
1161+
result = get_result(other)
11601162

11611163
# if we have an invalid shape/broadcast error
11621164
# GH4576, so raise instead of allowing to pass through

pandas/core/nanops.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def _f(*args, **kwargs):
4545
'this dtype'.format(
4646
f.__name__.replace('nan', '')))
4747
try:
48-
return f(*args, **kwargs)
48+
with np.errstate(invalid='ignore'):
49+
return f(*args, **kwargs)
4950
except ValueError as e:
5051
# we want to transform an object array
5152
# ValueError message to the more typical TypeError
@@ -513,7 +514,8 @@ def nanskew(values, axis=None, skipna=True):
513514
m2 = _zero_out_fperr(m2)
514515
m3 = _zero_out_fperr(m3)
515516

516-
result = (count * (count - 1) ** 0.5 / (count - 2)) * (m3 / m2 ** 1.5)
517+
with np.errstate(invalid='ignore', divide='ignore'):
518+
result = (count * (count - 1) ** 0.5 / (count - 2)) * (m3 / m2 ** 1.5)
517519

518520
dtype = values.dtype
519521
if is_float_dtype(dtype):
@@ -562,10 +564,11 @@ def nankurt(values, axis=None, skipna=True):
562564
m2 = adjusted2.sum(axis, dtype=np.float64)
563565
m4 = adjusted4.sum(axis, dtype=np.float64)
564566

565-
adj = 3 * (count - 1) ** 2 / ((count - 2) * (count - 3))
566-
numer = count * (count + 1) * (count - 1) * m4
567-
denom = (count - 2) * (count - 3) * m2**2
568-
result = numer / denom - adj
567+
with np.errstate(invalid='ignore', divide='ignore'):
568+
adj = 3 * (count - 1) ** 2 / ((count - 2) * (count - 3))
569+
numer = count * (count + 1) * (count - 1) * m4
570+
denom = (count - 2) * (count - 3) * m2**2
571+
result = numer / denom - adj
569572

570573
# floating point error
571574
numer = _zero_out_fperr(numer)
@@ -658,7 +661,8 @@ def _maybe_null_out(result, axis, mask):
658661

659662
def _zero_out_fperr(arg):
660663
if isinstance(arg, np.ndarray):
661-
return np.where(np.abs(arg) < 1e-14, 0, arg)
664+
with np.errstate(invalid='ignore'):
665+
return np.where(np.abs(arg) < 1e-14, 0, arg)
662666
else:
663667
return arg.dtype.type(0) if np.abs(arg) < 1e-14 else arg
664668

@@ -760,7 +764,8 @@ def f(x, y):
760764
ymask = isnull(y)
761765
mask = xmask | ymask
762766

763-
result = op(x, y)
767+
with np.errstate(all='ignore'):
768+
result = op(x, y)
764769

765770
if mask.any():
766771
if is_bool_dtype(result):

pandas/core/ops.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,8 @@ def na_op(x, y):
636636

637637
def safe_na_op(lvalues, rvalues):
638638
try:
639-
return na_op(lvalues, rvalues)
639+
with np.errstate(all='ignore'):
640+
return na_op(lvalues, rvalues)
640641
except Exception:
641642
if isinstance(rvalues, ABCSeries):
642643
if is_object_dtype(rvalues):
@@ -743,7 +744,8 @@ def na_op(x, y):
743744
x = x.view('i8')
744745

745746
try:
746-
result = getattr(x, name)(y)
747+
with np.errstate(all='ignore'):
748+
result = getattr(x, name)(y)
747749
if result is NotImplemented:
748750
raise TypeError("invalid type comparison")
749751
except AttributeError:
@@ -796,13 +798,15 @@ def wrapper(self, other, axis=None):
796798
# which would then not take categories ordering into account
797799
# we can go directly to op, as the na_op would just test again and
798800
# dispatch to it.
799-
res = op(self.values, other)
801+
with np.errstate(all='ignore'):
802+
res = op(self.values, other)
800803
else:
801804
values = self.get_values()
802805
if isinstance(other, (list, np.ndarray)):
803806
other = np.asarray(other)
804807

805-
res = na_op(values, other)
808+
with np.errstate(all='ignore'):
809+
res = na_op(values, other)
806810
if isscalar(res):
807811
raise TypeError('Could not compare %s type with Series' %
808812
type(other))
@@ -1096,13 +1100,15 @@ def na_op(x, y):
10961100
xrav = xrav[mask]
10971101
yrav = yrav[mask]
10981102
if np.prod(xrav.shape) and np.prod(yrav.shape):
1099-
result[mask] = op(xrav, yrav)
1103+
with np.errstate(all='ignore'):
1104+
result[mask] = op(xrav, yrav)
11001105
elif hasattr(x, 'size'):
11011106
result = np.empty(x.size, dtype=x.dtype)
11021107
mask = notnull(xrav)
11031108
xrav = xrav[mask]
11041109
if np.prod(xrav.shape):
1105-
result[mask] = op(xrav, y)
1110+
with np.errstate(all='ignore'):
1111+
result[mask] = op(xrav, y)
11061112
else:
11071113
raise TypeError("cannot perform operation {op} between "
11081114
"objects of type {x} and {y}".format(

pandas/core/panel.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,8 @@ def _combine(self, other, func, axis=0):
713713
(str(type(other)), str(type(self))))
714714

715715
def _combine_const(self, other, func):
716-
new_values = func(self.values, other)
716+
with np.errstate(all='ignore'):
717+
new_values = func(self.values, other)
717718
d = self._construct_axes_dict()
718719
return self._constructor(new_values, **d)
719720

@@ -723,14 +724,15 @@ def _combine_frame(self, other, func, axis=0):
723724

724725
other = other.reindex(index=index, columns=columns)
725726

726-
if axis == 0:
727-
new_values = func(self.values, other.values)
728-
elif axis == 1:
729-
new_values = func(self.values.swapaxes(0, 1), other.values.T)
730-
new_values = new_values.swapaxes(0, 1)
731-
elif axis == 2:
732-
new_values = func(self.values.swapaxes(0, 2), other.values)
733-
new_values = new_values.swapaxes(0, 2)
727+
with np.errstate(all='ignore'):
728+
if axis == 0:
729+
new_values = func(self.values, other.values)
730+
elif axis == 1:
731+
new_values = func(self.values.swapaxes(0, 1), other.values.T)
732+
new_values = new_values.swapaxes(0, 1)
733+
elif axis == 2:
734+
new_values = func(self.values.swapaxes(0, 2), other.values)
735+
new_values = new_values.swapaxes(0, 2)
734736

735737
return self._constructor(new_values, self.items, self.major_axis,
736738
self.minor_axis)
@@ -744,7 +746,8 @@ def _combine_panel(self, other, func):
744746
this = self.reindex(items=items, major=major, minor=minor)
745747
other = other.reindex(items=items, major=major, minor=minor)
746748

747-
result_values = func(this.values, other.values)
749+
with np.errstate(all='ignore'):
750+
result_values = func(this.values, other.values)
748751

749752
return self._constructor(result_values, items, major, minor)
750753

@@ -1011,7 +1014,8 @@ def apply(self, func, axis='major', **kwargs):
10111014
# try ufunc like
10121015
if isinstance(f, np.ufunc):
10131016
try:
1014-
result = np.apply_along_axis(func, axis, self.values)
1017+
with np.errstate(all='ignore'):
1018+
result = np.apply_along_axis(func, axis, self.values)
10151019
return self._wrap_result(result, axis=axis)
10161020
except (AttributeError):
10171021
pass
@@ -1113,7 +1117,8 @@ def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
11131117
axis_number = self._get_axis_number(axis_name)
11141118
f = lambda x: op(x, axis=axis_number, skipna=skipna, **kwds)
11151119

1116-
result = f(self.values)
1120+
with np.errstate(all='ignore'):
1121+
result = f(self.values)
11171122

11181123
axes = self._get_plane_axes(axis_name)
11191124
if result.ndim == 2 and axis_name != self._info_axis_name:

pandas/core/series.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,8 @@ def _binop(self, other, func, level=None, fill_value=None):
16261626
this_vals[this_mask & mask] = fill_value
16271627
other_vals[other_mask & mask] = fill_value
16281628

1629-
result = func(this_vals, other_vals)
1629+
with np.errstate(all='ignore'):
1630+
result = func(this_vals, other_vals)
16301631
name = _maybe_match_name(self, other)
16311632
result = self._constructor(result, index=new_index, name=name)
16321633
result = result.__finalize__(self)
@@ -1658,10 +1659,12 @@ def combine(self, other, func, fill_value=nan):
16581659
for i, idx in enumerate(new_index):
16591660
lv = self.get(idx, fill_value)
16601661
rv = other.get(idx, fill_value)
1661-
new_values[i] = func(lv, rv)
1662+
with np.errstate(all='ignore'):
1663+
new_values[i] = func(lv, rv)
16621664
else:
16631665
new_index = self.index
1664-
new_values = func(self._values, other)
1666+
with np.errstate(all='ignore'):
1667+
new_values = func(self._values, other)
16651668
new_name = self.name
16661669
return self._constructor(new_values, index=new_index, name=new_name)
16671670

@@ -2240,14 +2243,15 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
22402243
else:
22412244
f = func
22422245

2243-
if isinstance(f, np.ufunc):
2244-
return f(self)
2246+
with np.errstate(all='ignore'):
2247+
if isinstance(f, np.ufunc):
2248+
return f(self)
22452249

2246-
if is_extension_type(self.dtype):
2247-
mapped = self._values.map(f)
2248-
else:
2249-
values = self.asobject
2250-
mapped = lib.map_infer(values, f, convert=convert_dtype)
2250+
if is_extension_type(self.dtype):
2251+
mapped = self._values.map(f)
2252+
else:
2253+
values = self.asobject
2254+
mapped = lib.map_infer(values, f, convert=convert_dtype)
22512255

22522256
if len(mapped) and isinstance(mapped[0], Series):
22532257
from pandas.core.frame import DataFrame
@@ -2272,7 +2276,8 @@ def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
22722276
if numeric_only:
22732277
raise NotImplementedError('Series.{0} does not implement '
22742278
'numeric_only.'.format(name))
2275-
return op(delegate, skipna=skipna, **kwds)
2279+
with np.errstate(all='ignore'):
2280+
return op(delegate, skipna=skipna, **kwds)
22762281

22772282
return delegate._reduce(op=op, name=name, axis=axis, skipna=skipna,
22782283
numeric_only=numeric_only,

pandas/core/window.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -733,10 +733,11 @@ def calc(x):
733733
def calc(x):
734734
return func(x, window, min_periods=self.min_periods)
735735

736-
if values.ndim > 1:
737-
result = np.apply_along_axis(calc, self.axis, values)
738-
else:
739-
result = calc(values)
736+
with np.errstate(all='ignore'):
737+
if values.ndim > 1:
738+
result = np.apply_along_axis(calc, self.axis, values)
739+
else:
740+
result = calc(values)
740741

741742
if center:
742743
result = self._center_window(result, window)
@@ -1617,10 +1618,11 @@ def _cov(x, y):
16171618

16181619
x_values = X._prep_values()
16191620
y_values = Y._prep_values()
1620-
cov = _cov(x_values, y_values)
1621-
x_var = _cov(x_values, x_values)
1622-
y_var = _cov(y_values, y_values)
1623-
corr = cov / _zsqrt(x_var * y_var)
1621+
with np.errstate(all='ignore'):
1622+
cov = _cov(x_values, y_values)
1623+
x_var = _cov(x_values, x_values)
1624+
y_var = _cov(y_values, y_values)
1625+
corr = cov / _zsqrt(x_var * y_var)
16241626
return X._wrap_result(corr)
16251627

16261628
return _flex_binary_moment(self._selected_obj, other._selected_obj,
@@ -1757,8 +1759,9 @@ def _use_window(minp, window):
17571759

17581760

17591761
def _zsqrt(x):
1760-
result = np.sqrt(x)
1761-
mask = x < 0
1762+
with np.errstate(all='ignore'):
1763+
result = np.sqrt(x)
1764+
mask = x < 0
17621765

17631766
from pandas import DataFrame
17641767
if isinstance(x, DataFrame):

0 commit comments

Comments
 (0)