fix(deps): require yfinance >=1.4.1 and tolerate non-Date index column

yfinance 1.4.0 regressed the daily-download index to unnamed, so
reset_index() produced an "index" column instead of "Date" and every
stockstats indicator silently failed (no SMA/RSI/MACD/Bollinger/ATR).
Verified across versions: 1.2.0 / 1.3.0 / 1.4.1 name it "Date"; only
1.4.0 is broken. Pin to >=1.4.1 (the upstream fix) and normalize the
date column defensively so a non-"Date" index can't silently drop
indicators on any build.

#890
This commit is contained in:
Yijia-Xiao
2026-05-31 00:51:30 +00:00
parent 3543e5397e
commit a66aa8fb94
3 changed files with 88 additions and 2 deletions

View File

@@ -32,8 +32,24 @@ def yf_retry(func, max_retries=3, base_delay=2.0):
raise
def _ensure_date_column(data: pd.DataFrame) -> pd.DataFrame:
"""Normalize the date column to ``Date``.
Some yfinance builds leave the index unnamed (so ``reset_index()`` yields
``index``) or use ``Datetime`` for intraday data. Rename the first
date-like column so indicators don't silently drop when it isn't ``Date``.
"""
if "Date" in data.columns:
return data
for candidate in ("index", "Datetime", "date"):
if candidate in data.columns:
return data.rename(columns={candidate: "Date"})
return data
def _clean_dataframe(data: pd.DataFrame) -> pd.DataFrame:
"""Normalize a stock DataFrame for stockstats: parse dates, drop invalid rows, fill price gaps."""
data = _ensure_date_column(data)
data["Date"] = pd.to_datetime(data["Date"], errors="coerce")
data = data.dropna(subset=["Date"])
@@ -82,7 +98,7 @@ def load_ohlcv(symbol: str, curr_date: str) -> pd.DataFrame:
progress=False,
auto_adjust=True,
))
data = data.reset_index()
data = _ensure_date_column(data.reset_index())
data.to_csv(data_file, index=False, encoding="utf-8")
data = _clean_dataframe(data)