FinanceData / FinanceDataReader

Financial data reader
1.14k stars 359 forks source link

S&P 데이터를 읽을 수 없습니다. #63

Closed keonho-kim closed 3 years ago

keonho-kim commented 3 years ago

제목 그대로, S&P 데이터를 읽어올 수 없습니다.

` KeyError Traceback (most recent call last)

in ----> 1 sp500 = fdr.StockListing('S&P500') ~/anaconda3/lib/python3.8/site-packages/FinanceDataReader/data.py in StockListing(market) 52 return KrxAdministrative(market).read() 53 if market in [ 'S&P500', 'SP500']: ---> 54 return WikipediaStockListing(market).read() 55 else: 56 msg = "market='%s' is not implemented" % market ~/anaconda3/lib/python3.8/site-packages/FinanceDataReader/wikipedia/listing.py in read(self) 10 cols_ren = {'Security':'Name', 'Ticker symbol':'Symbol', 'GICS Sector':'Sector', 'GICS Sub Industry':'Industry'} 11 df = df.rename(columns = cols_ren) ---> 12 df = df[['Symbol', 'Name', 'Sector', 'Industry']] 13 df['Symbol'] = df['Symbol'].str.replace('\.', '') 14 return df ~/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key) 2906 if is_iterator(key): 2907 key = list(key) -> 2908 indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1] 2909 2910 # take() does not accept boolean indexers ~/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in _get_listlike_indexer(self, key, axis, raise_missing) 1252 keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr) 1253 -> 1254 self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing) 1255 return keyarr, indexer 1256 ~/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing) 1302 if raise_missing: 1303 not_found = list(set(key) - set(ax)) -> 1304 raise KeyError(f"{not_found} not in index") 1305 1306 # we skip the warning on Categorical KeyError: "['Industry'] not in index" ` 이런 에러 메세지를 얻게됩니다. 현재 버전은 0.9.10입니다. 추가로, 혹시 미국 주식 종목들에 대해서 섹터와 산업의 영문 버전은 구할 수 없을까요? 좋은 패키지 감사합니다.
chaconne67 commented 3 years ago
class WikipediaStockListing:
    def __init__(self, market):
        self.market = market

    def read(self):
        url = 'https://en.wikipedia.org/wiki/List_of_S&P_500_companies'
        df = pd.read_html(url, header=0)[0]
        cols_ren = {'Security':'Name', 'Ticker symbol':'Symbol', 'GICS Sector':'Sector', 'GICS Sub Industry':'Industry'}
        df = df.rename(columns = cols_ren)
        df = df[['Symbol', 'Name', 'Sector', 'Industry']]
        df['Symbol'] = df['Symbol'].str.replace('\.', '')
        return df

/설치된 디렉토리/FinanceDataReader/wikipedia/listing.py 파일의 위 내용 중, 'GICS Sub Industry':'Industry' => 'GICS Sub-Industry':'Industry'로 수정하면 됩니다. 구글 코랩에서는 어딘가 만지면 될거 같은데 잘 모르겠네요.

FinanceData commented 3 years ago

FIX 되었습니다. 원천 데이터 사이트 (wikipedia)의 변경을 반영하였습니다.

다음과 같이 0.9.12 로 upgrade 하셔서 사용하세요.

pip install --upgrade finance-datareader

https://github.com/FinanceData/FinanceDataReader/wiki/Release-Note-0.9.12