Consider the following example:
>>> import numpy as np
>>> a = np.array([1.0, 2.1j])
>>> b = np.array(a, dtype=np.float64)
/Users/goerz/anaconda3/bin/ipython:1: ComplexWarning: Casting complex values to real discards the imaginary part
#!/Users/goerz/anaconda3/bin/python
How can I catch the ComplexWarning as an Exception?
I have tried np.seterr
, but this has no effect (as it only relates to floating point warnings such as underflows/overflows).
I've also tried the with warnings.catch_warnings():
context manager from the standard library, but it also has no effect.