The average number array returns NaN

I have np.array with over 330,000 lines. I am just trying to take the average and it returns NaN. Even if I try to filter out any potential NaN values ​​in my array (there shouldn’t be anyway), the average returns NaN. Am I doing something completely stupid?

My code is here:

average(ngma_heat_daily) Out[70]: nan average(ngma_heat_daily[ngma_heat_daily != nan]) Out[71]: nan 
+7
python arrays numpy
source share
1 answer

try the following:

 >>> np.nanmean(ngma_heat_daily) 

This function discards NaN values ​​from your array before accepting the average value.

Edit: the reason average(ngma_heat_daily[ngma_heat_daily != nan]) doesn't work is related to this:

 >>> np.nan == np.nan False 

according to the IEEE floating point standard, NaN is not equal to itself! You could do this instead to implement the same idea:

 >>> average(ngma_heat_daily[~np.isnan(ngma_heat_daily)]) 

np.isnan , np.isinf , and similar functions are very useful for this type of data masking.

+5
source share

All Articles