Created
May 23, 2017 11:43
-
-
Save fanannan/c03323f374688d1e3ac9f8c94b80d19a to your computer and use it in GitHub Desktop.
supplimental fix to pandas-datareader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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