def annualize_rets(r):
    '\n    Annualizes a set of returns\n    We should infer the periods per year\n    but that is currently left as an exercise\n    to the reader :-)\n    '
    r_valid = r[((r != 0) & r.notnull())]
    date_beg = r_valid.agg((lambda x: x.first_valid_index()))
    date_end = r_valid.agg((lambda x: x.last_valid_index()))
    try:
        years_fraction = ((date_end - date_beg).dt.days / 365.2425)
    except:
        years_fraction = ((date_end - date_beg).days / 365.2425)
    compounded_growth = (1 + r_valid).prod()
    return ((compounded_growth ** (1 / years_fraction)) - 1)
Annualizes a set of returns
We should infer the periods per year
but that is currently left as an exercise
to the reader :-)
def annualize_rets(r):
    '\n    Annualizes a set of returns\n    We should infer the periods per year\n    but that is currently left as an exercise\n    to the reader :-)\n    '
    r_valid = r[((r != 0) & r.notnull())]
    date_beg = r_valid.agg((lambda x: x.first_valid_index()))
    date_end = r_valid.agg((lambda x: x.last_valid_index()))
    try:
        years_fraction = ((date_end - date_beg).dt.days / 365.2425)
    except:
        years_fraction = ((date_end - date_beg).days / 365.2425)
    compounded_growth = (1 + r_valid).prod()
    return ((compounded_growth ** (1 / years_fraction)) - 1)