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

使用Vocmax库get_weather_data函数遇KeyError:'Year'问题求助

问题描述

使用vocmax库的get_weather_data函数获取气象数据时,触发KeyError: 'Year'错误。根据库文档说明,单组经纬度坐标的处理耗时约3分钟,但实际程序在几秒内就尝试读取main.py中的df_iter对象,此时API尚未返回有效DataFrame。已尝试更换API密钥、使用示例坐标,问题均未解决。

代码片段

weather, info = vocmax.get_weather_data(lat,lon,api_key=api_key)

报错信息

cached_weather_data\*_45.136_-92.307.npz
Downloading weather data and saving to "cached_weather_data" ...
  0%|                                                                                                                     | 0/21 [00:00<?, ?it/s]C:\Users\FS126969\AppData\Local\anaconda3\Lib\site-packages\pvlib\iotools\psm3.py:359: pvlibDeprecationWarning: PSM3 variable names will be renamed to pvlib conventions by default starting in pvlib 0.11.0. Specify map_variables=True to enable that behavior now, or specify map_variables=False to hide this warning.
  warnings.warn(
  0%|                                                                                                                     | 0/21 [00:03<?, ?it/s] 
Traceback (most recent call last):
  File "c:\Users\FS126969\Downloads\PVTools_weather_file.py", line 244, in <module>
    weather, info = vocmax.get_weather_data(lat, lon,
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\FS126969\AppData\Local\anaconda3\Lib\site-packages\vocmax\main.py", line 279, in get_weather_data
    year_iter = str(df_iter['Year'][0])
                    ~~~~~~~^^^^^^^^
KeyError: 'Year'
解决建议
  • 适配pvlib版本兼容性:报错中提示pvlib从0.11.0版本开始会默认将PSM3变量名改为pvlib规范(首字母小写),而vocmax可能依赖旧版本的字段名。可安装0.10.x系列的pvlib版本:

    pip install pvlib==0.10.5
    

    安装完成后重新运行代码,验证问题是否解决。

  • 验证API返回字段:直接使用pvlib的psm3接口测试目标经纬度的返回数据,确认字段名是否变更:

    from pvlib.iotools import psm3
    lat, lon = 45.136, -92.307
    api_key = "你的API密钥"
    email = "你的注册邮箱"  # PSM3 API需要绑定邮箱
    df, meta = psm3.get_psm3(lat, lon, api_key, email)
    print(df.columns)
    

    如果输出中没有Year,而是year,则需要修改vocmax的main.py第279行,将df_iter['Year'][0]改为df_iter['year'][0]。

  • 修复缓存文件:缓存的npz文件可能损坏,导致读取异常。删除cached_weather_data目录下的所有文件,让程序重新下载生成缓存数据。

  • 完善API等待逻辑:若vocmax内部未正确等待API响应完成,可修改main.py中对应代码,添加字段检查与重试机制:

    # 在main.py第279行前添加
    import time
    max_retry = 12  # 对应文档说明的3分钟(12次*15秒)
    retry_count = 0
    while 'Year' not in df_iter.columns and retry_count < max_retry:
        time.sleep(15)
        # 重新加载df_iter的逻辑,例如从缓存或API重新获取
        df_iter = ...
        retry_count += 1
    if retry_count >= max_retry:
        raise RuntimeError("气象数据加载超时,请检查API连接")
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 12:00:24