Issue
I have a numpy array like this:
np.array([1, 2, 3, 4, np.nan, 2, 4, np.nan, 5, 1, 2, 1, np.nan, 10])
I want to split it and sum.
Expected output:
np.array([10, 6, 9, 10])
Can someone help me please? Thanks.
Solution
Copy and set nan
to 0, and then use numpy.add.reduceat
to sum:
>>> mask = np.isnan(ar)
>>> arr = np.where(mask, 0, ar)
>>> mask[0] = True
>>> np.add.reduceat(arr, mask.nonzero()[0])
array([10., 6., 9., 10.])
Answered By - Mechanic Pig
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.