If I have a pandas.core.series.Series named ts of either 1\'s or NaN\'s like this:
pandas.core.series.Series
ts
3382 NaN 3381 NaN ... 3369 NaN 3368 NaN
If you can accept a similar boolean Series b, try
b
(b.cumsum() - b.cumsum().where(~b).fillna(method='pad').fillna(0)).astype(int)
Starting from your Series ts, either b = (ts == 1) or b = ~ts.isnull().
b = (ts == 1)
b = ~ts.isnull()