[SciPy-user] calculate average by excluding NaN value in Array

Pierre GM pgmdevlist@gmail....
Mon Mar 30 10:03:40 CDT 2009


On Mar 30, 2009, at 9:21 AM, Chris Barker wrote:

> David Cournapeau wrote:
>>> nanmean should do what you want,
>
> Actually, I don't think that's what the OP wanted:
>
>>>>> a=array([2.,3,4,5])
>>>>> b=([3.,2,4, NaN])
>>>>> average(array([a,b],"f"),axis=0)
>> array([ 2.5,  2.5,  4. ,  NaN], dtype=float32)
>>>>>
>> I mean that I need is [2.5,2.5,4,5], instead.
>
> In this case, numpy really is doing the only correct thing, in an
> operation like this, the result should be the same size as the input
> arrays, and the average of NaN and anything else can only be NaN.

Still, masked arrays were defined exactly for that kind of problem.
 >>> c=np.ma.fix_invalid(np.array([a,b],dtype=float))
 >>> c.mean(axis=0)

fix_invalid masks values that are NaNs/Infs.


More information about the SciPy-user mailing list