logo
Loading...

請問Part1_data_collector於跨年後產生錯誤 - Cupoy

今日運行程式跳出錯誤訊息如下:(以下錯誤代碼部分) 生成 2021 年每日成交資訊 : (0, 0)...

請問Part1_data_collector於跨年後產生錯誤

2021/01/01 下午 05:08
《用 Python 打造你的 AI 股票交易引擎》業界專家實戰教學
larry wong
觀看數:21
回答數:2
收藏數:0

今日運行程式跳出錯誤訊息如下:(以下錯誤代碼部分) 生成 2021 年每日成交資訊 : (0, 0) 合併歷年每日成交 載入 2013 年每日成交資訊 載入 2014 年每日成交資訊 載入 2015 年每日成交資訊 載入 2016 年每日成交資訊 載入 2017 年每日成交資訊 載入 2018 年每日成交資訊 載入 2019 年每日成交資訊 載入 2020 年每日成交資訊 載入 2021 年每日成交資訊 history daily file does not exist! 合併歷年月報表 / 季報表 / 指數資料 --------------------------------------------------------------------------- KeyError Traceback (most recent call last) C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2894 try: -> 2895 return self._engine.get_loc(casted_key) 2896 except KeyError as err: pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: '日期' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) in 1 # 執行相關指令 [除了存檔名稱外, 請勿修改] 2 stock_list = cs.set_combine(SELECT_CODE, TRAIN_NUM) ----> 3 df = cs.import_dataset(stock_list, RESET_MODE, MAX_CHAR_LENGTH) 4 cs.save_char(df, char_generator, RESET_MODE, MAX_CHAR_LENGTH, 5 file_1d='1d_char.csv', file_2d='2d_char.csv') ~\CH4_CStock_v1_0_5 - 1228測試所有股票\cstock.py in import_dataset(stock_list, reset_mode, char_length) 743 df, stock_list = daily_data(stock_list) 744 print('合併歷年月報表 / 季報表 / 指數資料') --> 745 df = merge_other_data(df, stock_list) 746 print('還原權值計算') 747 df = restore_reduce_rate(df, stock_list) ~\CH4_CStock_v1_0_5 - 1228測試所有股票\cstock.py in merge_other_data(df, select_list) 683 dfi = dfi.drop_duplicates().sort_values(by=['日期']).reset_index(drop=True) 684 # 設定合併主鑰 --> 685 df['月份'] = df['日期'].map(lambda x: datetime.strptime(x, '%Y-%m-%d')) 686 df['季度'] = df['月份'].map(lambda x: f'{x.year-1}-04' if x.strftime('%m-%d')<'03-31' 687 else f'{x.year}-01' if x.strftime('%m-%d')<'05-15' C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key) 2900 if self.columns.nlevels > 1: 2901 return self._getitem_multilevel(key) -> 2902 indexer = self.columns.get_loc(key) 2903 if is_integer(indexer): 2904 indexer = [indexer] C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2895 return self._engine.get_loc(casted_key) 2896 except KeyError as err: -> 2897 raise KeyError(key) from err 2898 2899 if tolerance is not None: KeyError: '日期' (以上錯誤代碼部分) 這是不是代表我們要自己製造新一年的資料?

回答列表

  • 2021/01/01 下午 05:12
    larry wong
    贊同數:0
    不贊同數:0
    留言數:0

    我按照格式弄了一個1月1日就能跑過去,但是實際上1月1日不應該有資料,應該如何正確處理?

  • 2021/01/03 上午 00:40
    Jeffrey
    贊同數:1
    不贊同數:0
    留言數:0

    一般程式碼可以去定義假期(設定以 NULL 值取代)或是去複製最近一筆的交易