coelidonum
coelidonum

Reputation: 543

Pandas finds NaNs where there are floats instead

I was trying to convert the floats in a column into int.

zbill['ACTIVITY START MONTH']=zbill['ACTIVITY START MONTH'].astype(int)

It returned this error:

ValueError: Cannot convert non-finite values (NA or inf) to integer

I thougth it was strange, since there should not be any NaN. So I got the rows where in theory there should be NaNs.

missing_values_list= zbill[zbill['ACTIVITY START MONTH'].isnull()].index.tolist()
zbill.iloc[missing_values_list]

What I found out is that actually there are no NaNs! But only floats... I get something like this:

A | B | ACTIVITY START MONTH 
______________________________
A | R | 11.0
W | S | 9,0
....

How is it even possible? Pandas do think that there are NaNs in that column.

zbill['ACTIVITY START MONTH'].isnull().values.any()
True

Why?

Upvotes: 1

Views: 37

Answers (1)

jezrael
jezrael

Reputation: 862601

For get rows with missing values or with infinitive values use boolean indexing only:

df = zbill[zbill['ACTIVITY START MONTH'].isnull() | 
           np.isinf(zbill['ACTIVITY START MONTH']) ]

In your solution use DataFrame.loc instead iloc for selecting by labels, not by positions:

df = zbill.loc[missing_values_list]

Upvotes: 2

Related Questions