Skip to content

BUG: Pandas converts nullable int to float, even when this loses data #63903

@Nodrance

Description

@Nodrance

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
def add_two(x):
    if pd.isna(x):
        return pd.NA
    return x+2

sequence = [10000000000000001,None] #above the float integer precision limit
df1 = pd.DataFrame(sequence, dtype='Int64')
df1[0] = df1[0].apply(add_two).astype('Int64')
print(int(df1[0][0])) # 002, not 003

Issue Description

Anytime there's a null value in a series when using .apply() (or creating a dataframe with no dtype specified), it will be converted to a float, even if the data type it already is can support null values, and is a better fit for the data, and this results in loss of precision compared to not converting

Possibly related to #62710

Expected Behavior

Pandas should not convert data types to a float when this loses data and is a worse fit for the data.

Installed Versions

Details

INSTALLED VERSIONS

commit : 366ccdf
python : 3.14.2
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.26200
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_Canada.1252

pandas : 3.0.0
numpy : 2.4.1
dateutil : 2.9.0.post0
pip : 25.3
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
psycopg2 : None
pymysql : None
pyarrow : None
pyiceberg : None
pyreadstat : None
pytest : None
python-calamine : None
pytz : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions