Skip to content

Commit

Permalink
Improve 'get_shares_full()' error handling ; Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ValueRaider committed Jan 14, 2023
1 parent 47c579f commit 5d9a91d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions tests/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_badTicker(self):
dat.splits
dat.actions
dat.shares
dat.shares_full
dat.get_shares_full()
dat.info
dat.calendar
dat.recommendations
Expand Down Expand Up @@ -101,7 +101,7 @@ def test_goodTicker(self):
dat.splits
dat.actions
dat.shares
dat.shares_full
dat.get_shares_full()
dat.info
dat.calendar
dat.recommendations
Expand Down
13 changes: 8 additions & 5 deletions yfinance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ def get_shares_full(self, start=None, end=None, proxy=None):
if end is None:
end = dt_now
if start is None:
start = end - _pd.Timedelta(days=365)
start = end - _pd.Timedelta(days=548) # 18 months
if start >= end:
print("ERROR: start date must be before end")
return None
Expand All @@ -1159,10 +1159,13 @@ def get_shares_full(self, start=None, end=None, proxy=None):
return None

shares_data = json_data["timeseries"]["result"]
if not "shares_out" in shares_data[0]:
print(f"{self.ticker}: Yahoo did not return share count in date range {start} -> {end}")
return None
try:
df = _pd.Series(shares_data[0]["shares_out"], index=_pd.to_datetime(shares_data[0]["timestamp"], unit="s"))
except:
print(f"{self.ticker}: Failed to parse shares count data")
except Exception as e:
print(f"{self.ticker}: Failed to parse shares count data: "+str(e))
return None

df.index = df.index.tz_localize(tz)
Expand Down Expand Up @@ -1305,8 +1308,8 @@ def get_earnings_dates(self, limit=12, proxy=None) -> Optional[pd.DataFrame]:
dates[cn] = _pd.to_datetime(dates[cn], format="%b %d, %Y, %I %p")
# - instead of attempting decoding of ambiguous timezone abbreviation, just use 'info':
self._quote.proxy = proxy
dates[cn] = dates[cn].dt.tz_localize(
tz=self._quote.info["exchangeTimezoneName"])
tz = self._get_ticker_tz(debug_mode=False, proxy=proxy, timeout=30)
dates[cn] = dates[cn].dt.tz_localize(tz)

dates = dates.set_index("Earnings Date")

Expand Down
2 changes: 1 addition & 1 deletion yfinance/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ def _reindex_events(df, new_index, data_col_name):
if interval.endswith('m') or interval.endswith('h') or interval == "1d":
# Update: is possible with daily data when dividend very recent
f_missing = ~df_sub.index.isin(df.index)
df_sub_missing = df_sub[f_missing]
df_sub_missing = df_sub[f_missing].copy()
keys = {"Adj Open", "Open", "Adj High", "High", "Adj Low", "Low", "Adj Close",
"Close"}.intersection(df.columns)
df_sub_missing[list(keys)] = _np.nan
Expand Down

0 comments on commit 5d9a91d

Please sign in to comment.