欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Python-jupyter环境下报错Passinglist-likesto.locor[]withanymissinglabelsisnolongersupported

时间:2023-05-18

研究git上的一个abu量化源码是复现时发现的bug,因为是3~4年前的源码和教程,兼容性上会有问题,按照步骤复现时报错如下:

报错日志如下:

'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'Traceback (most recent call last): File "C:UsersAdminDesktopabu-masterabu-masterabupyAlphaBuABuPickTimeExecute.py", line 117, in _batch_symbols_with_same_factors kl_pd = kl_pd_manager.get_pick_time_kl_pd(target_symbol) File "C:UsersAdminDesktopabu-masterabu-masterabupyTradeBuABuKLManager.py", line 169, in get_pick_time_kl_pd kl_pd = self._fetch_pick_time_kl_pd(target_symbol) File "C:UsersAdminDesktopabu-masterabu-masterabupyTradeBuABuKLManager.py", line 157, in _fetch_pick_time_kl_pd return ABuSymbolPd.make_kl_df(target_symbol, data_mode=EMarketDataSplitMode.E_DATA_SPLIT_UNDO, File "C:UsersAdminDesktopabu-masterabu-masterabupyMarketBuABuSymbolPd.py", line 305, in make_kl_df df, _ = _make_kl_df(symbol, data_mode=data_mode, File "C:UsersAdminDesktopabu-masterabu-masterabupyMarketBuABuSymbolPd.py", line 128, in _make_kl_df df = _benchmark(df, benchmark, temp_symbol) File "C:UsersAdminDesktopabu-masterabu-masterabupyMarketBuABuSymbolPd.py", line 53, in _benchmark kl_pd = df.loc[benchmark.kl_pd.index] File "E:Anaconda3libsite-packagespandascoreindexing.py", line 1768, in __getitem__ return self._getitem_axis(maybe_callable, axis=axis) File "E:Anaconda3libsite-packagespandascoreindexing.py", line 1954, in _getitem_axis return self._getitem_iterable(key, axis=axis) File "E:Anaconda3libsite-packagespandascoreindexing.py", line 1595, in _getitem_iterable keyarr, indexer = self._get_listlike_indexer(key, axis, raise_missing=False) File "E:Anaconda3libsite-packagespandascoreindexing.py", line 1552, in _get_listlike_indexer self._validate_read_indexer( File "E:Anaconda3libsite-packagespandascoreindexing.py", line 1654, in _validate_read_indexer raise KeyError(KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'

 目前使用的是Python3.8版本,报错后一步一步排查,发现是报错中的这个问题

kl_pd = df.loc[benchmark.kl_pd.index]

遂去源码中查找,发现日期及个股问题,个股存在某日停牌的情况,所以在某些日期下是空,使用.loc没法查询到该日期下的数据,会返回报错(新版本)

知道是这个问题就好办了,用了官方文档上的链接查看最新版本的解决方法,即使用.reindex()方法解决,在源码文件  abu-master/abupy/MarketBu/ABuSymbolPd.py 中修改这行代码后可以解决兼容现在版本;

原代码:

kl_pd = df.loc[benchmark.kl_pd.index]

 修改后源码:

kl_pd = df.reindex(benchmark.kl_pd.index)

 再次运行回测相关的脚本,不再报错,完美解决!

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。