Skip to content

Commit bdad383

Browse files
committedJan 22, 2023
Fallback to yfinance if encountering API error with pandas-datareader.
1 parent 6e0270d commit bdad383

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed
 

‎requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pandas-datareader>=0.7.0
55
seaborn>=0.11.0
66
statsmodels>=0.11.1
77
mplfinance>=0.12.7a4
8+
yfinance>=0.2.4

‎setup.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='stock_analysis',
5-
version='0.2',
5+
version='0.2.1',
66
description='Classes for technical analysis of stocks.',
77
author='Stefanie Molin',
88
author_email='24376333+stefmolin@users.noreply.github.com',
@@ -11,11 +11,12 @@
1111
packages=['stock_analysis'],
1212
install_requires=[
1313
'matplotlib>=3.0.2',
14+
'mplfinance>=0.12.7a4',
1415
'numpy>=1.15.2',
1516
'pandas>=0.23.4',
1617
'pandas-datareader>=0.7.0',
1718
'seaborn>=0.11.0',
1819
'statsmodels>=0.11.1',
19-
'mplfinance>=0.12.7a4'
20+
'yfinance>=0.2.4'
2021
],
2122
)

‎stock_analysis/stock_reader.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pandas as pd
77
import pandas_datareader.data as web
8+
import yfinance as yf
89

910
from .utils import label_sanitizer
1011

@@ -94,8 +95,18 @@ def get_ticker_data(self, ticker):
9495
Returns:
9596
A `pandas.DataFrame` object with the stock data.
9697
"""
97-
return web.get_data_yahoo(ticker, self.start, self.end)
98-
98+
try:
99+
return web.get_data_yahoo(ticker, self.start, self.end)
100+
except TypeError: # API issue upstream – switch to yfinance
101+
# https://github.com/pydata/pandas-datareader/issues/952
102+
start, end = (
103+
dt.datetime.strptime(str_date, '%Y%m%d')
104+
for str_date in (self.start, self.end)
105+
)
106+
return yf.download(
107+
ticker, start, end + dt.timedelta(days=1),
108+
progress=False, ignore_tz=True
109+
)
99110

100111
def get_index_data(self, index):
101112
"""

0 commit comments

Comments
 (0)