Matt
Matt

Reputation: 33

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!

Upvotes: 3

Views: 2295

Answers (1)

jezrael
jezrael

Reputation: 862481

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

Upvotes: 1

Related Questions