Skip to content

Instantly share code, notes, and snippets.

@fanannan
Created May 23, 2017 11:43
Show Gist options
  • Save fanannan/c03323f374688d1e3ac9f8c94b80d19a to your computer and use it in GitHub Desktop.
Save fanannan/c03323f374688d1e3ac9f8c94b80d19a to your computer and use it in GitHub Desktop.
supplimental fix to pandas-datareader
# supplimental fix to pandas-datareader-'0.4.1' (not yet pulled proposal)
#
#
#$ git clone https://github.com/rgkimball/pandas-datareader
#$ cd pandas-datareader
#$ git checkout fix-yahoo
#$ pip install -e .
def fetch_yahoo(ticker):
NUM_RETRY = 5
def is_convertible(x):
return x != '' and x != 'null'
def to_float(x):
return float(x) if is_convertible(x) else np.nan
def to_int(x):
return int(x) if is_convertible(x) else np.nan
def fetch(ticker):
i = 0
while True:
try:
return data.DataReader(ticker, 'yahoo')
except:
i += 1
if i > NUM_RETRY:
raise Exception('could not get data from Yahoo Finance.')
df = fetch(ticker)
for c in df.columns:
df[c] = df[c].apply(to_float if c != 'Volume' else to_int)
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment