KKKMatthias commited on
Commit
1fdcb21
·
verified ·
1 Parent(s): a7bb0ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -8
app.py CHANGED
@@ -44,21 +44,38 @@ def get_stock(ticker: str):
44
  # 假设数据按时间升序排列,第0行是当前年,第1行是明年
45
  # 注意:有时 yfinance 返回的顺序可能是反的,或者包含季度数据,
46
  # 但 earnings_estimate 通常是年度数据且按时间排序。
47
-
48
- if len(est) > 0:
49
- eps_est_current = est.iloc[2]['avg'] # 当前财年
50
- if len(est) > 1:
51
- eps_est_next = est.iloc[3]['avg'] # 下一财年
 
 
52
 
53
  except Exception as e:
54
  print(f"Error parsing dataframe: {e}")
55
 
56
  # 3. 如果从表格获取失败,使用 info 中的备用数据
57
- #if eps_est_next is None and info:
58
- # eps_est_next = info.get('forwardEps')
59
 
60
  if not info or len(info) < 5:
61
  return {"error": "No data found", "ticker": ticker}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  return {
64
  "ticker": ticker.upper(),
@@ -67,7 +84,8 @@ def get_stock(ticker: str):
67
 
68
  "eps_est_avg_current_year": clean_num(eps_est_current),
69
  "eps_est_avg_next_year": clean_num(eps_est_next),
70
-
 
71
  }
72
  except Exception as e:
73
  return {"error": str(e)}
 
44
  # 假设数据按时间升序排列,第0行是当前年,第1行是明年
45
  # 注意:有时 yfinance 返回的顺序可能是反的,或者包含季度数据,
46
  # 但 earnings_estimate 通常是年度数据且按时间排序。
47
+ #eps_est_current = est.iloc[2]['avg'] # 当前财年
48
+ #eps_est_next = est.iloc[3]['avg'] # 下一财年
49
+
50
+ if '0Y' in est.index:
51
+ eps_est_current = est.loc['0Y'].get('avg')
52
+ if '+1Y' in est.index:
53
+ eps_est_next = est.loc['+1Y'].get('avg')
54
 
55
  except Exception as e:
56
  print(f"Error parsing dataframe: {e}")
57
 
58
  # 3. 如果从表格获取失败,使用 info 中的备用数据
59
+ if eps_est_next is None and info:
60
+ eps_est_next = info.get('forwardEps')
61
 
62
  if not info or len(info) < 5:
63
  return {"error": "No data found", "ticker": ticker}
64
+
65
+ try:
66
+ ept = stock.eps_trend
67
+ except Exception:
68
+ ept = None
69
+
70
+ eps_current = None
71
+
72
+ if ept is not None and not ept.empty:
73
+ try:
74
+ if '0Y' in ept.index:
75
+ eps_current = ept.loc['0Y'].get('Current Estimate') # 当前财年
76
+
77
+ except Exception as e:
78
+ print(f"Error parsing dataframe: {e}")
79
 
80
  return {
81
  "ticker": ticker.upper(),
 
84
 
85
  "eps_est_avg_current_year": clean_num(eps_est_current),
86
  "eps_est_avg_next_year": clean_num(eps_est_next),
87
+ "eps_avg_current_year": clean_num(eps_current),
88
+
89
  }
90
  except Exception as e:
91
  return {"error": str(e)}