简体   繁体   中英

How to force Pandas to read numbers as “int64” instead of “float64”

I have an issue where all of the legacy code I have written no longer works.

I have pandas reading an Excel file, and instead of reading as int64, it now reads as float64. It is an issue because I cannot perform .merge or .isin on different data type. I know I can use df.blah.astype(int), but that is very inconvenient to refactor and seems like it should not be necessary.

I am not sure what has caused this. I am working on a new build of Windows 10, Python 3.5, and Pandas 18.1. All that changed was an upgrade from Windows 7 and Pandas 18.0.

Was there any change to Pandas? I cannot find any change in the release notes.

Thanks for any help!

I think in problematic column between int values are some NaN . So pandas automatically convert int to float .

Na type promotions .

You can check values by isnull with boolean indexing :

import pandas as pd
import numpy as np

df = pd.DataFrame({'A':[1,2,np.nan],
                   'B':[4,5,6]})

print (df)
     A  B
0  1.0  4
1  2.0  5
2  NaN  6

print (df[df.A.isnull()])
    A  B
2 NaN  6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM