[SciPy-user] What about 'None'? Comment about nan...

A. M. Archibald peridot.faceted at gmail.com
Thu Nov 30 01:43:55 CST 2006


On 30/11/06, Martin Kuemmel <mkuemmel at eso.org> wrote:
> Hi,
>
> this question may (as well) be trivial for some of us, but bothers
> me for some time. It is:
>
>  >>> gaga = numpy.array([1.0,2.0,None])
>  >>> gaga
> array([1.0, 2.0, None], dtype=object)
>
> The type of the array is 'object'. Is there a way to have 'None'
> in a numerical array type? Any trick, idea or recipe?

The short answer is no. Since numerical arrays by definition contain
only a single data type (and can therefore reduce the burden of type
tagging, dynamic allocation, and so on) they can't contain anything
but numbers.

If you want to use "None" to mark places there is no data,
"MaskedArray"s are what you want; they also include appropriate
propagation of masked (i.e., no-data) entries through calculations.
Alternatively, you could use NaNs to signify "no data"; this may not
be a good idea as on some hardware (pentium M but not opteron, for
example) they are slower. (There are other pros and cons to using
NaNs, but I think the list is sick of me going on about them.)

> About nan: It is great that 'numpy.NAN' exists, but the handbook
>             says nothing about it. Are there more glorious, but
>             hidden constants or functions or methods?

The documentation does have its lacunae, but this one has a more
plausible excuse than usual. NaN is not an invention of numpy but of
the IEEE floating-point standard. It is a normal (well, all right,
abnormal but accepted) floating-point number, supported by most
platforms (it is represented by one of many otherwise-invalid bit
patterns for floating-point numbers). Which is why you can put it in
floating-point arrays (but not integer arrays). There are also
positive and negative infinities, positive and negative zero, and
"denormalized" numbers (almost too small to represent, they lose
accuracy gradually as they get smaller). This bestiary is actually
designed to reduce the confusion induced by floating-point programs by
causing them to behave more gracefully when given extreme inputs
(whether it succeeds is a matter of opinion).

A. M. Archibald


More information about the SciPy-user mailing list