JoinQuant / jqfactor_analyzer

聚宽单因子分析工具
MIT License
504 stars 184 forks source link

官方demo跑不通,报错AssertionError: Length of new_levels (3) must be <= self.nlevels (2) #12

Open sunshe35 opened 5 months ago

sunshe35 commented 5 months ago

使用官方demo:

# 载入函数库
import pandas as pd
import jqfactor_analyzer as ja

# 获取 jqdatasdk 授权,输入用户名、密码,申请地址:http://t.cn/EINDOxE
# 聚宽官网,使用方法参见:http://t.cn/EINcS4j
import jqdatasdk
jqdatasdk.auth('user', 'passwd')

# 获取5日平均换手率因子2018-01-01到2018-12-31之间的数据(示例用从库中直接调取)
# 聚宽因子库数据获取方法在下方
from jqfactor_analyzer.sample import VOL5
factor_data = VOL5

# 对因子进行分析
far = ja.analyze_factor(
    factor_data,  # factor_data 为因子值的 pandas.DataFrame
    quantiles=10,
    periods=(1, 10),
    industry='jq_l1',
    weight_method='avg',
    max_loss=0.1
)

报错信息如下: 实际上,测试其他因子也报错。

(l2ck) [sunshe35@arch Alphalen-master]$  cd /home/sunshe35/Data/Projects/Alphalen-master ; /usr/bin/env /home/sunshe35/.conda/envs/l2ck/bin/python /home/sunshe35/.vscode/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher 42437 -- /home/sunshe35/Data/Projects/Alphalen-master/test3.py 
auth success  ( 如需更多使用说明请查看API文档:https://www.joinquant.com/help/api/doc?name=JQDatadoc )
/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/jqfactor_analyzer/analyze.py:258: FutureWarning: The previous implementation of stack is deprecated and will be removed in a future version of pandas. See the What's New notes for pandas 2.1.0 for details. Specify future_stack=True to adopt the new implementation and silence this warning.
  factor_data = factor_data.stack(dropna=False)
Traceback (most recent call last):
  File "/home/sunshe35/Data/Projects/Alphalen-master/test3.py", line 16, in <module>
    far = ja.analyze_factor(
          ^^^^^^^^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/jqfactor_analyzer/__init__.py", line 37, in analyze_factor
    return FactorAnalyzer(factor,
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/jqfactor_analyzer/analyze.py", line 251, in __init__
    self.__gen_clean_factor_and_forward_returns()
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/jqfactor_analyzer/analyze.py", line 290, in __gen_clean_factor_and_forward_returns
    self._clean_factor_data = get_clean_factor_and_forward_returns(
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/jqfactor_analyzer/prepare.py", line 387, in get_clean_factor_and_forward_returns
    factor_data = get_clean_factor(factor, forward_returns, groupby=groupby,
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/jqfactor_analyzer/prepare.py", line 296, in get_clean_factor
    merged_data['factor_quantile'] = quantile_data
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/pandas/core/frame.py", line 4299, in __setitem__
    self._set_item(key, value)
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/pandas/core/frame.py", line 4512, in _set_item
    value, refs = self._sanitize_column(value)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/pandas/core/frame.py", line 5250, in _sanitize_column
    return _reindex_for_setitem(value, self.index)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/pandas/core/frame.py", line 12674, in _reindex_for_setitem
    reindexed_value = value.reindex(index)._values
                      ^^^^^^^^^^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/pandas/core/series.py", line 5144, in reindex
    return super().reindex(
           ^^^^^^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/pandas/core/generic.py", line 5607, in reindex
    return self._reindex_axes(
           ^^^^^^^^^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/pandas/core/generic.py", line 5630, in _reindex_axes
    new_index, indexer = ax.reindex(
                         ^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/pandas/core/indexes/base.py", line 4422, in reindex
    indexer = self.get_indexer(
              ^^^^^^^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/pandas/core/indexes/base.py", line 3953, in get_indexer
    return self._get_indexer(target, method, limit, tolerance)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/pandas/core/indexes/base.py", line 3974, in _get_indexer
    tgt_values = engine._extract_level_codes(  # type: ignore[union-attr]
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "index.pyx", line 734, in pandas._libs.index.BaseMultiIndexCodesEngine._extract_level_codes
  File "/home/sunshe35/.conda/envs/l2ck/lib/python3.11/site-packages/pandas/core/indexes/multi.py", line 2579, in _recode_for_new_levels
    raise AssertionError(
AssertionError: Length of new_levels (3) must be <= self.nlevels (2)
sunshe35 commented 5 months ago

我解决了适配pandas最新版的问题,可以提交吗?

teancake commented 3 months ago

点赞加支持~~ alphalens也有一样groupby的问题,alphalens-reloaded 已经修复了。

sunshe35 commented 3 months ago

点赞加支持~~ alphalens也有一样groupby的问题,alphalens-reloaded 已经修复了。

我已经提交了,还没合并通过