Cumsum reset at NaN Cumsum reset at NaN python python

Cumsum reset at NaN


A simple Numpy translation of your Matlab code is this:

import numpy as npv = np.array([1., 1., 1., np.nan, 1., 1., 1., 1., np.nan, 1.])n = np.isnan(v)a = ~nc = np.cumsum(a)d = np.diff(np.concatenate(([0.], c[n])))v[n] = -dnp.cumsum(v)

Executing this code returns the result array([ 1., 2., 3., 0., 1., 2., 3., 4., 0., 1.]). This solution will only be as valid as the original one, but maybe it will help you come up with something better if it isn't sufficient for your purposes.


Even more pandas-onic way to do it:

v = pd.Series([1., 3., 1., np.nan, 1., 1., 1., 1., np.nan, 1.])cumsum = v.cumsum().fillna(method='pad')reset = -cumsum[v.isnull()].diff().fillna(cumsum)result = v.where(v.notnull(), reset).cumsum()

Contrary to the matlab code, this also works for values different from 1.


Here's a slightly more pandas-onic way to do it:

v = Series([1, 1, 1, nan, 1, 1, 1, 1, nan, 1], dtype=float)n = v.isnull()a = ~nc = a.cumsum()index = c[n].index  # need the index for reconstruction after the np.diffd = Series(np.diff(np.hstack(([0.], c[n]))), index=index)v[n] = -dresult = v.cumsum()

Note that either of these requires that you're using pandas at least at 9da899b or newer. If you aren't then you can cast the bool dtype to an int64 or float64 dtype:

v = Series([1, 1, 1, nan, 1, 1, 1, 1, nan, 1], dtype=float)n = v.isnull()a = ~nc = a.astype(float).cumsum()index = c[n].index  # need the index for reconstruction after the np.diffd = Series(np.diff(np.hstack(([0.], c[n]))), index=index)v[n] = -dresult = v.cumsum()