Back to Community
Help: Using pyfolio with zipline in a notebook?

I've been trying to use the new pyfolio analysis capability with a zipline-run backtest within a research notebook, but I can't figure out how to pass the zipline results to pyfolio.

I'm running my algo in zipline the way it's shown in the zipline tutorial:

algo_obj = TradingAlgorithm(  

algo_obj._analyze = analyze  
perf_manual =,1,0))  

... then I'm attempting to pass the result data to pyfolio:

import pyfolio as pf  
import pytz  

which results in this error:

KeyError                                  Traceback (most recent call last)  
<ipython-input-87-723363adf35d> in <module>()  
      1 import pyfolio as pf  
      2 import pytz  
----> 3 pf.create_full_tear_sheet(perf_manual.returns.tz_localize(pytz.utc))  
 [...]  (let me know if you need the intermediate trace)  
/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.pyc in _has_valid_type(self, key, axis)
   1261                 raise KeyError("None of [%s] are in the [%s]" %  
-> 1262                                (key, self.obj._get_axis_name(axis)))  
   1264             return True

KeyError: "None of [DatetimeIndex(['2010-01-04 21:00:00+00:00', '2010-01-05 21:00:00+00:00',\n               '2010-01-06 21:00:00+00:00', '2010-01-07 21:00:00+00:00',\n               '2010-01-08 21:00:00+00:00', '2010-01-11 21:00:00+00:00',\n               '2010-01-12 21:00:00+00:00', '2010-01-13 21:00:00+00:00',\n               '2010-01-14 21:00:00+00:00', '2010-01-15 21:00:00+00:00', \n               ...\n               '2013-12-17 21:00:00+00:00', '2013-12-18 21:00:00+00:00',\n               '2013-12-19 21:00:00+00:00', '2013-12-20 21:00:00+00:00',\n               '2013-12-23 21:00:00+00:00', '2013-12-24 18:00:00+00:00',\n               '2013-12-26 21:00:00+00:00', '2013-12-27 21:00:00+00:00',\n               '2013-12-30 21:00:00+00:00', '2013-12-31 21:00:00+00:00'],\n              dtype='datetime64[ns]', length=1006, freq=None, tz='UTC')] are in the [index]"  

If I try the same thing without ".tz_localize(pytz.utc)" on the returns data I get the dreaded:

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

I also tried switching to "minute" frequency but that didn't make any difference. Am I passing the results incorrectly? Any pointers?

10 responses


Can you try: returns, positions, transactions, gross_lev = pf.utils.extract_rets_pos_txn_from_zipline(perf_manual)

And then passing those returns to pyfolio instead?

If that doesn't work, it might be a bug we recently fixed but that hasn't made it onto the pyfolio version installed on Quantopian. We'll do the update soon and I'll let you know.



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.

Okay, removed the pytz bit and tried it:

import pyfolio as pf  
returns, positions, transactions, gross_lev = pf.utils.extract_rets_pos_txn_from_zipline(perf_manual)  


TypeError                                 Traceback (most recent call last)  
<ipython-input-101-fd8361dea1f8> in <module>()  
      1 import pyfolio as pf  
----> 2 returns, positions, transactions, gross_lev = pf.utils.extract_rets_pos_txn_from_zipline(perf_manual)  

/usr/local/lib/python2.7/dist-packages/pyfolio/utils.pyc in extract_rets_pos_txn_from_zipline(backtest)
    367     transactions_frame = txn.make_transaction_frame(backtest.transactions)  
    368     transactions = txn.get_txn_vol(transactions_frame)  
--> 369     transactions.index = transactions.index.normalize().tz_localize('utc')  
    371     return returns, positions, transactions, gross_lev

/usr/local/lib/python2.7/dist-packages/pandas/util/decorators.pyc in wrapper(*args, **kwargs)
     86                 else:  
     87                     kwargs[new_arg_name] = new_arg_value  
---> 88             return func(*args, **kwargs)  
     89         return wrapper  
     90     return _deprecate_kwarg

/usr/local/lib/python2.7/dist-packages/pandas/tseries/index.pyc in tz_localize(self, tz, ambiguous)
   1638                 new_dates = tslib.tz_convert(self.asi8, 'UTC',  
   1639             else:  
-> 1640                 raise TypeError("Already tz-aware, use tz_convert to convert.")  
   1641         else:  
   1642             tz = tslib.maybe_get_tz(tz)

TypeError: Already tz-aware, use tz_convert to convert.  

Then I added tz_convert(pytz.utc) to perf_manual for giggles, but that doesn't seem to be where the problem is as it threw the same error.

Okay, we'll update pyfolio and hopefully that fixes things.

Sorry for the problems you're encountering. I was hoping the pyfolio update would fix things but apparently it didn't. You can track progress of this bug here:

It seems like pyfolio was not updated before. Can someone retry and let me know if the problem persists?

Yes, I'm getting a different error now. The tz issue seems to be resolved, but I also get the "TypeError: cannot concatenate 'str' and 'zipline.assets._assets.Equity' objects" error that Derek's notebook shows above.

I'm now getting TypeError: Empty 'DataFrame': no numeric data to plot

Loading notebook preview...


The problem is in this line:

pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions,  
                          gross_lev=gross_lev, live_start_date='2000-01-01')  

You're setting the live_start_date to be earlier than the backtest started. Thus, your backtest period is 0 and there is no data to display. Try setting it to e.g. 2014-01-01 or whenever that algorithm was posted, or you can just remove that kwarg all-together.

Thanks Thomas. I understand
live_start_date better now. I made the fix and your suggestion works. However, if I want to get the bayesan_tear_sheet I run into a problem in plotting the annual Alpha and Beta:

ValueError: On entry to DLASCL parameter number 4 had an illegal value

although, interestingly, the problem does not exist if I run the full tear sheet. Example attached.

Loading notebook preview...

Thanks Paul, I have seen that error before and will investigate.