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

TypeError: Cannot infer number of levels from empty list #347

Open xiafansheng opened 5 years ago

xiafansheng commented 5 years ago

Problem Description

here is my data for price and factor ,i got a error of TypeError: Cannot infer number of levels from empty list ,can anyone help me ,thanks a lot

             close              ...                        

asset 000001.XSHE 000002.XSHE ... 603986.XSHG 603993.XSHG date ...
2006-01-13 1.44 1.38 ... NaN NaN 2006-01-16 1.41 1.34 ... NaN NaN 2006-01-17 1.42 1.38 ... NaN NaN 2006-01-18 1.44 1.41 ... NaN NaN 2006-01-19 1.46 1.43 ... NaN NaN

[5 rows x 300 columns] id date asset
2006-01-13 000001.XSHE -0.216 000002.XSHE -0.272 000063.XSHE -0.240 000069.XSHE -0.232 000100.XSHE -0.312

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

path =r"D:\notebook\hs300 stock data.csv"
factor = pd.read_csv(path,header=None,names=['date','open','close','high','low','volume','capital','ret','id','asset','name'])
factordata = factor[['date','asset','id']].set_index(['date','asset']).sort_index()
prices_data = factor[['date','asset','close']].set_index(['date','asset']).unstack('asset')
factor_data = alphalens.utils.get_clean_factor_and_forward_returns(factordata,
                                                                   prices_data,
                                                                   quantiles=5,)

Please provide the full traceback:

Traceback (most recent call last):
  File "C:/Users/xfs9619/PycharmProjects/myprojct/Quant/alphalens_api.py", line 17, in <module>
    quantiles=5,)
  File "C:\Users\xfs9619\Anaconda3\lib\site-packages\alphalens\utils.py", line 794, in get_clean_factor_and_forward_returns
    cumulative_returns)
  File "C:\Users\xfs9619\Anaconda3\lib\site-packages\alphalens\utils.py", line 333, in compute_forward_returns
    names=['date', 'asset']
  File "C:\Users\xfs9619\Anaconda3\lib\site-packages\pandas\core\indexes\multi.py", line 538, in from_product
    codes, levels = _factorize_from_iterables(iterables)
  File "C:\Users\xfs9619\Anaconda3\lib\site-packages\pandas\core\arrays\categorical.py", line 2816, in _factorize_from_iterables
    return map(list, zip(*(_factorize_from_iterable(it) for it in iterables)))
  File "C:\Users\xfs9619\Anaconda3\lib\site-packages\pandas\core\arrays\categorical.py", line 2816, in <genexpr>
    return map(list, zip(*(_factorize_from_iterable(it) for it in iterables)))
  File "C:\Users\xfs9619\Anaconda3\lib\site-packages\pandas\core\arrays\categorical.py", line 2788, in _factorize_from_iterable
    cat = Categorical(values, ordered=False)
  File "C:\Users\xfs9619\Anaconda3\lib\site-packages\pandas\core\arrays\categorical.py", line 386, in __init__
    codes, categories = factorize(values, sort=False)
  File "C:\Users\xfs9619\Anaconda3\lib\site-packages\pandas\util\_decorators.py", line 208, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\xfs9619\Anaconda3\lib\site-packages\pandas\core\algorithms.py", line 686, in factorize
    uniques = original._shallow_copy(uniques, name=None)
  File "C:\Users\xfs9619\Anaconda3\lib\site-packages\pandas\core\indexes\multi.py", line 997, in _shallow_copy
    return MultiIndex.from_tuples(values, names=names, **kwargs)
  File "C:\Users\xfs9619\Anaconda3\lib\site-packages\pandas\core\indexes\multi.py", line 477, in from_tuples
    raise TypeError(msg)
TypeError: Cannot infer number of levels from empty list

Please provide any additional information below:

Versions

Pshower commented 4 months ago

did you fix it?