[Numpy-discussion] Tests for empty arrays

Keith Goodman kwgoodman@gmail....
Tue May 6 12:14:41 CDT 2008


On Tue, May 6, 2008 at 10:03 AM, Timothy Hochberg <tim.hochberg@ieee.org> wrote:
> Why don't you just roll your own?
>
> >>> def nans(shape, dtype=float):
> ...     a = np.empty(shape, dtype)
> ...     a.fill(np.nan)
> ...     return a
>  ...
> >>> nans([3,4])
> array([[ NaN,  NaN,  NaN,  NaN],
>        [ NaN,  NaN,  NaN,  NaN],
>        [ NaN,  NaN,  NaN,  NaN]])

I learn a lot from this list. I didn't know about fill. Looks like it
is much faster than adding nan.

>> timeit nans0((500,500))
10 loops, best of 3: 30.5 ms per loop
>> timeit nans1((500,500))
1000 loops, best of 3: 956 µs per loop

def nans0(shape, dtype=float):
    a = np.ones(shape, dtype)
    a += np.nan
    return a

def nans1(shape, dtype=float):
    a = np.empty(shape, dtype)
    a.fill(np.nan)

No need to roll my own. I'll smoke yours.
    return a


More information about the Numpy-discussion mailing list