pandas
python
dataframes
type conversion
programming

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:

python
1import pandas as pd
2
3s = pd.Series([1.0, 2.0, 3.0])
4converted = s.astype(int)
5
6print(converted)
7print(converted.dtype)

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.

python
s = pd.Series([1.9, 2.1, -3.7])
print(s.astype(int))

The output drops the decimal part:

  • '1.9 becomes 1'
  • '2.1 becomes 2'
  • '-3.7 becomes -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:

python
s = pd.Series([1.9, 2.1, -3.7])
rounded = s.round().astype(int)
print(rounded)

If your business rule is floor or ceiling rather than generic rounding, use NumPy:

python
1import numpy as np
2
3s = pd.Series([1.9, 2.1, -3.7])
4
5print(np.floor(s).astype(int))
6print(np.ceil(s).astype(int))

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:

python
s = pd.Series([1.0, None, 3.0])
# s.astype(int)

For nullable data, use pandas' nullable integer dtype:

python
1s = pd.Series([1.0, None, 3.0])
2converted = s.astype("Int64")
3
4print(converted)
5print(converted.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.

python
1df = pd.DataFrame({
2    "quantity": [1.0, 2.0, 3.0],
3    "price": [10.5, 20.2, 30.9],
4    "rating": [4.0, 5.0, 3.0]
5})
6
7df["quantity"] = df["quantity"].astype(int)
8df["rating"] = df["rating"].astype(int)
9
10print(df)
11print(df.dtypes)

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:

python
1df = pd.DataFrame({"value": ["1.0", "2.0", "bad", None]})
2
3df["value"] = pd.to_numeric(df["value"], errors="coerce")
4df["value"] = df["value"].astype("Int64")
5
6print(df)

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.

python
1def is_integer_like(series: pd.Series) -> bool:
2    clean = series.dropna()
3    return (clean == clean.astype(int)).all()
4
5
6s = pd.Series([1.0, 2.0, 3.0])
7print(is_integer_like(s))

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.

Course illustration
Course illustration

All Rights Reserved.