def _get_twr(x, annualize=False, ann_tday=253):
    """Get Time-Weighted Return (TWR)

    https://en.wikipedia.org/wiki/Time-weighted_return#Time-weighted_return_compensating_for_external_flows

    Make sure the DataFrame `x` is sorted by date (ascending; oldest at the top).
        Presumably `x` has 'date' in its index, but this isn't
        strictly necessary (just needs to be sorted).

    Args:
        x (DataFrame): a DataFrame with columns of 'tday' and 'hpr';
            expected as a time series
        annualize (bool, optional): If True, annualizes the TWR. Defaults to False.
        ann_tday (int, optional): Number of (trading) days to use in
            annualization calc. Defaults to 253.

    Returns:
        DataFrame: a pandas DataFrame with a column of 'twr'
    """
    tday_elapsed = x.tday.diff(1).sum()
    twr = x.agg({'hpr':'prod'})
    twr.rename({'hpr':'twr'}, inplace=True) # after agg product, hpr is essentially twr
    if annualize:
        twr['twr'] = (twr.twr**(1/tday_elapsed))**ann_tday

    return twr