Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Panda astype not converting column to int even when using errors=ignore

I have the following DF

    ID
0   1.0
1   555555.0
2   NaN
3   200.0

When I try to convert the ID column to Int64 I got the following error:

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

I’ve used the following code to solve this problem:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df["ID"] = df["ID"].astype('int64', errors='ignore')

Although, when I use the above code my ID column persists with float64 type.

Any tip to solve this problem?

>Solution :

Use pd.Int64DType64 instead of np.int64:

df['ID'] = df['ID'].fillna(pd.NA).astype(pd.Int64Dtype())

Output:

>>> df
       ID
0       1
1  555555
2    <NA>
3     200

>>> df['ID'].dtype
Int64Dtype()

>>> df['ID'] + 10
0        11
1    555565
2      <NA>
3       210
Name: ID, dtype: Int64

>>> print(df.to_csv(index=False))
ID
1
555555
""
200
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading