Why am I getting the "stop iteration" error and how do I fix this?
-
I get the
StopIteration
error when I run the code below. I saw some other posts on stack overflow with this same error but not sure they fit this case. Why is this happening and how do I fix it? The goal is tobreak
up a dataframe column into chunks ofn = 15,000
. -
Also I know the last line is redundant. I read something on Stack Overflow that seemed to suggest the
next()
part needs to be in a for loop. Would this fix theStopIteration
issue? And if so, would that for loop be a separate function or can I put it within the code below? I tried the the latter but haven't been successful.
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
outputs = chunks(df['Delay Comment'] ,15000)
c15k,c30k,c45k,c60k,c75k,c90k,c105k,c120k,c135k,c150k,c165k,c180k,c195k,c210k,c225k,c240k,c255k,c270k,c285k,c300k,c315k,c330k = next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs)
Error: ---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
C:\Users\HECTOR~1.HER\AppData\Local\Temp/ipykernel_9116/1126382813.py in <module>
5
6 outputs = chunks(df['Delay Comment'] ,15000)
----> 7 c15k,c30k,c45k,c60k,c75k,c90k,c105k,c120k,c135k,c150k,c165k,c180k,c195k,c210k,c225k,c240k,c255k,c270k,c285k,c300k,c315k,c330k = next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs)
StopIteration:
The exception has nothing in particular to do with dataframes. It's what happens if you try to extract more data from any iterator that's already finished its life:
>>> def f():
... yield 1
... yield 2
>>> output = f()
>>> next(output)
1
>>> next(output)
2
>>> next(output)
Traceback (most recent call last):
...
StopIteration
>>>