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

从yfinance导入Nifty50数据后访问Datetime列触发KeyError求助

解决yfinance数据KeyError: 'Datetime'的问题

问题原因

从你提供的截图能看到,Datetime是数据框的索引,不是普通列,直接用df['Datetime']访问自然会触发KeyError。

解决方法

你可以用两种方式获取时间数据:

  • 直接访问索引:print(df.index)
  • 将索引转为普通列后再访问:
    # 把索引转为列,同时保留原索引(可选)
    df.reset_index(inplace=True)
    # 现在可以正常访问'Datetime'列
    print(df['Datetime'])
    

验证步骤

执行完上述操作后,你可以用df.columns查看所有列名,确认Datetime已经在列列表里,或者用df.head()预览数据结构,确保时间列存在。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 01:01:07