'float' object has no attribute 'lower'

I'm facing this error and I'm really not able to find the reason for it.
Can somebody please point out the reason for it ?

for i in tweet_raw.comments:
    mns_proc.append(processComUni(i))

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-416-439073b420d1> in <module>()
      1 for i in tweet_raw.comments:
----> 2     tweet_processed.append(processtwt(i))
      3 

<ipython-input-414-4e1b8a8fb285> in processtwt(tweet)
      4     #Convert to lower case
      5     #tweet = re.sub('RT[\s]+','',tweet)
----> 6     tweet = tweet.lower()
      7     #Convert www.* or https?://* to URL
      8     #tweet = re.sub('((www\.[\s]+)|(https?://[^\s]+))','',tweet)

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

A second similar error that facing is this :

for i in tweet_raw.comments:
    tweet_proc.append(processtwt(i))

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-423-439073b420d1> in <module>()
      1 for i in tweet_raw.comments:
----> 2     tweet_proc.append(processtwt(i))
      3 

<ipython-input-421-38fab2ef704e> in processComUni(tweet)
     11         tweet=re.sub(('[http]+s?://[^\s<>"]+|www\.[^\s<>"]+'),'', tweet)
     12     #Convert @username to AT_USER
---> 13     tweet = re.sub('@[^\s]+',' ',tweet)
     14     #Remove additional white spaces
     15     tweet = re.sub('[\s]+', ' ', tweet)

C:\Users\m1027201\AppData\Local\Continuum\Anaconda\lib\re.pyc in sub(pattern, repl, string, count, flags)
    149     a callable, it's passed the match object and must return
    150     a replacement string to be used."""
--> 151     return _compile(pattern, flags).sub(repl, string, count)
    152 
    153 def subn(pattern, repl, string, count=0, flags=0):

TypeError: expected string or buffer

Shall I check whether of not a particluar tweet is tring before passing it to processtwt() function ? For this error I dont even know which line its failing at.



Solution 1:[1]

Just try using this:

tweet = str(tweet).lower()

Lately, I've been facing many of these errors, and converting them to a string before applying lower() always worked for me.

Solution 2:[2]

My answer will be broader than shalini answer. If you want to check if the object is of type str then I suggest you check type of object by using isinstance() as shown below. This is more pythonic way.

tweet = "stackoverflow"

## best way of doing it
if isinstance(tweet,(str,)):
    print tweet

## other way of doing it
if type(tweet) is str:
    print tweet

## This is one more way to do it
if type(tweet) == str:
    print tweet

All the above works fine to check the type of object is string or not.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1
Solution 2