Skip to content

Commit

Permalink
Changed fillna, iloc, and added test for changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JuliaLWang8 committed Dec 7, 2023
1 parent f08fe83 commit e988afc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
7 changes: 6 additions & 1 deletion tests/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,15 @@ def test_goodTicker_withProxy(self):
self.assertIsNotNone(v)
self.assertFalse(v.empty)

dat.get_history_metadata(proxy=self.proxy)
v = dat.get_history_metadata(proxy=self.proxy)
self.assertIsNotNone(v)
self.assertTrue(len(v) > 0)

v = dat._reconstruct_intervals_batch(dat.history(period="3mo", interval="5d", prepost=True), "1d", True)
self.assertIsNotNone(v)
self.assertTrue(len(v) > 0)


# Below will fail because not ported to Yahoo API

# v = dat.stats(proxy=self.proxy)
Expand Down
10 changes: 5 additions & 5 deletions yfinance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,26 +704,26 @@ def _reconstruct_intervals_batch(self, df, interval, prepost, tag=-1):
# But in case are repairing a chunk of bad 1d data, back/forward-fill the
# good div-adjustments - not perfect, but a good backup.
div_adjusts[f_tag] = np.nan
div_adjusts = div_adjusts.fillna(method='bfill').fillna(method='ffill')
div_adjusts = div_adjusts.ffill().bfill()
for idx in np.where(f_tag)[0]:
dt = df_new_calib.index[idx]
n = len(div_adjusts)
if df_new.loc[dt, "Dividends"] != 0:
if idx < n - 1:
# Easy, take div-adjustment from next-day
div_adjusts[idx] = div_adjusts[idx + 1]
div_adjusts[idx] = div_adjusts.iloc[idx + 1]
else:
# Take previous-day div-adjustment and reverse todays adjustment
div_adj = 1.0 - df_new_calib["Dividends"].iloc[idx] / df_new_calib['Close'].iloc[
idx - 1]
div_adjusts[idx] = div_adjusts[idx - 1] / div_adj
div_adjusts.iloc[idx] = div_adjusts.iloc[idx - 1] / div_adj
else:
if idx > 0:
# Easy, take div-adjustment from previous-day
div_adjusts[idx] = div_adjusts[idx - 1]
div_adjusts[idx] = div_adjusts.iloc[idx - 1]
else:
# Must take next-day div-adjustment
div_adjusts[idx] = div_adjusts[idx + 1]
div_adjusts[idx] = div_adjusts.iloc[idx + 1]
if df_new_calib["Dividends"].iloc[idx + 1] != 0:
div_adjusts[idx] *= 1.0 - df_new_calib["Dividends"].iloc[idx + 1] / \
df_new_calib['Close'].iloc[idx]
Expand Down

0 comments on commit e988afc

Please sign in to comment.