logo
Loading...

【PART3 (V1.03) - IndexError錯誤】 - Cupoy

您好,執行PART3最後一步的時候,出現以下錯誤訊息應該要怎麼修改呢?感謝!------------...

【PART3 (V1.03) - IndexError錯誤】

2020/08/02 下午 11:47
《用 Python 打造你的 AI 股票交易引擎》業界專家實戰教學
陳昱臻
觀看數:0
回答數:0
收藏數:0

您好,執行PART3最後一步的時候,出現以下錯誤訊息

應該要怎麼修改呢?

感謝!


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-6-e48eedced9e4> in <module>
     7 model_2d60 = cs.load_model(data_2d, model_file='2d_60D_model')
     8 model_60 = cs.blending_model([model_1d60, model_2d60], [0.7])
----> 9 cs.predict_result(trading_params=TRADING_PARAMS, model_d1=model_1, model_d60=model_60)

~\Documents\CH4_CStock2\cstock.py in predict_result(trading_params, model_d1, model_d60)
  1311     return_df = return_df[return_df['代號'].isin(keep_list)]
  1312     return_df = pd.merge(return_df, asset_df, on=['代號'], how='outer')
-> 1313     query_df = asset_query(set(return_df['代號'])-{'現金'})[['代號', '最新收盤', '最新漲跌']]
  1314     return_df = pd.merge(return_df, query_df, on=['代號'], how='left')
  1315

~\Documents\CH4_CStock2\cstock.py in asset_query(query_set)
  1089         data = df[df['代號']==stock_id]
  1090         data = data.iloc[-2:]
-> 1091         last_date = data['日期'].iloc[-1]
  1092         last_close = data['收盤'].iloc[-1]
  1093         prev_close = data['收盤'].iloc[-2]

~\Anaconda3\envs\py36\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
  1422
  1423             maybe_callable = com.apply_if_callable(key, self.obj)
-> 1424             return self._getitem_axis(maybe_callable, axis=axis)
  1425
  1426     def _is_scalar_access(self, key: Tuple):

~\Anaconda3\envs\py36\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
  2155
  2156             # validate the location
-> 2157             self._validate_integer(key, axis)
  2158
  2159             return self._get_loc(key, axis=axis)

~\Anaconda3\envs\py36\lib\site-packages\pandas\core\indexing.py in _validate_integer(self, key, axis)
  2086         len_axis = len(self.obj._get_axis(axis))
  2087         if key >= len_axis or key < -len_axis:
-> 2088             raise IndexError("single positional indexer is out-of-bounds")
  2089
  2090     def _getitem_tuple(self, tup):

IndexError: single positional indexer is out-of-bounds

回答列表

  • 2020/08/03 上午 00:21
    徐偉峻
    贊同數:1
    不贊同數:0
    留言數:0

    應該跟另一位遇到是相同的問題,選股清單中某些股號在 2020_daily.csv 中沒有歷史交易資料。

    https://www.cupoy.com/qa/club/ai_tw/0000017274D4FA7C000000016375706F795F72656C656173654B5741535354434C5542/000001739D1F9AB1000000126375706F795F72656C656173655155455354


    錯誤是發生在 cstock.py 1313行要執行asset_query()的步驟。

    asset_query 裡面要做的事情,是從 2020_daily.csv 所有資料中,將個人選股清單股號的資料弄成一個資料表 ( 就是1089行 data = df[df['代號']==stock_id] )


    然後再從這個股號的表中找到最後一個日期,存成一個值 ( 就是 1091行 last_date = data['日期'].iloc[-1])


    在這個步驟,若該股號沒有資料在 2020_daily.csv 中,1089 行執行後的 data 就是空的表格,所以在 1091 行要從一個空表格中取得日期,就會出現IndexError: single positional indexer is out-of-bounds 的錯誤。


    可以用以下這段程式碼,找出你的選股中是哪一個股號在 2020_daily.csv 中沒有資料。找到那個股號後,將它從選股中移除就暫時解了這問題。


    但是為何在 2020_daily.csv 中沒有撈到那個股號,就要另外找問題點了。