Skip to content

Commit

Permalink
Fallback to yfinance if encountering API error with pandas-datareader.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefmolin committed Jan 22, 2023
1 parent 6e0270d commit bdad383
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pandas-datareader>=0.7.0
seaborn>=0.11.0
statsmodels>=0.11.1
mplfinance>=0.12.7a4
yfinance>=0.2.4
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='stock_analysis',
version='0.2',
version='0.2.1',
description='Classes for technical analysis of stocks.',
author='Stefanie Molin',
author_email='[email protected]',
Expand All @@ -11,11 +11,12 @@
packages=['stock_analysis'],
install_requires=[
'matplotlib>=3.0.2',
'mplfinance>=0.12.7a4',
'numpy>=1.15.2',
'pandas>=0.23.4',
'pandas-datareader>=0.7.0',
'seaborn>=0.11.0',
'statsmodels>=0.11.1',
'mplfinance>=0.12.7a4'
'yfinance>=0.2.4'
],
)
15 changes: 13 additions & 2 deletions stock_analysis/stock_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pandas as pd
import pandas_datareader.data as web
import yfinance as yf

from .utils import label_sanitizer

Expand Down Expand Up @@ -94,8 +95,18 @@ def get_ticker_data(self, ticker):
Returns:
A `pandas.DataFrame` object with the stock data.
"""
return web.get_data_yahoo(ticker, self.start, self.end)

try:
return web.get_data_yahoo(ticker, self.start, self.end)
except TypeError: # API issue upstream – switch to yfinance
# https://github.com/pydata/pandas-datareader/issues/952
start, end = (
dt.datetime.strptime(str_date, '%Y%m%d')
for str_date in (self.start, self.end)
)
return yf.download(
ticker, start, end + dt.timedelta(days=1),
progress=False, ignore_tz=True
)

def get_index_data(self, index):
"""
Expand Down

0 comments on commit bdad383

Please sign in to comment.