Skip to content Skip to sidebar Skip to footer

How To Handle Nans In Binning With Numpy Add.reduceat?

I'm using the numpy reduceat method for binning data. Background: I'm processing measurement data sampled at high frequencies and I need to down-sample them by extracting bin means

Solution 1:

We can use a masking based method -

# Mask of NaNs
mask = np.isnan(v)

# Replace NaNs with zeros
vn = np.where(mask,0,v)

# Use add.reduceat on NaNs skipped array to get summations
# Use add.reduceat on the mask to get valid counts
# Divide them to get final output
out = np.add.reduceat(vn, bins[:-1])/np.add.reduceat(~mask, bins[:-1])

Post a Comment for "How To Handle Nans In Binning With Numpy Add.reduceat?"