Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

Python "KeyError: 'Date'" using pandas data reader and yahoofinance

I am running into a "KeyError: 'Date'" message at the last piece of my code.

This script is meant to first webscrape wikipedia for the stock ticker of the SP500 index, store them as 'tickers' and use yahoofinance to pull the adjusted closes. The error appears in my last block of code.

Importing packages:

import datetime as dt
import time
import pandas as pd
import pandas_datareader as pdr
import requests
import bs4 as bs

Webscraping for the tickers dataframe:

html = requests.get('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
soup = bs.BeautifulSoup(html.text)
tickers = []
table = soup.find('table',{'class': 'wikitable sortable'})
rows = table.findAll('tr')[1:]
for row in rows:
    ticker = row.findAll('td')[0].text
    tickers.append(ticker[:-1])

Defining Start and End Date:

start = dt.datetime(2021,1,8)
end = dt.datetime.now()

This piece of code returns the error:

df = pd.DataFrame()
for ticker in tickers:
    df2 = pdr.DataReader(ticker,'yahoo',start,end)
    df[ticker] = df2['Adj Close']

The error message:

KeyError                                  Traceback (most recent call last)
~anaconda3libsite-packagespandascoreindexesase.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libsindex.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libsindex.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libshashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libshashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Date'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-70-86e494615bb1> in <module>
      1 df = pd.DataFrame()
      2 for ticker in tickers:
----> 3     df2 = pdr.DataReader(ticker,'yahoo',start,end)
      4     df[ticker] = df2['Adj Close']

~anaconda3libsite-packagespandasutil\_decorators.py in wrapper(*args, **kwargs)
    212                 else:
    213                     kwargs[new_arg_name] = new_arg_value
--> 214             return func(*args, **kwargs)
    215 
    216         return cast(F, wrapper)

~anaconda3libsite-packagespandas_datareaderdata.py in DataReader(name, data_source, start, end, retry_count, pause, session, api_key)
    382             retry_count=retry_count,
    383             pause=pause,
--> 384             session=session,
    385         ).read()
    386 

~anaconda3libsite-packagespandas_datareaderase.py in read(self)
    251         # If a single symbol, (e.g., 'GOOG')
    252         if isinstance(self.symbols, (string_types, int)):
--> 253             df = self._read_one_data(self.url, params=self._get_params(self.symbols))
    254         # Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
    255         elif isinstance(self.symbols, DataFrame):

~anaconda3libsite-packagespandas_datareaderyahoodaily.py in _read_one_data(self, url, params)
    163         prices = DataFrame(data["prices"])
    164         prices.columns = [col.capitalize() for col in prices.columns]
--> 165         prices["Date"] = to_datetime(to_datetime(prices["Date"], unit="s").dt.date)
    166 
    167         if "Data" in prices.columns:

~anaconda3libsite-packagespandascoreframe.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

~anaconda3libsite-packagespandascoreindexesase.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libsindex.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libsindex.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libshashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libshashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Date'
question from:https://stackoverflow.com/questions/65661454/python-keyerror-date-using-pandas-data-reader-and-yahoofinance

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...