Given the following pandas data frame (a copy of it can by found here). How to fill na in a separate column with incrementing/decrementing nr of rows until the next sig
The usual approach to signal-based groupings (which we should really have better native support for, IMHO) to use the compare-cumsum-groupby pattern. Here the comparison is to determine whether a signal entry is null or not, after which we do a cumulative sum so that each signal group has its own id (group id, or gid). The rest is just arithmetic.
While there's some duplication here we could refactor away, I'm feeling lazy, and so:
gid = df["Signal"].notnull().cumsum()
dg = df.groupby(gid)
sign = dg["Signal"].transform("first")
df["forward signal rows"] = (dg.cumcount() + 1) * sign
df["backward signal rows"] = (dg["Signal"].transform("size") - dg.cumcount()) * sign
df["value at signal"] = dg["Values"].transform("last")
df.loc[gid == 0, "value at signal"] = np.nan
gives me a frame matching your target one.