two-liner:
cumsum = ts.cumsum().ffill() (pd.Series(np.where(ts.isna(), -cumsum, np.nan)).ffill().fillna(0) + cumsum).fillna(0)