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

如何解决AttributeError: 'DataFrame'对象无'ix'属性错误?

问题解决:IO-Model-Builder中AttributeError: 'DataFrame' object has no attribute 'ix'

问题场景

运行IO-Model-Builder的示例代码时,执行以下卫星表相关代码段:

sat_i = iomb.make_sat_table('satellite_table.csv')
sat_c = sat_i.apply_market_shares(io_model.get_market_shares(), 'sector_meta_data.csv')
sat_c.to_csv('satellite_table_com.csv')

触发如下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_13756\2825653640.py in ?()
      1 sat_i = iomb.make_sat_table('satellite_table.csv')
----> 2 sat_c = sat_i.apply_market_shares(io_model.get_market_shares(), 'sector_meta_data.csv')
      3 
      4 #sat_c.to_csv('satellite_table_com.csv')

~\AppData\Local\Programs\Python\Python311\Lib\site-packages\iomb\sat.py in ?(self, market_shares sector_info_csv)
    313             new_table.sectors.append(new_sector)
    314             new_table.sector_idx[new_sector.key] = new_col
    315             share_entries = {}
    316             shares[new_col] = share_entries
---> 317             m_shares = market_shares.ix[:, commodity]
    318             for industry_key in m_shares.index:
    319                 share = m_shares[industry_key]
    320                 if share == 0:

~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\generic.py in ?(self, name)
   5985             and name not in self._accessors
   5986             and self._info_axis._can_hold_identifiers_and_holds_name(name)
   5987         ):
   5988             return self[name]
-> 5989         return object.__getattribute__(self, name)

AttributeError: 'DataFrame' object has no attribute 'ix'

错误原因

IO-Model-Builder的源码中使用了pandas已废弃的ix索引器。ix在pandas 1.0.0版本后被完全移除,而当前使用的高版本pandas(Python3.11配套的pandas版本必然高于1.0)不再支持该属性,因此触发报错。

解决方法

方法一:修改IO-Model-Builder源码

  1. 找到iomb库的安装路径,定位到sat.py文件(示例路径:~\AppData\Local\Programs\Python\Python311\Lib\site-packages\iomb\sat.py)
  2. 找到第317行的代码:
    m_shares = market_shares.ix[:, commodity]
    
  3. 将ix替换为loc(因为commodity是列名,属于标签索引):
    m_shares = market_shares.loc[:, commodity]
    
  4. 保存文件后重新运行代码即可。

方法二:降级pandas到兼容版本

打开终端执行以下命令,安装最后支持ix的pandas版本之一:

pip install pandas==0.25.3

注意:降级pandas可能会影响其他依赖该库的项目,需谨慎操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 18:42:34