title: I got an error while loop statement in python
body: <p>My statement with While Loop got an error which showing me the error is in class </p>
  
  <pre><code>batchsize = 10
  train_iter = iterators.SerialIterator(train, batchsize)
  max_epoch = 90
  studyThreshold = 1
  needStudy = True
  
  try:            
      while(train_iter.epoch &lt; max_epoch) and needStudy:
          train_batch = train_iter.next()
          x, t = concat_examples(train_batch)
          print(t)
          y = model(x)
          loss = F.mean_squared_error(y, t)
          model.cleargrads()
          loss.backward()
          optimizer.update()
          if train_iter.is_new_epoch:
              print("epoch", train_iter.epoch, "loss=", loss.data, end=" ")
              loss_X.append(train_iter.epoch)
              loss_Y.append(loss.data)
  except Exception as e: # replaced with Exception to handle other errors
      print('except :',type(e)) 
  </code></pre>
  
  <p>and the output is exception :</p>
  
  <pre><code>except : &lt;class 'KeyError'&gt;
  </code></pre>
  
  <p>Note: I am using Pandas Package</p>
  
  <p>The whole error I got </p>
  
  <pre><code>except : &lt;class 'KeyError'&gt;
  ---------------------------------------------------------------------------
  KeyError                                  Traceback (most recent call last)
  ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
     2656             try:
  -&gt; 2657                 return self._engine.get_loc(key)
     2658             except KeyError:
  
  pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
  
  pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
  
  pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
  
  pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
  
  KeyError: 33617
  
  During handling of the above exception, another exception occurred:
  
  KeyError                                  Traceback (most recent call last)
  &lt;ipython-input-57-7a80e54a4aea&gt; in &lt;module&gt;
       35     while((train_iter.epoch &lt; max_epoch) and needStudy):
  ---&gt; 36         train_batch = train_iter.next()
       37         x, t = concat_examples(train_batch)
  
  ~\Anaconda3\lib\site-packages\chainer\iterators\serial_iterator.py in __next__(self)
       76 
  ---&gt; 77         batch = [self.dataset[index] for index in indices]
       78         return batch
  
  ~\Anaconda3\lib\site-packages\chainer\iterators\serial_iterator.py in &lt;listcomp&gt;(.0)
       76 
  ---&gt; 77         batch = [self.dataset[index] for index in indices]
       78         return batch
  
  ~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
     2926                 return self._getitem_multilevel(key)
  -&gt; 2927             indexer = self.columns.get_loc(key)
     2928             if is_integer(indexer):
  
  ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
     2658             except KeyError:
  -&gt; 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
     2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
  
  pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
  
  pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
  
  pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
  
  pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
  </code></pre>