Open quantopiancal opened 5 years ago
CC @luca-s
Thanks for reporting this @quantopiancal . You are right in saying that Alphalens assumes the list of quantiles/bins must start with 1 and increase by + 1 with no gaps. This is an assumption around the which the code has been built. There are probably only few places where this assumption is used, but I cannot list them by heart.
I believe it is not a big issue since it is possible to pre-process the input data to make it suitable for Alphalens, but it is never nice to have assumptions in the code. If you like to provide a PR to relax this constraint it would certainly be very welcome.
Hi @luca-s - Changing line 415 in alphalens.tears.create_turnover_tear_sheet
from:
quantile_turnover = \
{p: pd.concat([perf.quantile_turnover(quantile_factor, q, p)
for q in range(1, int(quantile_factor.max()) + 1)],
axis=1)
for p in turnover_periods}
to
quantile_turnover = \
{p: pd.concat([perf.quantile_turnover(quantile_factor, q, p)
for q in quantile_factor.sort_values().unique().tolist()],
axis=1)
for p in turnover_periods}
relaxes the constraint being discussed here. It's worth noting that the TypeError @quantopiancal mentioned in the original post is tied to an empty series being presented for empty quantiles when working with pandas 0.18.1 (and potentially older versions). I've tested with pandas >= 0.20.3
and the tearsheets run without the modification mentioned above.
I'll submit a PR with this change as a next step.
Problem Description
If you try to pass a dataframe with a non-continuous list of ints (that starts with 1) in the
factor_quantile
column tocreate_turnover_tearsheet()
, you will get the following error:TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'Timedelta'
Also, the list of ints must start with 1 and increase by + 1. For example, if
factor_data[factor_quantile].unique()
is [1,2,3,4,5], you're good to go. but iffactor_data[factor_quantile].unique()
is [1,3,4,5], or [2,3,4,5] it will return the error.I believe this is caused by Alphalens looking for a continuous list (starting with 1) because of the range statement on this line: https://github.com/quantopian/alphalens/blob/master/alphalens/tears.py#L415
Please provide a minimal, self-contained, and reproducible example:
Please provide the full traceback:
Python / Alphalens versions are whatever the Quantopian research platform is running on as of March 21st 2019.