Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check float using isinstance in Python?

Tags:

python

I cannot make sense of below: When I check the type of "RUN_ID", it clearly states "float". But when I check if it is float, it returns False. What is this type exactly and how to check?

tags.loc[0, 'RUN_ID']
Out[36]: 38607.0   
type(tags.loc[0, 'RUN_ID'])
Out[34]: float 
isinstance(type(tags.loc[0, 'RUN_ID']),np.float64)
Out[32]: False
isinstance(type(tags.loc[0, 'RUN_ID']),np.float32)
Out[33]: False

isinstance(type(tags.loc[0, 'RUN_ID']),float)
Out[35]: False
like image 226
Lisa Avatar asked Jun 28 '26 09:06

Lisa


2 Answers

You're using isinstance wrong. Instead of

isinstance(type(tags.loc[0, 'RUN_ID']),float)

just do

isinstance(tags.loc[0, 'RUN_ID'],float)

The type() function returns an object of type type, where you want the type of the object itself.

like image 162
Green Cloak Guy Avatar answered Jun 30 '26 21:06

Green Cloak Guy


check with the regular float type instead of numpy. Test results below

w = isinstance(n, float)    # True
x = isinstance(n, np.float32)   # False
y = isinstance(n, np.float64)   # False
z = type(n) == float        # True
like image 40
Joshua Nixon Avatar answered Jun 30 '26 21:06

Joshua Nixon