Python Forum
text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'enc - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'enc (/thread-4219.html)



text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'enc - ulrich48155 - Jul-31-2017

Im trying to do a sentiment analysis of Twitter tweets on different topics. Thereby, some topics produce the following error message:

AttributeError: 'float' object has no attribute 'encode' 

What shall I do?

My code looks like this:

import pandas as pd
import numpy as np from nltk.sentiment.vader
import SentimentIntensityAnalyzer

tweets=pd.read_excel('Topic1.xlsx',header=0)

sid = SentimentIntensityAnalyzer()

for sentence in tweets.Tweet: tweets['Sentiment'] = tweets['Tweet'].apply(lambda sentence: sid.polarity_scores(sentence)['compound'])

tweets.to_excel('Topic1s.xlsx')



RE: text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'enc - buran - Jul-31-2017

Please, post the full traceback, not just the last line, in error tags. Also post your full code if you have not done so (the code in thread title is not seen in your code snippet)
cross-posted at reddit 7 days ago
https://www.reddit.com/r/learnpython/comments/6p1ihf/text_strtextencodeutf8_attributeerror_float/


RE: text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'enc - ulrich48155 - Jul-31-2017

Here is the full error message. The line from the title is part from this message.

Output:
Traceback (most recent call last):   File "<ipython-input-1-2b37aa124c89>", line 10, in <module>     tweets['Sentiment'] = tweets['Tweet'].apply(lambda sentence: sid.polarity_scores(sentence)['compound'])   File "C:\Anaconda\Anaconda3\lib\site-packages\pandas\core\series.py", line 2355, in apply     mapped = lib.map_infer(values, f, convert=convert_dtype)   File "pandas\_libs\src\inference.pyx", line 1569, in pandas._libs.lib.map_infer (pandas\_libs\lib.c:66440)   File "<ipython-input-1-2b37aa124c89>", line 10, in <lambda>     tweets['Sentiment'] = tweets['Tweet'].apply(lambda sentence: sid.polarity_scores(sentence)['compound'])   File "C:\Anaconda\Anaconda3\lib\site-packages\nltk\sentiment\vader.py", line 223, in polarity_scores     sentitext = SentiText(text)   File "C:\Anaconda\Anaconda3\lib\site-packages\nltk\sentiment\vader.py", line 158, in __init__     text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'encode'