def _portfolio_difference(df, portvar, long, short, byvars=None, retvars=None):
    """
    Calculates long portfolio minus short portfolio
    """
    if byvars:
        out = df[df[portvar] == long].set_index(byvars) - df[df[portvar] ==
            short].set_index(byvars)
    else:
        out = df[df[portvar] == long] - df[df[portvar] == short]
    if retvars:
        return out[retvars]
    else:
        return out
