TypeError: cannot unpack non-iterable int objec

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:

def get_grps(s, thresh=-1, Nmin=3):
    # ...
    if gps.isnull().all():
        return None
    else:
        # ...
        return agg, gps

for i in range(len(data_spi)-70):
    ts = data_spi[i:i+10, x, y].fillna(1)

    result = get_grps(pd.Series(ts), thresh=-1, Nmin=3)
    if result is None:
        break

    agg, gps = result

    duration = np.nanmean(agg['sum'])
    frequency = len(agg['sum'])

Leave a Comment