Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help with KeyError when trying to generate a tear sheet

Hi Everyone
Im hoping someone is able to help me debug an issue I have because I dont understand why its happening. Im a noob using quantopian, so I am just trying to go through some examples provided on this site.

I am trying to run this code:

from alphalens.tears import create_full_tear_sheet

create_full_tear_sheet(df)  

and I keep getting this error message

---------------------------------------------------------------------------  
KeyError                                  Traceback (most recent call last)  
/venvs/py35/lib/python3.5/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)
   1944             try:  
-> 1945                 return self._engine.get_loc(key)  
   1946             except KeyError:

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)()

KeyError: 'factor_quantile'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)  
<ipython-input-25-94ca3d496549> in <module>()  
      1 from alphalens.tears import create_full_tear_sheet  
      2  
----> 3 create_full_tear_sheet(df)

/venvs/py35/lib/python3.5/site-packages/alphalens/plotting.py in call_w_context(*args, **kwargs)
     43             with plotting_context(), axes_style(), color_palette:  
     44                 sns.despine(left=True)  
---> 45                 return func(*args, **kwargs)  
     46         else:  
     47             return func(*args, **kwargs)

/venvs/py35/lib/python3.5/site-packages/alphalens/tears.py in create_full_tear_sheet(factor_data, long_short, group_neutral, by_group)
    478     """  
    479  
--> 480     plotting.plot_quantile_statistics_table(factor_data)  
    481     create_returns_tear_sheet(factor_data,  
    482                               long_short,

/venvs/py35/lib/python3.5/site-packages/alphalens/plotting.py in plot_quantile_statistics_table(factor_data)
    181  
    182 def plot_quantile_statistics_table(factor_data):  
--> 183     quantile_stats = factor_data.groupby('factor_quantile') \  
    184         .agg(['min', 'max', 'mean', 'std', 'count'])['factor']  
    185     quantile_stats['count %'] = quantile_stats['count'] \

/venvs/py35/lib/python3.5/site-packages/pandas/core/generic.py in groupby(self, by, axis, level, as_index, sort, group_keys, squeeze, **kwargs)
   3776         return groupby(self, by=by, axis=axis, level=level, as_index=as_index,  
   3777                        sort=sort, group_keys=group_keys, squeeze=squeeze,  
-> 3778                        **kwargs)  
   3779  
   3780     def asfreq(self, freq, method=None, how=None, normalize=False):

/venvs/py35/lib/python3.5/site-packages/pandas/core/groupby.py in groupby(obj, by, **kwds)
   1425         raise TypeError('invalid type: %s' % type(obj))  
   1426  
-> 1427     return klass(obj, by, **kwds)  
   1428  
   1429 

/venvs/py35/lib/python3.5/site-packages/pandas/core/groupby.py in __init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze, **kwargs)
    352                                                     level=level,  
    353                                                     sort=sort,  
--> 354                                                     mutated=self.mutated)  
    355  
    356         self.obj = obj

/venvs/py35/lib/python3.5/site-packages/pandas/core/groupby.py in _get_grouper(obj, key, axis, level, sort, mutated)
   2381  
   2382         elif is_in_axis(gpr):  # df.groupby('name')  
-> 2383             in_axis, name, gpr = True, gpr, obj[gpr]  
   2384             exclusions.append(name)  
   2385 

/venvs/py35/lib/python3.5/site-packages/pandas/core/frame.py in __getitem__(self, key)
   1995             return self._getitem_multilevel(key)  
   1996         else:  
-> 1997             return self._getitem_column(key)  
   1998  
   1999     def _getitem_column(self, key):

/venvs/py35/lib/python3.5/site-packages/pandas/core/frame.py in _getitem_column(self, key)
   2002         # get column  
   2003         if self.columns.is_unique:  
-> 2004             return self._get_item_cache(key)  
   2005  
   2006         # duplicate columns & possible reduce dimensionality

/venvs/py35/lib/python3.5/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
   1348         res = cache.get(item)  
   1349         if res is None:  
-> 1350             values = self._data.get(item)  
   1351             res = self._box_item_values(item, values)  
   1352             cache[item] = res

/venvs/py35/lib/python3.5/site-packages/pandas/core/internals.py in get(self, item, fastpath)
   3288  
   3289             if not isnull(item):  
-> 3290                 loc = self.items.get_loc(item)  
   3291             else:  
   3292                 indexer = np.arange(len(self.items))[isnull(self.items)]

/venvs/py35/lib/python3.5/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)
   1945                 return self._engine.get_loc(key)  
   1946             except KeyError:  
-> 1947                 return self._engine.get_loc(self._maybe_cast_indexer(key))  
   1948  
   1949         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)()

KeyError: 'factor_quantile'