rec_start = (df['signal'] == 1) & (df['signal'].shift(-1) == 1)
rec_end = (df['signal'] == 0) & (df['signal'].shift(-1) == 0)
df.loc[rec_start, 'recession'] = True
df.loc[rec_end, 'recession'] = False
df['recession'] = df['recession'].ffill().fillna(False)
Pandas: flag consecutive values