1
votes

I get the error

OverflowError: cannot convert float infinity to integer

from this code:

if not math.isinf(data['occurrence'][0][key]):
                   df.set_value(df.date == key, name, data['occurrence'][0][key])

How come the set_value part gets executed anyway? How to fix this?

EDIT:

Full Stack Trace:

Traceback (most recent call last): File "aggregateData.py", line 27, in df.set_value(df.date == key, name, data['occurrence'][0][key]) #update df File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1690, in set_value self.loc[index, col] = value File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py", line 114, in setitem indexer = self._get_setitem_indexer(key) File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py", line 106, in _get_setitem_indexer return self._convert_tuple(key, is_setter=True) File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py", line 155, in _convert_tuple idx = self._convert_to_indexer(k, axis=i, is_setter=is_setter) File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py", line 1025, in _convert_to_indexer obj = self._convert_scalar_indexer(obj, axis) File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py", line 163, in _convert_scalar_indexer return ax._convert_scalar_indexer(key, kind=self.name) File "/usr/local/lib/python2.7/dist-packages/pandas/core/index.py", line 806, in _convert_scalar_indexer return to_int() File "/usr/local/lib/python2.7/dist-packages/pandas/core/index.py", line 787, in to_int ikey = int(key) OverflowError: cannot convert float infinity to integer

1
What does print(data['occurrence'][0][key]) give?Simeon Visser
Based on the snippet you gave I agree that it seems somewhat mysterious. try...except would be a simple work-around, although it would clearly be preferable to understand exactly what is happening.John Coleman
Try poking around with the debugger: import pdb; pdb.set_trace(). Figure out which part is throwing the error instead of posting a big blob of code. Posting the full stack trace doesn't hurt either.darkfeline
Are you sure that it is df.set_value() which is throwing the error? Perhaps Python throws the error when it tries to evaluate data['occurrence'][0][key] in order to feed it into math.isinf()John Coleman
print(data['occurrence'][0][key]) gives me a '1'Pete

1 Answers

0
votes

The end of the stack trace

"/usr/local/lib/python2.7/dist-packages/pandas/core/index.py", line 787, in to_int ikey = int(key) OverflowError: cannot convert float infinity to integer

strongly suggests that it is the key that can't be converted to an integer (especially given the fact that data['occurrence'][0][key] seems to be 1. What does print key print?