[Numpy-discussion] question on NumPy NaN

Keith Goodman kwgoodman@gmail....
Tue May 20 11:24:36 CDT 2008


On Tue, May 20, 2008 at 9:11 AM, Anne Archibald
<peridot.faceted@gmail.com> wrote:
> 2008/5/20 Vasileios Gkinis <v.gkinis@rug.nl>:
>
>> I have a question concerning nan in NumPy.
>> Lets say i have an array of sample measurements
>> a = array((2,4,nan))
>> in NumPy calculating the mean of the elements in array a looks like:
>>
>>>>> a = array((2,4,nan))
>>>>> a
>> array([  2.,   4.,  NaN])
>>>>> mean(a)
>> nan
>>
>> What if i simply dont want nan to propagate and get something that would
>> look like:
>>
>>>>> a = array((2,4,nan))
>>>>> a
>> array([  2.,   4.,  NaN])
>>>>> mean(a)
>> 3.
>
> For more elaborate handling of missing data, look into "masked
> arrays", in numpy.ma. They are designed to deal with exactly this sort
> of thing.

Or

np.nansum(a) / np.isfinite(a).sum()

A nanmean would be nice to have in numpy.


More information about the Numpy-discussion mailing list