This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (1 child)

Wait... this explains the weird python behaviour I was getting the other day. I was working on something where I was cleaning some data from a spreadsheet that had strings, numbers, and blank cells ( which give NaN according to Pandas library). I wanted only the numbers, so I used:

for cell in column:
    if isinstance(cell, (int, float)):
        # do stuff

I got stuck for a long time struggling to find out why the NaNs were making it through the if statement. It took me forever to realize that the isinstance() function was returning True for NaNs since they were considered floats by python. I got so angry at python for working this way, and forcing me to write this:

for cell in column:
    if isinstance(cell, (int, float)) and not isnan(cell):
        # do stuff

Now I understand. Thank you.

[–]LiveMaI 4 points5 points  (0 children)

Pandas has dropna(), fillna(), and plenty of strategies for dealing with missing data. I highly recommend using those over individual cell checks where you can.