title: Exception handling when applying function to pandas df
body: <p>I want to do some language detection using the python package textblob: I created a new column in a pandas df which should contain the detected language:</p>
  
  <pre><code>from textblob import TextBlob
  posts['Language']=posts['Caption'].apply(TextBlob.detect_language)
  </code></pre>
  
  <p>This code works. However, with one df it interrupts and throws an exeception ('TranslatorError') where the respective row contains less then 3 character. Therefore, I'd like write a function which ensures that the 'TextBlob.detect_language' function gets applied to the full df even when an exception occurs. </p>
  
  <p>I thought about something like that:</p>
  
  <pre><code>def get_language(r):
      try:
          return r.TextBlob.detect_language()
      # except (r.TextBlob.detect_language==TranslatorError):
          return np.nan # where textblob was not able to detect language -&gt; nan
  </code></pre>
  
  <p>However, I don't know what to write after the (outcommented) "except" clause. Any help?</p>
  
  <p>The current function applied (with the except not outcommented)</p>
  
  <pre><code>posts['Language']=posts['Caption'].apply(get_language)
  </code></pre>
  
  <p>returns</p>
  
  <blockquote>
    <p>AttributeError: 'TextBlob' object has no attribute 'TextBlob'</p>
  </blockquote>
  
  <p>if I try</p>
  
  <pre><code>def get_language(r):
      try:
          return r.TextBlob.detect_language()
      except:
          pass # (or np.nan)
  </code></pre>
  
  <p>it just passes all the rows, i.e. doesn't detect the language for any row...</p>
  
  <p>Thanks for help guys!</p>