numpy.errstate#

classnumpy.errstate(**kwargs)[source]#

Context manager for floating-point error handling.

Using an instance oferrstate as a context manager allows statements inthat context to execute with a known error handling behavior. Upon enteringthe context the error handling is set withseterr andseterrcall, andupon exiting it is reset to what it was before.

Changed in version 1.17.0:errstate is also usable as a function decorator, savinga level of indentation if an entire function is wrapped.

Changed in version 2.0:errstate is now fully thread and asyncio safe, but may not beentered more than once.It is not safe to decorate async functions usingerrstate.

Parameters:
kwargs{divide, over, under, invalid}

Keyword arguments. The valid keywords are the possible floating-pointexceptions. Each keyword should have a string value that defines thetreatment for the particular error. Possible values are{‘ignore’, ‘warn’, ‘raise’, ‘call’, ‘print’, ‘log’}.

Notes

For complete documentation of the types of floating-point exceptions andtreatment options, seeseterr.

Examples

>>>importnumpyasnp>>>olderr=np.seterr(all='ignore')# Set error handling to known state.
>>>np.arange(3)/0.array([nan, inf, inf])>>>withnp.errstate(divide='ignore'):...np.arange(3)/0.array([nan, inf, inf])
>>>np.sqrt(-1)np.float64(nan)>>>withnp.errstate(invalid='raise'):...np.sqrt(-1)Traceback (most recent call last):  File"<stdin>", line2, in<module>FloatingPointError:invalid value encountered in sqrt

Outside the context the error handling behavior has not changed:

>>>np.geterr(){'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}>>>olderr=np.seterr(**olderr)# restore original state

Methods

__call__(func)

Call self as a function.

On this page