Skip to content

Commit

Permalink
Repair-100x now tolerates zeroes
Browse files Browse the repository at this point in the history
  • Loading branch information
ValueRaider committed Dec 14, 2022
1 parent e7bf360 commit b48212e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions yfinance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ def _reconstruct_intervals_batch(self, df, interval, tag=-1):
if not idx in df_new.index:
# Yahoo didn't return finer-grain data for this interval,
# so probably no trading happened.
print("no fine data")
# print("no fine data")
continue
df_new_row = df_new.loc[idx]

Expand Down Expand Up @@ -646,10 +646,15 @@ def _fix_unit_mixups(self, df, interval, tz_exchange):

data_cols = ["High", "Open", "Low", "Close"] # Order important, separate High from Low
data_cols = [c for c in data_cols if c in df2.columns]
f_zeroes = (df2[data_cols]==0).any(axis=1)
if f_zeroes.any():
df2_zeroes = df2[f_zeroes]
df2 = df2[~f_zeroes]
else:
df2_zeroes = None
if df2.shape[0] <= 1:
return df
median = _ndimage.median_filter(df2[data_cols].values, size=(3, 3), mode="wrap")

if (median == 0).any():
raise Exception("median contains zeroes, why?")
ratio = df2[data_cols].values / median
ratio_rounded = (ratio / 20).round() * 20 # round ratio to nearest 20
f = ratio_rounded == 100
Expand Down Expand Up @@ -715,6 +720,9 @@ def _fix_unit_mixups(self, df, interval, tz_exchange):
if fj.any():
c = data_cols[j]
df2.loc[fj, c] = df.loc[fj, c]
if df2_zeroes is not None:
df2 = _pd.concat([df2, df2_zeroes]).sort_index()
df2.index = _pd.to_datetime()

return df2

Expand Down

0 comments on commit b48212e

Please sign in to comment.