def ts_bin_states(df, column):
    """
    Binning of states that last for several samples. Basically, it is a
    'state_changed' counter. E.g.:
    Index:     | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
    State:     | a | a | a | a | b | b | b | a | a |
    State_bin: | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 2 | 2 |

    Parameters:
    -----------
    df : DataFrame
        Pandas DataFrame.
    column : str
        Name of column with states to be binned.

    Returns:
    -----------
    df : DataFrame
        Updated DataFrame
    """
    df[column+'_bin'] = 0
    states = list(df[column].unique())
    last_st = None
    st_bin = -1
    for index, row in df.iterrows():
        curr_st = row[column]
        if curr_st == last_st:
            df.loc[index,column+'_bin'] = st_bin
        else:
            st_bin += 1
            df.loc[index,column+'_bin'] = st_bin
            last_st = curr_st
    return df