title: How do I handle an error despite the code is in a try block
body: <p>I put my block of code that I know is generating the error in a <code>try</code> block but I still keep getting a KeyError <code>error</code>. I know the reason for getting this error is that sometimes the dataframe is empty</p>
  <pre><code>try:
      df = df.groupby(by='col1')['col2'].agg(total = 'sum').reset_index()
  except KeyError:
      raise Exception from None
  </code></pre>
  <p>Why am I still getting an error message when I have already put my code in a <code>try</code> block. How do I get around this?</p>