You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何结合pd.to_timedelta解决yfinance下载的FutureWarning问题?

解决yfinance的TimedeltaIndex过时警告

这个警告不是你的代码问题,是yfinance库内部使用了pandas已废弃的TimedeltaIndex构造方式导致的,和你设置的period、start/end参数无关。

解决步骤:

  • 优先升级yfinance到最新版本,开发者大概率已经修复了这个过时API的使用:
    pip install --upgrade yfinance
    
  • 如果升级后警告依然存在,可以临时屏蔽这个特定的FutureWarning,在代码开头添加:
    import warnings
    from pandas.errors import FutureWarning
    
    # 只屏蔽这个特定的过时警告,不影响其他警告
    warnings.filterwarnings("ignore", category=FutureWarning, message="The 'unit' keyword in TimedeltaIndex construction is deprecated")
    

补充说明:

这个警告是yfinance在处理时区调整(比如夏令时)时触发的,和你请求的股票时间范围没有关系,所以修改period或者改用start/end参数无法解决问题,本质是库的内部代码需要适配pandas的新版本。

内容的提问来源于stack exchange,提问作者Michael P

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 22:32:03