Convert floats to ints in Pandas?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting floats to integers in pandas is simple syntactically, but the correct method depends on how you want to handle decimals and missing values. A direct cast truncates toward zero, while nullable integer types and explicit rounding are often safer for real datasets.
Direct Conversion With astype
If a column contains whole numbers stored as floats, you can cast it directly:
This works when every value is finite and already integer-like. It is the fastest and simplest option for clean data.
astype(int) Does Not Round
The most important behavior to remember is that astype(int) truncates rather than rounds.
The output drops the decimal part:
- '
1.9becomes1' - '
2.1becomes2' - '
-3.7becomes-3'
That may be correct for some workloads, but it is often not what people mean when they say "convert floats to ints".
Round First When That Matches the Data Rule
If the intended behavior is rounding, do that explicitly before converting:
If your business rule is floor or ceiling rather than generic rounding, use NumPy:
This makes the intent clear and avoids silent data loss from the wrong conversion rule.
Missing Values Need Nullable Integers
Standard NumPy integer dtypes cannot store NaN, so this fails:
For nullable data, use pandas' nullable integer dtype:
The capital I matters. "Int64" is the pandas nullable type, while int64 is the standard non-nullable integer dtype.
Convert Selected DataFrame Columns
Real work usually happens on DataFrames rather than isolated Series. Convert only the columns that should become integers.
That avoids accidentally converting columns such as prices or measurements that should stay floating point.
Clean Dirty Inputs First
If the column contains strings such as "4.0" or invalid values, normalize it before casting:
errors="coerce" turns invalid entries into missing values so the conversion can proceed safely.
Validate Integer-Like Data Before Casting
In ETL pipelines, it is often worth checking whether a float column really behaves like integer data before converting it.
If this check returns False, a plain cast would lose fractional information.
Common Pitfalls
The biggest mistake is assuming astype(int) rounds values. It does not; it truncates toward zero.
Another common issue is casting a column with missing values to a normal integer dtype instead of using "Int64".
People also sometimes convert entire DataFrames when only a few columns should become integers, which can quietly damage numeric columns that need decimals.
Finally, do not skip input cleaning. String values, invalid tokens, and mixed types should usually go through pd.to_numeric() before integer conversion.
Summary
- Use
astype(int)only when direct truncation is truly the desired rule. - Round, floor, or ceil explicitly when the data semantics require it.
- Use pandas
"Int64"for integer columns that may contain missing values. - Convert only the DataFrame columns that genuinely represent whole numbers.
- Clean mixed or dirty inputs with
pd.to_numeric()before casting.

