Skip to content
Advertisement

TypeError: cannot unpack non-iterable int objec

How can I solve this error After running my code as follows . I am using the function below and implementin running window for loop on it but end up getting the error below. The for loop works and hungs at a point.

JavaScript

I get this error

JavaScript

How can I resolve this error?

Advertisement

Answer

Just replace return 0 by return 0, 0, or better: raise an error instead of returning 0

When your if condition is True, you only return 0. Then later, when you do agg, gps = get_grps(...), you tell python to unpack the result of the function. Then, python is expecting a 2-length iterable, and try to unpack it, but as it says: it ‘cannot unpack non-iterable int object’…

So a quick workaround is to return a tuple (0, 0) with return 0, 0, but it is quite bad because you return integers where objects are expected. your script will crash on the next line duration = np.nanmean(agg['sum']) (since agg is 0).

Some cleaner solutions to handle this case would be to unpack in a second time:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement