I want to take subsets of elements and quickly apply nanmean
to the associated columns, without looping.
For specificity, consider the reduction array r=[0,2,3]
, and the data array
a=np.array([
[2,3,4],
[3,np.NaN,5],
[16,66,666],
[2,2,5],
[np.NaN,3,4],
[np.NaN,4,5],
[np.NaN,5,4],
[3,6,4.5],
])
then I want to get back
b = np.array([
[2.5,3,4.5],
[16,66,666],
[2.5,4,4.5],
])
The top answer to this question solves the problem (for a single column) by using reduceat
. Unfortunately for me, since nanmean
is not a ufunc that trick does not work.