title: Sklearn CountVectorizer "Empty Vocabulary" error in Dataframe when computing nGram
body: <p>I have a dataframe (<code>data</code>) with 3 records:</p>
  
  <pre><code>id    text
  0001  The farmer plants grain
  0002  The fisher catches tuna
  0003  The police officer fights crime
  </code></pre>
  
  <p>I group that dataframe by id:</p>
  
  <pre><code>data_grouped = data.groupby('id')
  </code></pre>
  
  <p>Describing the resulting groupby object shows that all the records remain.</p>
  
  <p>I then run this code to find the nGrams in the <code>text</code> and join them to the <code>id</code>:</p>
  
  <pre><code>word_vectorizer = CountVectorizer(stop_words=None, ngram_range=(2,2), 
  analyzer='word')
  for id, group in data_grouped:
         X = word_vectorizer.fit_transform(group['text'])
         frequencies = sum(X).toarray()[0]
         results = pd.DataFrame(frequencies, columns=['frequency'])
         dfinner = pd.DataFrame(word_vectorizer.get_feature_names())
         dfinner['id'] = id
         final = results.join(dfinner)
  </code></pre>
  
  <p>When I run all of this code together, an error kicks out for the <code>word_vectorizer</code> that states "empty vocabulary; perhaps the documents only contain stop words". I know this error has been mentioned in many other questions, but I couldn't find one that deals with a Dataframe. </p>
  
  <p>To further complicate the issue, the error doesn't always show up. I am pulling the data from a SQL DB, and depending on how many records I pull in, the error may or may not show up. For instance, pulling in <code>Top 10</code> records causes the error, but <code>Top 5</code> doesn't. </p>
  
  <p>EDIT:</p>
  
  <p>Complete Traceback</p>
  
  <pre><code>Traceback (most recent call last):
  
    File "&lt;ipython-input-63-d261e44b8cce&gt;", line 1, in &lt;module&gt;
      runfile('C:/Users/taca/Documents/Work/Python/Text Analytics/owccomments.py', wdir='C:/Users/taca/Documents/Work/Python/Text Analytics')
  
    File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
      execfile(filename, namespace)
  
    File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
      exec(compile(f.read(), filename, 'exec'), namespace)
  
    File "C:/Users/taca/Documents/Work/Python/Text Analytics/owccomments.py", line 38, in &lt;module&gt;
      X = word_vectorizer.fit_transform(group['cleanComments'])
  
    File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 839, in fit_transform
      self.fixed_vocabulary_)
  
    File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 781, in _count_vocab
      raise ValueError("empty vocabulary; perhaps the documents only"
  
  ValueError: empty vocabulary; perhaps the documents only contain stop words
  </code></pre>