Skip to content

Commit ece41cd

Browse files
authored
Merge pull request #1411 from sdeibel/main
Fix format_history_metadata for some symbols
2 parents 53fca70 + c362d54 commit ece41cd

File tree

3 files changed

+75
-53
lines changed

3 files changed

+75
-53
lines changed

tests/ticker.py

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,16 @@ def test_getTz(self):
5252
def test_badTicker(self):
5353
# Check yfinance doesn't die when ticker delisted
5454

55-
tkr = "AM2Z.TA"
55+
tkr = "DJI" # typo of "^DJI"
5656
dat = yf.Ticker(tkr, session=self.session)
5757
dat.history(period="1wk")
5858
dat.history(start="2022-01-01")
5959
dat.history(start="2022-01-01", end="2022-03-01")
6060
yf.download([tkr], period="1wk")
61+
62+
for k in dat.fast_info:
63+
dat.fast_info[k]
64+
6165
dat.isin
6266
dat.major_holders
6367
dat.institutional_holders
@@ -91,43 +95,48 @@ def test_badTicker(self):
9195
def test_goodTicker(self):
9296
# that yfinance works when full api is called on same instance of ticker
9397

94-
tkr = "IBM"
95-
dat = yf.Ticker(tkr, session=self.session)
96-
97-
dat.isin
98-
dat.major_holders
99-
dat.institutional_holders
100-
dat.mutualfund_holders
101-
dat.dividends
102-
dat.splits
103-
dat.actions
104-
dat.shares
105-
dat.get_shares_full()
106-
dat.info
107-
dat.calendar
108-
dat.recommendations
109-
dat.earnings
110-
dat.quarterly_earnings
111-
dat.income_stmt
112-
dat.quarterly_income_stmt
113-
dat.balance_sheet
114-
dat.quarterly_balance_sheet
115-
dat.cashflow
116-
dat.quarterly_cashflow
117-
dat.recommendations_summary
118-
dat.analyst_price_target
119-
dat.revenue_forecasts
120-
dat.sustainability
121-
dat.options
122-
dat.news
123-
dat.earnings_trend
124-
dat.earnings_dates
125-
dat.earnings_forecasts
98+
tkrs = ["IBM"]
99+
tkrs.append("QCSTIX") # weird ticker, no price history but has previous close
100+
for tkr in tkrs:
101+
dat = yf.Ticker(tkr, session=self.session)
126102

127-
dat.history(period="1wk")
128-
dat.history(start="2022-01-01")
129-
dat.history(start="2022-01-01", end="2022-03-01")
130-
yf.download([tkr], period="1wk")
103+
dat.history(period="1wk")
104+
dat.history(start="2022-01-01")
105+
dat.history(start="2022-01-01", end="2022-03-01")
106+
yf.download([tkr], period="1wk")
107+
108+
for k in dat.fast_info:
109+
dat.fast_info[k]
110+
111+
dat.isin
112+
dat.major_holders
113+
dat.institutional_holders
114+
dat.mutualfund_holders
115+
dat.dividends
116+
dat.splits
117+
dat.actions
118+
dat.shares
119+
dat.get_shares_full()
120+
dat.info
121+
dat.calendar
122+
dat.recommendations
123+
dat.earnings
124+
dat.quarterly_earnings
125+
dat.income_stmt
126+
dat.quarterly_income_stmt
127+
dat.balance_sheet
128+
dat.quarterly_balance_sheet
129+
dat.cashflow
130+
dat.quarterly_cashflow
131+
dat.recommendations_summary
132+
dat.analyst_price_target
133+
dat.revenue_forecasts
134+
dat.sustainability
135+
dat.options
136+
dat.news
137+
dat.earnings_trend
138+
dat.earnings_dates
139+
dat.earnings_forecasts
131140

132141

133142
class TestTickerHistory(unittest.TestCase):

yfinance/base.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -270,28 +270,39 @@ def last_price(self):
270270
return self._last_price
271271
prices = self._get_1y_prices()
272272
if prices.empty:
273-
self._last_price = self._get_exchange_metadata()["regularMarketPrice"]
273+
md = self._get_exchange_metadata()
274+
if "regularMarketPrice" in md:
275+
self._last_price = md["regularMarketPrice"]
274276
else:
275277
self._last_price = float(prices["Close"].iloc[-1])
276278
if _np.isnan(self._last_price):
277-
self._last_price = self._get_exchange_metadata()["regularMarketPrice"]
279+
md = self._get_exchange_metadata()
280+
if "regularMarketPrice" in md:
281+
self._last_price = md["regularMarketPrice"]
278282
return self._last_price
279283

280284
@property
281285
def previous_close(self):
282286
if self._prev_close is not None:
283287
return self._prev_close
284288
prices = self._get_1wk_1h_prepost_prices()
285-
prices = prices[["Close"]].groupby(prices.index.date).last()
286-
if prices.shape[0] < 2:
287-
# Very few symbols have previousClose despite no
288-
# no trading data. E.g. 'QCSTIX'.
289-
# So fallback to original info[] if available.
290-
self._tkr.info # trigger fetch
291-
if "previousClose" in self._tkr._quote._retired_info:
292-
self._prev_close = self._tkr._quote._retired_info["previousClose"]
289+
fail = False
290+
if prices.empty:
291+
fail = True
293292
else:
294-
self._prev_close = float(prices["Close"].iloc[-2])
293+
prices = prices[["Close"]].groupby(prices.index.date).last()
294+
if prices.shape[0] < 2:
295+
# Very few symbols have previousClose despite no
296+
# no trading data e.g. 'QCSTIX'.
297+
fail = True
298+
else:
299+
self._prev_close = float(prices["Close"].iloc[-2])
300+
if fail:
301+
# Fallback to original info[] if available.
302+
self._tkr.info # trigger fetch
303+
k = "previousClose"
304+
if self._tkr._quote._retired_info is not None and k in self._tkr._quote._retired_info:
305+
self._prev_close = self._tkr._quote._retired_info[k]
295306
return self._prev_close
296307

297308
@property
@@ -309,8 +320,9 @@ def regular_market_previous_close(self):
309320
# no trading data. E.g. 'QCSTIX'.
310321
# So fallback to original info[] if available.
311322
self._tkr.info # trigger fetch
312-
if "regularMarketPreviousClose" in self._tkr._quote._retired_info:
313-
self._reg_prev_close = self._tkr._quote._retired_info["regularMarketPreviousClose"]
323+
k = "regularMarketPreviousClose"
324+
if self._tkr._quote._retired_info is not None and k in self._tkr._quote._retired_info:
325+
self._reg_prev_close = self._tkr._quote._retired_info[k]
314326
else:
315327
self._reg_prev_close = float(prices["Close"].iloc[-2])
316328
return self._reg_prev_close
@@ -483,8 +495,9 @@ def market_cap(self):
483495
# E.g. 'BTC-USD'
484496
# So fallback to original info[] if available.
485497
self._tkr.info
486-
if "marketCap" in self._tkr._quote._retired_info:
487-
self._mcap = self._tkr._quote._retired_info["marketCap"]
498+
k = "marketCap"
499+
if self._tkr._quote._retired_info is not None and k in self._tkr._quote._retired_info:
500+
self._mcap = self._tkr._quote._retired_info[k]
488501
else:
489502
self._mcap = float(shares * self.last_price)
490503
return self._mcap

yfinance/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ def format_history_metadata(md):
699699
tz = md["exchangeTimezoneName"]
700700

701701
for k in ["firstTradeDate", "regularMarketTime"]:
702-
if k in md:
702+
if k in md and md[k] is not None:
703703
md[k] = _pd.to_datetime(md[k], unit='s', utc=True).tz_convert(tz)
704704

705705
if "currentTradingPeriod" in md:

0 commit comments

Comments
 (0)