pandas
data cleaning
python
NaN handling
data type conversion

Convert Pandas column containing NaNs to dtype int

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

A regular NumPy-backed integer dtype cannot represent missing values, so converting a pandas column with NaN directly to int fails. The correct solution depends on what you want missing values to mean. If missing values should stay missing, use pandas' nullable integer dtype such as Int64. If missing values should become a concrete value like 0, fill them before conversion.

Why Plain int Fails

Traditional integer arrays do not have a built-in marker for missing data. In pandas, a column containing NaN is often stored as floating-point data because floating-point types can represent NaN.

python
1import pandas as pd
2
3s = pd.Series([1, 2, None])
4print(s)
5print(s.dtype)
6
7try:
8    print(s.astype(int))
9except Exception as exc:
10    print(type(exc).__name__, exc)

That conversion raises an error because pandas cannot map NaN into a standard integer value automatically.

Use Nullable Int64 to Preserve Missing Values

If you want integer semantics and still need missing entries, use the pandas nullable integer dtype.

python
1import pandas as pd
2
3s = pd.Series([1, 2, None])
4s2 = s.astype("Int64")
5
6print(s2)
7print(s2.dtype)

This produces a column that behaves like integers but can also hold pandas missing values. Note that Int64 with a capital I is pandas' nullable extension dtype, not the same as NumPy int64.

This is usually the best answer when the missing values are real missing data, not placeholders to be overwritten.

Fill Missing Values First When the Domain Requires It

Sometimes a business rule says that missing values should become 0, -1, or some other sentinel. In that case, fill first and then convert.

python
1import pandas as pd
2
3s = pd.Series([1, 2, None, 4])
4s2 = s.fillna(0).astype(int)
5
6print(s2)
7print(s2.dtype)

This gives a plain integer column because there are no missing values left.

Use this only when the replacement value is semantically correct. Replacing missing values with 0 is common, but it can also hide data-quality problems if 0 is a legitimate value with a different meaning.

Convert String Columns Safely

In real data, the column may contain strings, empty values, and invalid tokens, not just numeric values plus NaN. In that case, normalize the column first with pd.to_numeric.

python
1import pandas as pd
2
3s = pd.Series(["1", "2", None, "bad", "5"])
4parsed = pd.to_numeric(s, errors="coerce")
5result = parsed.astype("Int64")
6
7print(result)

errors="coerce" turns invalid values into missing values, after which the nullable integer dtype can preserve them cleanly.

Be Explicit in Data Pipelines

When cleaning a dataframe, it is better to make the missing-value policy visible in code rather than relying on one dense conversion line. For example:

python
1import pandas as pd
2
3df = pd.DataFrame({"age": [30, None, 41]})
4
5df["age"] = (
6    pd.to_numeric(df["age"], errors="coerce")
7    .astype("Int64")
8)
9
10print(df)
11print(df.dtypes)

This shows the exact intent:

  • parse values numerically
  • treat invalid data as missing
  • store as nullable integers

That clarity matters when the dataframe is later written to parquet, CSV, or a database.

Common Pitfalls

  • Using astype(int) on data that still contains NaN and expecting pandas to guess a replacement value.
  • Confusing pandas Int64 with NumPy int64.
  • Filling missing values with 0 when 0 has a real business meaning different from missing.
  • Forgetting to normalize string values before conversion.
  • Applying the conversion without checking downstream storage or serialization expectations for nullable integers.

Summary

  • Plain integer dtypes cannot represent NaN, so direct conversion fails.
  • Use pandas nullable Int64 when missing values should remain missing.
  • Use fillna(...) before conversion only when a concrete replacement value is correct.
  • 'pd.to_numeric(..., errors="coerce") is useful when the source column contains strings or invalid values.'
  • Choose the conversion strategy based on the meaning of missing data, not only on dtype convenience.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design