import warnings


def _ros_sort(df, result, censorship):
    """
    This function prepares a dataframe for ROS. It sorts ascending with
    left-censored observations on top. Censored results larger than the
    maximum uncensored results are removed from the dataframe.
    Parameters
    ----------
    df : pandas.DataFrame
    result : str
        Name of the column in the dataframe that contains observed
        values. Censored values should be set to the detection (upper)
        limit.
    censorship : str
        Name of the column in the dataframe that indicates that a
        result is left-censored. (i.e., True -> censored,
        False -> uncensored)
    Returns
    ------
    sorted_df : pandas.DataFrame
        The sorted dataframe with all columns dropped except the
        result and censorship columns.
    """

    # separate uncensored data from censored data
    max_uncensored = df.loc[~df[censorship], result].max()
    if (df.loc[df[censorship], result] > max_uncensored).any():
        msg = (
            "Dropping censored results greater than "
            "the max uncensored result."
        )
        warnings.warn(msg)

    df_sorted = (
        df[[censorship, result]]
            .sort_values(by=[censorship, result], ascending=[False, True])
            .where(lambda df:
                   (~df[censorship]) |  # uncensored values
                   ((df[result] < max_uncensored) & df[censorship])  # censored values < max_uncen
                   )
            .dropna(how='all')
            .reset_index(drop=True)
            .assign(**{censorship: lambda df: df[censorship].astype(bool)})
    )
    return df_sorted[[result, censorship]]