fix(datasource): yfinance时区标准化与NaN过滤修复
- yfinance_source.py: 用 tz_localize(None) 替代 pd.to_datetime(utc=True), 避免亚洲/欧洲市场因UTC转换导致日期回退一天(如日经225 5/25→5/24) - yfinance_source.py: 新增 _normalize_index() 静态方法统一处理时区剥除 - yfinance_source.py: fetch() 增加 close=NaN 行过滤(yfinance未收盘日返回不完整数据) - flask_api_source.py: 客户端同步增加 close=NaN 过滤防御 验证结果:N225 5/25-6/3 返回7个交易日数据,日期无偏移
This commit is contained in:
@@ -167,6 +167,15 @@ class FlaskAPIDataSource:
|
||||
standard_cols = ['code'] + standard_cols
|
||||
df = df[standard_cols]
|
||||
|
||||
# 过滤 yfinance 返回的不完整数据(未收盘日 close=NaN, volume=0)
|
||||
nan_count = df['close'].isna().sum()
|
||||
if nan_count > 0:
|
||||
df = df.dropna(subset=['close'])
|
||||
actual_count = len(df)
|
||||
if actual_count == 0:
|
||||
print(f"⚠ {code}: 所有数据 close 均为 NaN")
|
||||
return None
|
||||
|
||||
# 使用 API 返回的实际数据范围(而非请求参数)
|
||||
actual_start = validated.date_range.start if validated.date_range else start_date
|
||||
actual_end = validated.date_range.end if validated.date_range else end_date
|
||||
|
||||
Reference in New Issue
Block a user