df = pd.DataFrame([2, 2, 2, 1, 1, 1, 2, 2, 2, 3, 2, 2, 1, 1], index=pd.
    date_range('00:00', freq=freq, periods=14))
df['is_first'] = pd.rolling_apply(df, 2, lambda x: x[0] != x[1]).fillna(1)
Calculate the duration of a state with a pandas Dataframe