0
  1. 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 to break up a dataframe column into chunks of n = 15,000.

  2. 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 the StopIteration 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: 
Pedro Maia
  • 2,666
  • 1
  • 5
  • 20
hector.h2913
  • 41
  • 2
  • 8
  • 3
    `StopIteration` isn't an error as such -- it's an exception thrown for flow-control purposes when something calls `next()` after the end of an iterator. It's normal for it to happen; what's abnormal is for code to be using `next()` manually without catching that exception or being sure there are enough items available to take that it'll never happen. – Charles Duffy Jan 17 '22 at 00:51
  • 1
    That is to say, anything doing a bunch of `next()` calls should be prepared for one of them to fail. – Charles Duffy Jan 17 '22 at 00:52
  • 1
    Or to put it more succinctly, you're running off the end of the frame – Frank Yellin Jan 17 '22 at 00:53

1 Answers1

0

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
>>> 
Tim Peters
  • 67,464
  • 13
  • 126
  • 132