Pandas报IndexError: tuple index out of range的原因排查
问题分析:IndexError: tuple index out of range异常原因
问题场景
执行代码
print("cp_name={}, oe_name={}, category={}".format(cp_name, oe_name, category)) print("DF After: mtm={}, tenor={}, limit={}, utilization={}".format(*tuple(ac_df.loc[(ac_df['COUNTERPARTY ID'] == cp_name) & (ac_df['OWN ENTITY ID'] == oe_name) & (ac_df['MEASURE TYPE'] == category) & (ac_df['TENOR'] == 'Remaining'),['MTM','TENOR', 'LIMIT', 'UTILIZATION']].astype(str).values.flatten())))
触发错误
运行时输出如下错误:
cp_name=FX_FTA050377_LE, oe_name=BNS, category=FX Notional Traceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "/opt/rh/rh-python36/root/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line utility.execute() File "/opt/rh/rh-python36/root/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/opt/rh/rh-python36/root/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv self.execute(*args, **cmd_options) File "/opt/rh/rh-python36/root/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute output = self.handle(*args, **options) File "/opt/rh/rh-python36/root/lib64/python3.6/contextlib.py", line 52, in inner return func(*args, **kwds) File "/app/ccl/util/pfeweb_command.py", line 288, in handle bulk_list, updates = self.proc(file_map, **options) File "/app/ccl/counterparty/management/commands/actualexposure_8pm_rpt.py", line 149, in proc print("DF After: mtm={}, tenor={}, limit={}, utilization={}".format(*tuple(ac_df.loc[(ac_df['COUNTERPARTY ID'] == cp_name) & (ac_df['OWN ENTITY ID'] == oe_name) & (ac_df['MEASURE TYPE'] == category) & (ac_df['TENOR'] == 'Remaining'),['MTM','TENOR', 'LIMIT', 'UTILIZATION']].astype(str).values.flatten()))) IndexError: tuple index out of range
交互式环境测试正常
将变量替换为打印出的具体值后,在交互式环境执行可正常输出:
>>> print("DF After: mtm={}, tenor={}, limit={}, utilization={}".format(*tuple(ac_df.loc[(ac_df['COUNTERPARTY ID'] == 'FX_FTA050377_LE') & (ac_df['OWN ENTITY ID'] == 'BNS') & (ac_df['MEASURE TYPE'] == 'FX Notional') & (ac_df['TENOR'] == 'Remaining'),['MTM','TENOR', 'LIMIT', 'UTILIZATION']].astype(str).values.flatten()))) DF After: mtm=-13662.69, tenor=Remaining, limit=0.0, utilization=0.0
异常原因
核心问题是代码运行时的查询未返回任何数据,导致format()方法缺少所需参数,具体有两种常见可能性:
DataFrame状态不一致
代码运行时的ac_df和交互式环境中使用的ac_df不是同一个版本:可能代码执行到报错语句前,ac_df已经被其他逻辑修改(比如过滤、删除行,或者重新加载了不同的数据),此时用变量查询无法匹配到任何行,values.flatten()得到空数组,解包后没有参数传递给format(),触发IndexError。而交互式环境中使用的是未被修改的原始ac_df,所以能查询到结果。变量存在隐形字符
打印出来的cp_name、oe_name或category看起来和手动输入的字符串一致,但实际包含隐形字符(比如多余空格、换行符、控制字符)。这些字符在打印时无法被肉眼识别,但会导致和DataFrame中的值不匹配,查询无结果。而手动输入的字符串没有这些隐形字符,因此能正常匹配到行。
内容的提问来源于stack exchange,提问作者techie11
相关产品推荐
相关产品推荐

