def _cut_series_by_rank(df, ts_settings, n=1, top=True):
    """
    Select top-n or bottom-n series by rank
    
    df: pandas df
    ts_settings: dict
        Parameters for datetime DR projects
    n: int
        number of series to select
    top: bool
        Select highest (True) or lowest series (False)
    
    Returns:
    --------
    pandas df
    """
    
    df_agg = df.groupby(ts_settings['series_id']).mean()
    selected_series_names = (
        df_agg.sort_values(by=ts_settings['target'], ascending=top).tail(n).index.values
    )

    return selected_series_names