Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trouble with basic Pyfolio Tearsheet

Hi.

I've been going mad trying to get daily returns of a backtest I have into Pyfolio. No matter which way I twist the data it seems to be throwing an error. Using the code,

import pyfolio as pf  
import pandas as pd

returns = pd.read_csv('returnsTZ2.txt')  
returns.columns = [ 'date', 'return']  
returns['date']  = pd.to_datetime(returns['date'])  
print(returns)  
returns = returns.set_index(['date'])

pf.create_returns_tear_sheet(returns, live_start_date='2016-6-6')

2014-12-08 01:00:00  0.000831  
2014-12-09 01:00:00  0.043870  
2014-12-10 01:00:00 -0.002749  
2014-12-11 01:00:00 -0.009615  
2014-12-12 01:00:00 -0.021883  
2014-12-15 01:00:00 -0.017754  
2014-12-16 01:00:00 -0.002320  

I'm getting this error,

TypeError: Cannot compare tz-naive and tz-aware timestamps

Then changing the date data with this code,

import pyfolio as pf  
import pandas as pd


filename = 'returnsTZ2.txt'  
returns = pd.read_csv(filename,sep=',')  
returns.columns = [ 'date', 'return']  
returns['date']  = pd.to_datetime(returns['date'],format='%Y-%m-%d %H:%M:%S')#,utc=None)  
returns = returns.set_index(['date'])  
returns.index = pd.to_datetime(returns.index)  
returns.index  = returns.index.tz_localize('GMT')

pf.create_returns_tear_sheet(returns, live_start_date='2016-6-6')

2014-12-11 01:00:00+00:00 -0.009615  
2014-12-12 01:00:00+00:00 -0.021883  
2014-12-15 01:00:00+00:00 -0.017754  
2014-12-16 01:00:00+00:00 -0.002320  
2014-12-17 01:00:00+00:00  0.008124  
2014-12-18 01:00:00+00:00  0.016539  
2014-12-19 01:00:00+00:00  0.002284  
2014-12-22 01:00:00+00:00 -0.013383  

unsupported operand type(s) for +: 'int' and 'str'

Anyone have any suggestions as to where I'm going wrong. I have been able to run the tear sheet example here http://quantopian.github.io/pyfolio/notebooks/single_stock_example/

3 responses

Asking the oracle and turning on the lights ...
a) Follow this pattern: https://www.google.com/search?q=tz-naive+and+tz-aware+timestamps+site%3Astackoverflow.com
b) Click a line number in teh margin where a problem is, to set a debug breakpoint, run, and in the console window that appears, at the prompt, try variations on the command, type variable names to look at their contents, set variable to different values, even run functions, and use the watch window sometimes.

Hmmm thanks been down that path but still no progress. I was assuming that pyfolio was wanting tz-aware timestamps. Now I'm thinking that it's actually the reverse. In that case, can anyone confirm that I should be feeding pyfolio tz-naive timestamps?

So made some progress. Though still far from the mark. Instead of passing a dataframe I pass a Series to pf.create_returns_tear_sheet it will get some way through the process but then throw an error after the stats section. So this code,

import pandas as pd  
import pyfolio as pf  
import pytz  
results= pd.Series.from_csv('returns.txt',header=0)  
pf.create_returns_tear_sheet(results.tz_localize(pytz.utc),live_start_date='2015-12-1')  

Will only get me to here!!!,
```

Out-of-Sample Months: 16
Backtest Months: 23
Performance statistics All history Backtest Out of sample
annual_return 2.83 3.15 2.43
cum_returns_final 82.12 14.31 4.43
annual_volatility 0.18 0.19 0.17
sharpe_ratio 7.56 7.77 7.27
calmar_ratio 36.29 40.36 34.27
stability_of_timeseries 1.00 0.99 0.98
max_drawdown -0.08 -0.08 -0.07
omega_ratio 3.79 3.81 3.75
sortino_ratio 16.07 15.76 16.82
skew 0.30 -0.02 0.84
kurtosis 2.42 1.97 3.33
tail_ratio 1.84 1.77 1.82
common_sense_ratio 7.03 7.36 6.25
information_ratio 0.00 0.00 0.00
alpha nan nan nan
beta nan nan nan
Traceback (most recent call last):

File "", line 1, in
runfile('C:/Users/jason/Documents/Code/PythonCode/Zipline/pract.py', wdir='C:/Users/jason/Documents/Code/PythonCode/Zipline')

File "C:\Users\jason\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)

File "C:\Users\jason\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/jason/Documents/Code/PythonCode/Zipline/pract.py", line 32, in
pf.create_returns_tear_sheet(returns.tz_localize(pytz.utc),live_start_date='2015-12-1')

File "C:\Users\jason\Anaconda3\lib\site-packages\pyfolio\plotting.py", line 56, in call_w_context
return func(*args, **kwargs)

File "C:\Users\jason\Anaconda3\lib\site-packages\pyfolio\tears.py", line 331, in create_returns_tear_sheet
ax=ax_rolling_returns_vol_match)

File "C:\Users\jason\Anaconda3\lib\site-packages\pyfolio\plotting.py", line 771, in plot_rolling_returns
bmark_vol = factor_returns.loc[returns.index].std()

File "C:\Users\jason\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1312, in getitem
return self._getitem_axis(key, axis=0)

File "C:\Users\jason\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1472, in getitem_axis
return self._getitem
iterable(key, axis=axis)

File "C:\Users\jason\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1036, in getitem_iterable
self.
has_valid_type(key, axis)

File "C:\Users\jason\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1395, in has_valid_type
(key, self.obj.
get_axis_name(axis)))

KeyError: "None of [DatetimeIndex(['2014-01-23 01:00:00+00:00', '2014-01-24 01:00:00+00:00',\n '2014-01-27 01:00:00+00:00', '2014-01-28 01:00:00+00:00',\n '2014-01-29 01:00:00+00:00', '2014-01-30 01:00:00+00:00',\n '2014-01-31 01:00:00+00:00', '2014-02-03 01:00:00+00:00',\n '2014-02-04 01:00:00+00:00', '2014-02-05 01:00:00+00:00',\n ...\n '2017-03-15 01:00:00+00:00', '2017-03-16 01:00:00+00:00',\n '2017-03-17 01:00:00+00:00', '2017-03-20 01:00:00+00:00',\n '2017-03-21 01:00:00+00:00', '2017-03-22 01:00:00+00:00',\n '2017-03-23 01:00:00+00:00', '2017-03-24 01:00:00+00:00',\n '2017-03-27 01:00:00+00:00', '2017-03-28 01:00:00+00:00'],\n dtype='datetime64[ns, UTC]', name='date', length=829, freq=None)] are in the [index]"
```