'Textblob issue ---- TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'float'>

I am trying to conduct sentiment analysis on a dataset of online reviews, but am having issues with the data. Specifically, here is my code:

    ##read in csv from file path
df = pd.read_csv ('/Filepath.csv', encoding='utf8')

Then I set up the packages:

    ##load NLP packages
from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

def detect_tb_polarity(text):
    return TextBlob(text).sentiment.polarity

def detect_tb_subjectivity(text):
    return TextBlob(text).sentiment.subjectivity

def detect_vader_pos(text):
    return vader.polarity_scores(text)['pos']

def detect_vader_neg(text):
    return vader.polarity_scores(text)['neg']

def detect_vader_comp(text):
    return vader.polarity_scores(text)['compound']

Next line:

df['tb_polarity'] = df.review.apply(detect_tb_polarity)
df['tb_subjectivity'] = df.review.apply(detect_tb_subjectivity)


vader = SentimentIntensityAnalyzer()
df['vader_pos'] = df.review.apply(detect_vader_pos)
df['vader_neg'] = df.review.apply(detect_vader_neg)
df['vader_comp'] = df.review.apply(detect_vader_comp)

However, after trying to run the packages, I get the error:

TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'float'>

This is being triggered by the following code line: df['tb_polarity'] = df.review.apply(detect_tb_polarity)

I cannot figure out how to change the review text in the dataframe to a string. In addition, when checking for class using:

print(type(df.review))

It says "<class 'pandas.core.series.Series'>"

Any suggestions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source