def timepoints(df, second='first'):
    """Extract timepoints from a dataframe.

    Parameters
    ----------
    df : pd.DataFrame
        This dataframe will be used as a base.

    second : {'first' or 'last'}, default 'first'
        'last' to have the latest, 'first' to have the earliest values
        for timepoint 2.

    """
    index = ['Subject ID', 'Image ID']
    df.reset_index(inplace=True)
    df.set_index(index, inplace=True)
    df.sort_index(inplace=True)
    if 'index' in df.columns:
        df = df.drop(columns='index')
    if 'Description' in df.columns:
        raise ValueError(
            "Make sure that 'Description' is not in columns and only one image per timepoint is in the pd.DataFrame."
            )
    df_subjects = df.index.get_level_values(0)
    df_images = df.index.get_level_values(1)
    timepoints = {}
    if second == 'first':
        total_timepoints = max(df_subjects.value_counts())
        for i in range(total_timepoints):
            timepoint = i + 1
            timepoint_df = df[~df_subjects.duplicated(keep='first')]
            timepoint_str = 'Timepoint ' + str(timepoint)
            timepoints[timepoint_str] = timepoint_df
            df = df[~df_images.isin(timepoint_df.index.get_level_values(1))]
            df_subjects = df.index.get_level_values(0)
            df_images = df.index.get_level_values(1)
    elif second == 'last':
        timepoint_1 = df[~df_subjects.duplicated()]
        timepoints['Timepoint 1'] = timepoint_1
        timepoint_1_images = timepoint_1.index.get_level_values(1)
        after_timepoint_1 = df[~df_images.isin(timepoint_1_images)]
        after_timepoint_1_images = after_timepoint_1.index.get_level_values(0)
        timepoint_2_last = after_timepoint_1[~after_timepoint_1_images.
            duplicated(keep='last')]
        timepoints['Timepoint 2'] = timepoint_2_last
    return timepoints
