[Numpy-discussion] nan_to_num and bool arrays
Robert Kern
robert.kern@gmail....
Fri Dec 11 20:38:12 CST 2009
On Fri, Dec 11, 2009 at 18:38, Keith Goodman <kwgoodman@gmail.com> wrote:
> That seems to work. To avoid changing the input
>
>>> x = np.array(1)
>>> x.shape
> ()
>>> y = nan_to_num(x)
>>> x.shape
> (1,)
>
> I moved y = x.copy() further up and switched x's to y's. Here's what
> it looks like:
>
> def nan_to_num(x):
> is_scalar = False
> if not isinstance(x, _nx.ndarray):
> x = asarray(x)
> if x.shape == ():
> # Must return this as a scalar later.
> is_scalar = True
> y = x.copy()
> old_shape = y.shape
> if y.shape == ():
> # We need element access.
> y.shape = (1,)
> t = y.dtype.type
> if issubclass(t, _nx.complexfloating):
> return nan_to_num(y.real) + 1j * nan_to_num(y.imag)
Almost! You need to handle the shape restoration in this branch, too.
In [9]: nan_to_num(array(1+1j))
Out[9]: array([ 1.+1.j])
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion
mailing list