quantopian / alphalens

Performance analysis of predictive (alpha) stock factors
http://quantopian.github.io/alphalens
Apache License 2.0
3.29k stars 1.14k forks source link

Problem:create_event_returns_tear_sheet #389

Open RemnantLi opened 3 years ago

RemnantLi commented 3 years ago

Problem Description

When I use the alphalens.tears.create_event_returns_tear_sheet, it shows one error: unsupported operand type(s) for -: 'slice' and 'int', besides other functions work well. Looking forwards someone could help me. Thank you.

Please provide a minimal, self-contained, and reproducible example:


[Paste code here]
**alphalens.tears.create_event_returns_tear_sheet(data,
                                                returns,
                                                avgretplot=(5, 15),
                                                long_short=True,
                                                group_neutral=False,
                                                std_bar=True,
                                                by_group=False)**

**Please provide the full traceback:**
```python
[Paste traceback here]
```---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
D:\Anaconda\lib\site-packages\pandas\core\groupby\groupby.py in apply(self, func, *args, **kwargs)
    734             try:
--> 735                 result = self._python_apply_general(f)
    736             except TypeError:

D:\Anaconda\lib\site-packages\pandas\core\groupby\groupby.py in _python_apply_general(self, f)
    750     def _python_apply_general(self, f):
--> 751         keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)
    752 

D:\Anaconda\lib\site-packages\pandas\core\groupby\ops.py in apply(self, f, data, axis)
    205             group_axes = group.axes
--> 206             res = f(group)
    207             if not _is_indexed_like(res, group_axes):

D:\Anaconda\lib\site-packages\pandas\core\groupby\groupby.py in f(g)
    718                     with np.errstate(all="ignore"):
--> 719                         return func(g, *args, **kwargs)
    720 

D:\Anaconda\lib\site-packages\alphalens\performance.py in average_cumulative_return(q_fact, demean_by)
    802     def average_cumulative_return(q_fact, demean_by):
--> 803         q_returns = cumulative_return_around_event(q_fact, demean_by)
    804         q_returns.replace([np.inf, -np.inf], np.nan, inplace=True)

D:\Anaconda\lib\site-packages\alphalens\performance.py in cumulative_return_around_event(q_fact, demean_by)
    798             mean_by_date=True,
--> 799             demean_by=demean_by,
    800         )

D:\Anaconda\lib\site-packages\alphalens\performance.py in common_start_returns(factor, returns, before, after, cumulative, mean_by_date, demean_by)
    701 
--> 702         starting_index = max(day_zero_index - before, 0)
    703         ending_index = min(day_zero_index + after + 1,

TypeError: unsupported operand type(s) for -: 'slice' and 'int'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-46-6fc348201897> in <module>
      5                                                 group_neutral=False,
      6                                                 std_bar=True,
----> 7                                                 by_group=False)

D:\Anaconda\lib\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)

D:\Anaconda\lib\site-packages\alphalens\tears.py in create_event_returns_tear_sheet(factor_data, returns, avgretplot, long_short, group_neutral, std_bar, by_group)
    573         periods_after=after,
    574         demeaned=long_short,
--> 575         group_adjust=group_neutral,
    576     )
    577 

D:\Anaconda\lib\site-packages\alphalens\performance.py in average_cumulative_return_by_quantile(factor_data, returns, periods_before, periods_after, demeaned, group_adjust, by_group)
    858         elif demeaned:
    859             fq = factor_data['factor_quantile']
--> 860             return fq.groupby(fq).apply(average_cumulative_return, fq)
    861         else:
    862             fq = factor_data['factor_quantile']

D:\Anaconda\lib\site-packages\pandas\core\groupby\generic.py in apply(self, func, *args, **kwargs)
    222     )
    223     def apply(self, func, *args, **kwargs):
--> 224         return super().apply(func, *args, **kwargs)
    225 
    226     @Substitution(

D:\Anaconda\lib\site-packages\pandas\core\groupby\groupby.py in apply(self, func, *args, **kwargs)
    744 
    745                 with _group_selection_context(self):
--> 746                     return self._python_apply_general(f)
    747 
    748         return result

D:\Anaconda\lib\site-packages\pandas\core\groupby\groupby.py in _python_apply_general(self, f)
    749 
    750     def _python_apply_general(self, f):
--> 751         keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)
    752 
    753         return self._wrap_applied_output(

D:\Anaconda\lib\site-packages\pandas\core\groupby\ops.py in apply(self, f, data, axis)
    204             # group might be modified
    205             group_axes = group.axes
--> 206             res = f(group)
    207             if not _is_indexed_like(res, group_axes):
    208                 mutated = True

D:\Anaconda\lib\site-packages\pandas\core\groupby\groupby.py in f(g)
    717                 def f(g):
    718                     with np.errstate(all="ignore"):
--> 719                         return func(g, *args, **kwargs)
    720 
    721             elif hasattr(nanops, "nan" + func):

D:\Anaconda\lib\site-packages\alphalens\performance.py in average_cumulative_return(q_fact, demean_by)
    801 
    802     def average_cumulative_return(q_fact, demean_by):
--> 803         q_returns = cumulative_return_around_event(q_fact, demean_by)
    804         q_returns.replace([np.inf, -np.inf], np.nan, inplace=True)
    805 

D:\Anaconda\lib\site-packages\alphalens\performance.py in cumulative_return_around_event(q_fact, demean_by)
    797             cumulative=True,
    798             mean_by_date=True,
--> 799             demean_by=demean_by,
    800         )
    801 

D:\Anaconda\lib\site-packages\alphalens\performance.py in common_start_returns(factor, returns, before, after, cumulative, mean_by_date, demean_by)
    700             continue
    701 
--> 702         starting_index = max(day_zero_index - before, 0)
    703         ending_index = min(day_zero_index + after + 1,
    704                            len(returns.index))

TypeError: unsupported operand type(s) for -: 'slice' and 'int'

<Figure size 432x288 with 0 Axes>
​

**Please provide any additional information below:**

## Versions

* Alphalens version: 0.4.0
* Python version: 3.7.6
* Pandas version: 1.0.1
* Matplotlib version: 3.1.3