Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Basic tear sheet problem with rolling beta - found the cause of the error

EDIT 2017-04-24:
I actually managed to find what was causing the error. It was this piece of code used in the Fama-French plot:

def get_utc_timestamp(dt):  
    dt = pd.to_datetime(dt)  
    try:  
        dt = dt.tz_localize('UTC')  
    except TypeError:  
        dt = dt.tz_convert('UTC')  
    return dt  

It seems it was giving the time series for the frisk factors in the Fama-French plot time zones, which was not compatible with the other time series thus causing the error.

The simple solution was to comment out the whole plotting.plot_rolling_fama_french() call and the tear sheet appears.

END EDIT


Hello everyone,

I'm new to pyfolio and this community. Thank you for all the great resources and ideas here.

What I'm trying to do is create a tear sheet using my own return series for a fund and its benchmark. Using the create_full_tear_sheet function, the first block of stats pops out nicely but there's a problem in the rolling_beta in the timeseries module. My python skills are still evolving so I'm very grateful for any input or suggestions on how to solve the problem.

Thank you for your time and effort.

/a

EDIT Added Code Sample Quotes. Thank you Blue.

Here's what I'm doing:
``` from pyfolio.tears import *
import pandas as pd

def tear_sheet(fund_ret, bench_ret):
#create_returns_tear_sheet(fund, live_start_date=datetime.date(2001, 12, 5), cone_std=(1.0, 1.5, 2.0), benchmark_rets=bench, bootstrap=False, return_fig=True)
create_full_tear_sheet(fund_ret, positions=None, transactions=None, market_data=None, benchmark_rets=bench_ret,
slippage=None, live_start_date = None, sector_mappings = None,
bayesian = False, round_trips = False, estimate_intraday = False ,
hide_positions = False, cone_std = (1.0, 1.5, 2.0), bootstrap = False,
unadjusted_returns = None, set_context = False
)
def main():
fund = pd.Series.from_csv('fund_rets.csv', sep=';', parse_dates=True, header=0)
bench = pd.Series.from_csv('bench_rets.csv',sep=';', parse_dates=True, header=0)
tear_sheet(fund, bench)

if name == 'main':
main()



**The error message is a mess but looks like this:**

Entire data start date: 2001-12-05
Entire data end date: 2017-03-30

Backtest Months: 183
Performance statistics Backtest
annual_return -0.04
cum_returns_final -0.46
annual_volatility 0.22
sharpe_ratio -0.08
calmar_ratio -0.05
stability_of_timeseries 0.46
max_drawdown -0.73
omega_ratio 0.99
sortino_ratio -0.11
skew 0.87
kurtosis 10.49
tail_ratio 1.11
common_sense_ratio 1.06
information_ratio -0.01
alpha 0.00
beta -0.16
Traceback (most recent call last):
File "[user]/PycharmProjects/pyfolio/pyfolio/main.py", line 20, in
main()
File "[user]/PycharmProjects/pyfolio/pyfolio/main.py", line 17, in main
tear_sheet(fund, bench)
File "[user]/PycharmProjects/pyfolio/pyfolio/main.py", line 11, in tear_sheet
unadjusted_returns = None, set_context = False
File "[user]\PycharmProjects\pyfolio\pyfolio\tears.py", line 170, in create_full_tear_sheet
set_context=set_context)
File "[user]\PycharmProjects\pyfolio\pyfolio\plotting.py", line 58, in call_w_context
return func(args, *kwargs)
File "[user]\PycharmProjects\pyfolio\pyfolio\tears.py", line 360, in create_returns_tear_sheet
returns, ax=ax_rolling_risk)
File "[user]\PycharmProjects\pyfolio\pyfolio\plotting.py", line 160, in plot_rolling_fama_french
rolling_window=rolling_window)
File "[user]\PycharmProjects\pyfolio\pyfolio\timeseries.py", line 632, in rolling_fama_french
rolling_window=rolling_window)
File "[user]\PycharmProjects\pyfolio\pyfolio\timeseries.py", line 587, in rolling_beta
rolling_window=rolling_window)
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4152, in apply
return self.apply_standard(f, axis, reduce=reduce)
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4248, in apply_standard
results[i] = func(v)
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4129, in f
return func(x, *args, *kwds)
File "[user]\PycharmProjects\pyfolio\pyfolio\timeseries.py", line 594, in rolling_beta
factor_returns.loc[beg:end])
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\empyrical\stats.py", line 860, in beta
return beta_aligned(aligned_series(returns, factor
returns),
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\empyrical\stats.py", line 642, in aligned_series
for col, series in iteritems(pd.concat(many_series, axis=1))]
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\tools\merge.py", line 1451, in concat
copy=copy)
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\tools\merge.py", line 1597, in _init

self.new_axes = self.get_new_axes()
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\tools\merge.py", line 1672, in get_new_axes
new_axes[i] = self.get_comb_axis(i)
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\tools\merge.py", line 1698, in get_comb_axis
return getcombinedindex(all_indexes, intersect=self.intersect)
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\indexes\api.py", line 37, in getcombined_index
union = _union_indexes(indexes)
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\indexes\api.py", line 68, in _unionindexes
return result.union
many(indexes[1:])
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\tseries\index.py", line 1024, in union
many
this, other = this._maybe_utc_convert(other)
File "[user]\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\tseries\index.py", line 1062, in _maybe_utc_convert
raise TypeError('Cannot join tz-naive with tz-aware '
TypeError: ('Cannot join tz-naive with tz-aware DatetimeIndex', 'occurred at index SMB')
```

2 responses

Glad you're using pyfolio — at first glance, it looks like either your returns or your benchmark don't have a time zone specified. In pandas, you can add a timezone to your benchmark (bench, in your case), for example, by doing bench = bench.tz_localize('utc'). You might also have to do that with returns, positions, and transactions.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thank you Gus. I am learning bit by bit.

This was indeed the solution. I set both my series (fund and bench) to add time zones.