Skip to content

BUG: ENH: Improve IntCastingNaNError message to suggest solutions for NaN in integer conversion #62838

@ADITYA-kus

Description

@ADITYA-kus

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
import numpy as np

print(pd.__version__)

df = pd.DataFrame({
    "roll": [12, 14, np.nan],
    "age": [20, np.nan, 30]
})

print("Original DataFrame:")
print(df)
print(f"Data types: {df.dtypes}")

print("\nAttempting to convert 'roll' column to int64:")
df['roll'] = df['roll'].astype('int64')
print(df)

Issue Description

Throwing an unhelpful error when converting float columns with NaN values to integer.

Original DataFrame:
roll age
0 12.0 20.0
1 14.0 NaN
2 NaN 30.0
Data types: roll float64
age float64
dtype: object

Attempting to convert 'roll' column to int64:
raise IntCastingNaNError(
"Cannot convert non-finite values (NA or inf) to integer"
)
pandas.errors.IntCastingNaNError: Cannot convert non-finite values (NA or inf) to integer

Expected Behavior

The error message should provide actionable guidance to help users understand how to handle NaN values when converting to integer types.

Installed Versions

python : 3.13.3
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.26200
machine : AMD64
processor : Intel64 Family 6 Model 154 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_India.1252

pandas : 2.2.3
numpy : 2.2.6
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.2
Cython : None
sphinx : None
IPython : 9.3.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.4
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : 5.4.0
matplotlib : 3.10.3
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 20.0.0
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.15.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Dtype ConversionsUnexpected or buggy dtype conversionsEnhancementError ReportingIncorrect or improved errors from pandasNeeds 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