If I have a pandas.core.series.Series named ts of either 1\'s or NaN\'s like this:
3382 NaN
3381 NaN
...
3369 NaN
3368 NaN
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.