Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
d = {'col1': [1, None]}
df = pd.DataFrame(data=d).astype(int, errors='ignore')
print(df.iloc[0].dtypes)
# Output: float64
# Replace NaN with a float
df.fillna(value={"col1": 9999.0}, inplace=True)
df = df.astype(int, errors='ignore')
print(df.iloc[0].dtypes)
# Output: int64
Issue Description
astype(int, errors ='ignore')
should ignore NaNs
and convert other values, but that is not what happens, instead, all values are left unchanged
If NaNs
are converted to any given float, and astype(int, errors ='ignore')
applied again, it works. But that's not the expected behavior.
Expected Behavior
astype(int, errors ='ignore')
should ignore NaNs
and convert other values.
Installed Versions
Comment From: phofl
Hi, thanks for your report.
A dtype is always column based. As soon as you have one float, all values will be float. If you want nullable values in a int column you'll have to use our nullable dtypes, e.g. Int64 in this case