How to get rid of warning "DeprecationWarning generator 'ngrams' raised StopIteration"

langkilde picture langkilde · Apr 25, 2017 · Viewed 7.8k times · Source

While working on a Kaggle notebook I ran into an issue. The following code block:

from nltk import ngrams
def grams(tokens):
    return list(ngrams(tokens, 3))
negative_grams = preprocessed_negative_tweets.apply(grams)

resulted in a red box appearing saying

/opt/conda/bin/ipython:5: DeprecationWarning: generator 'ngrams' raised StopIteration

The variable preprocessed_negative_tweets is a Pandas data frame containing tokens.

Anyone know how to make this go away?

(Full notebook available here)

Answer

carla08 picture carla08 · Oct 6, 2017

To anyone else who doesn't want or can't suppress the warning.

This is happening because ngrams is raising StopIteration exception to end a generator, and this is deprecated from Python 3.5.

You could get rid of the warning by changing the code where the generator stops, so instead of raising StopIteration you just use Python's keyword return.

More on: PEP 479