【发布时间】:2015-10-16 01:17:48
【问题描述】:
我得到了错误
溢出错误:无法将浮点无穷大转换为整数
从此代码:
if not math.isinf(data['occurrence'][0][key]):
df.set_value(df.date == key, name, data['occurrence'][0][key])
set_value 部分怎么会被执行?如何解决这个问题?
编辑:
全栈跟踪:
Traceback(最近一次调用最后一次):文件“aggregateData.py”,第 27 行, 在 df.set_value(df.date == key, name, data['occurrence'][0][key]) #更新df文件“/usr/local/lib/python2.7/dist-packages/pandas/core/ frame.py", 行 第1690章 self.loc[index, col] = value 文件“/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py”,行 114,在 setitem 中 indexer = self._get_setitem_indexer(key) 文件“/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py”,行 106,在_get_setitem_indexer中 return self._convert_tuple(key, is_setter=True) 文件“/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py”,行 155,在_convert_tuple中 idx = self._convert_to_indexer(k, axis=i, is_setter=is_setter) 文件“/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py”, 第 1025 行,在 _convert_to_indexer obj = self._convert_scalar_indexer(obj, axis) 文件“/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py”,行 163,在_convert_scalar_indexer中 返回 ax._convert_scalar_indexer(key, kind=self.name) 文件“/usr/local/lib/python2.7/dist-packages/pandas/core/index.py”,行 806,在_convert_scalar_indexer中 返回 to_int() 文件“/usr/local/lib/python2.7/dist-packages/pandas/core/index.py”,行 第787章 ikey = int(key) OverflowError: cannot convert float infinity to integer
【问题讨论】:
-
print(data['occurrence'][0][key])提供了什么? -
根据您提供的 sn-p,我同意这似乎有些神秘。
try...except将是一个简单的解决方法,尽管清楚地了解正在发生的事情显然更可取。 -
尝试使用调试器:
import pdb; pdb.set_trace()。找出哪个部分引发了错误,而不是发布一大堆代码。发布完整的堆栈跟踪也没有什么坏处。 -
您确定是
df.set_value()引发了错误吗?也许 Python 在尝试评估data['occurrence'][0][key]以将其输入math.isinf()时抛出错误 -
print(data['occurrence'][0][key])给我一个“1”