ranaroussi / quantstats

Portfolio analytics for quants, written in Python
Apache License 2.0
4.59k stars 809 forks source link

Deprecated code in stats.py expected_return() #335

Open kartiksubbarao opened 5 months ago

kartiksubbarao commented 5 months ago

The expected_return() function in stats.py has deprecated code, which causes the following warning when generating a tearsheet with a benchmark:

/path/to/python/lib/python3.12/site-packages/numpy/core/fromnumeric.py:86: FutureWarning: The behavior of DataFrame.prod with axis=None is deprecated, in a future version this will reduce over both axes and return a scalar. To retain the old behavior, pass axis=0 (or do not pass axis)
  return reduction(axis=axis, out=out, **passkwargs)

Here is the deprecated code:

return _np.product(1 + returns) ** (1 / len(returns)) - 1

The corrected version is this:

return _np.prod(1 + returns, axis=0) ** (1 / len(returns)) - 1

In addition to explicitly specifying axis=0 to fix the FutureWarning, it also changes _np.product to _np.prod to fix another deprecation issue: https://github.com/numpy/numpy/blob/maintenance/1.26.x/numpy/core/fromnumeric.py#L3837-L3839

I will submit a pull request.