미해결
파이썬 증권 데이터 수집과 분석으로 신호와 소음 찾기
데이터 전체에 대한 수익률을 계산하고 싶은데 오류가 뜹니다
배운걸 활용해 Financial Data Reader KRX종목 전부에 대한 수익률을 계산하려고 합니다.
#다음 수식으로 FDR df의 "Name"과 "Symbol"만 남겼고
df_dic = df[["Name", "Symbol"]]df_dictionary = df_dic.valuesdf_dictionary
#dict 함수를 활용해 numpy array를 dictionary 형태로 변환했습니다.
df_list = dict(df_dictionary)df_list
#수업에서 배운대로 다음 수식을 적어 상장종목 전부에 대한 종가를 얻으려고 했는데
item_list = []for item_code in df_list.values(): close = fdr.DataReader(item_code, "2020", "2023")['Close'] item_list.append(close)
#다음과 같은 에러메시지가 뜹니다
eyError Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
~\anaconda3\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
~\anaconda3\lib\site-packages\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: 'Close'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_96644/3549058376.py in <module>
1 item_list = []
2 for item_code in df_list.values():
----> 3 close = fdr.DataReader(item_code, "2020", "2023")['Close']
4 item_list.append(close)
~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
3456 if self.columns.nlevels > 1:
3457 return self._getitem_multilevel(key)
-> 3458 indexer = self.columns.get_loc(key)
3459 if is_integer(indexer):
3460 indexer = [indexer]
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:
KeyError: 'Close'이거 혹시 2020~2022 사이 종가데이터가 없는 종목이 있어서 그런건가요?