Heerozh / spectre

GPU-accelerated Factors analysis library and Backtester
GNU General Public License v3.0
627 stars 108 forks source link

groupby with a column name #5

Closed rajach closed 4 years ago

rajach commented 4 years ago

Calling demean with a groupby column name gives error. Is this the right way to use it?

sec_fac = factors.AssetClassifierDataFactor(sec, default=-1)
engine.add(sec_fac, 'sector')
#engine.add(factors.Returns(win=252).demean(groupby='sector').rank().zscore(), 'momentum')
df = engine.run('2015-01-11', '2019-01-15')
df
gives: date asset close sector
2015-01-13 00:00:00+00:00 A 40.11 2.0
AA 48.51 1.0
.... ..... .....

Uncommenting the momentum factor (middle line) above gives:

--------------------------------------------------------------------------- KeyError Traceback (most recent call last) ..\spectre_env\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2645 try: -> 2646 return self._engine.get_loc(key) 2647 except KeyError: pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'sector' During handling of the above exception, another exception occurred: ......

Heerozh commented 4 years ago

Try this:

engine.add(factors.Returns(win=252).demean(groupby=sec).rank().zscore(), 'momentum')

Currently groupby does not support factor data.

rajach commented 4 years ago

Thank you. To avoid the value error I used: {asst:sec.get(asst, -1) for asst in df.index.get_level_values('asset').unique() }