[SciPy-user] NaN's in numpy (and Scipy)

Pierre GM pgmdevlist at gmail.com
Sat Jan 6 19:52:50 EST 2007


On Saturday 06 January 2007 18:47, Vincent Nijs wrote:
> I see. But when you assign a float (e.g., nan or 0.1) to an element of an
> array filled with 'int16' elements the array is not up-cast but the element
> down-cast.

True. 

When you create an array, you select a particular area of memory. How much 
depends on the shape of the array, and its dtype. Picture building a 
checkboard with a given number of rows and columns (the shape), and with a 
fixed cell size (the dtype). Once the array is created, you can't fill a cell 
with some data larger than the cell size: the data has to be shrunk to fit.

So, once you create an array of int_, any float/complex you add to it will be 
shrunk to int_, because the memory space has been already allocated for int_ 
and nothing larger.

If you really want another larger tile size, then you have to create a new 
array from scratch. That's what you do by using 'astype'.

In our particular case:
If you expect missing data as NaN, then use float_ as a default (which most of 
the numpy functions do). Note that you're not limited to NaN for the missing 
data: anything can do:
>>> x = MA.array([1,2,-99,4], mask=(x=-99))
will work, so will
>>> x=MA.masked_values([1,2,-99,4], -99)




More information about the SciPy-User mailing list