def slicex(df, values, keep_margins=True):
    """
    Return an index-wise slice of df, keeping margins if desired.

    Assuming a Quantipy-style view result this function takes an index
    slice of df as indicated by values and returns the result.

    Parameters
    ----------
    df : pandas.DataFrame
        The dataframe that should be sliced along the index.
    values : list-like
        A list of index values that should be sliced from df.
    keep_margins : bool, default=True
        If True and the margins index row exists, it will be kept.

    Returns
    -------
    df : list
        The sliced dataframe.
    """

    # If the index is from a frequency then the rule
    # should be skipped
    if df.index.levels[1][0]=='@':
        return df

    name_x = df.index.levels[0][0]
    slicer = [(name_x, value) for value in values]
    if keep_margins and (name_x, 'All') in df.index:
        slicer = [(name_x, 'All')] + slicer

    df = df.loc[slicer]

    return df