Vishwas1 commited on
Commit
0be8fa0
·
verified ·
1 Parent(s): b2a4427

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -3
app.py CHANGED
@@ -38,18 +38,35 @@ def fetch_series(ticker: str, years: int) -> pd.Series:
38
  )
39
  if data is None or data.empty:
40
  raise gr.Error(f"No price data found for '{ticker}'.")
41
- # Prefer 'Close' after auto_adjust; fall back to 'Adj Close' if needed
42
- col = "Close" if "Close" in data.columns else "Adj Close"
43
- y = data[col].rename(ticker)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  y.index = pd.DatetimeIndex(y.index).tz_localize(None)
45
 
46
  # Business-day index, forward-fill market holidays
47
  bidx = pd.bdate_range(y.index.min(), y.index.max())
48
  y = y.reindex(bidx).ffill()
 
49
  if y.isna().all():
50
  raise gr.Error(f"Only missing values for '{ticker}'.")
51
  return y
52
 
 
53
  def forecast_ticker(ticker: str,
54
  horizon: int,
55
  lookback_years: int,
 
38
  )
39
  if data is None or data.empty:
40
  raise gr.Error(f"No price data found for '{ticker}'.")
41
+
42
+ # Choose a price column
43
+ col = "Close" if "Close" in data.columns else ("Adj Close" if "Adj Close" in data.columns else None)
44
+ if col is None:
45
+ raise gr.Error(f"Unexpected columns from yfinance: {list(data.columns)}")
46
+
47
+ # yfinance can sometimes return a MultiIndex (e.g., if a list of tickers slips through)
48
+ if isinstance(data.columns, pd.MultiIndex):
49
+ if ticker in data[col].columns:
50
+ s = data[col][ticker]
51
+ else:
52
+ # fall back to the first column
53
+ s = data[col].iloc[:, 0]
54
+ else:
55
+ s = data[col]
56
+
57
+ y = s.copy()
58
+ y.name = ticker
59
  y.index = pd.DatetimeIndex(y.index).tz_localize(None)
60
 
61
  # Business-day index, forward-fill market holidays
62
  bidx = pd.bdate_range(y.index.min(), y.index.max())
63
  y = y.reindex(bidx).ffill()
64
+
65
  if y.isna().all():
66
  raise gr.Error(f"Only missing values for '{ticker}'.")
67
  return y
68
 
69
+
70
  def forecast_ticker(ticker: str,
71
  horizon: int,
72
  lookback_years: int,