Sub topic 4
How missing values change dtypes
This chapter assumes you know what a dtype is and how NumPy-backed columns store fixed-width values. It also assumes you know that the object dtype holds pointers to Python objects. Here you will see how a missing value can silently change a column's dtype, and how to prevent that.
Why a missing value matters
A NumPy int64 array has no spare bit pattern for "missing". Every one of the 64 bits is part of an integer. A float64 array does have one, because IEEE 754 floats define NaN (not a number). So when pandas has to put a missing value into an integer column, it changes the column to a dtype that can hold one. This change is called dtype promotion.
Promotion in practice
import pandas as pd
s_int = pd.Series([1, 2, 3])
s_missing = pd.Series([1, 2, None])
s_bool = pd.Series([True, False, None])
s_time = pd.Series(pd.to_datetime(["2026-01-01", None]))
for s in (s_int, s_missing, s_bool, s_time):
print(s.dtype)In pandas 2.x this prints int64, float64, object and datetime64[ns]. Each case follows a different rule:
| Original dtype | Missing marker | Dtype with a missing value |
|---|---|---|
| `int64` | `NaN` | `float64` |
| `bool` | `None` | `object` |
| `object` | `None` or `NaN` | `object` (unchanged) |
| `datetime64[ns]` | `NaT` | `datetime64[ns]` (unchanged) |
| `Int64`, `boolean`, `string` | `pd.NA` | unchanged |
Where the problem shows up
You rarely type None into a list yourself. Missing values usually appear as a side effect of an operation:
reindexor an alignment adds rows that were not in the original data.- A left
mergefinds no match for some keys. read_csvmeets empty cells in a column of integers.shiftpushes values off one end.
In each case a clean integer column can come back as float64. Two things go wrong. IDs print as 3.0 instead of 3. Large integers also lose exactness, because float64 represents integers exactly only up to 2**53.
import pandas as pd
big = 9007199254740993 # 2**53 + 1
as_float = pd.Series([big, None])
as_nullable = pd.Series([big, None], dtype="Int64")
print(as_float.dtype, int(as_float[0]))
print(as_nullable.dtype, as_nullable[0])The float column prints 9007199254740992, which is off by one. The nullable column keeps 9007199254740993.
Keeping integers with nullable dtypes
The nullable dtypes Int64, boolean and string store a separate mask of which entries are missing. The values array keeps its own dtype. The missing marker is pd.NA.
import pandas as pd
s_float = pd.Series([1, 2, None])
s_nullable = pd.Series([1, 2, None], dtype="Int64")
print(s_float.dtype)
print(s_nullable.dtype)
print(s_nullable)The output is:
float64
Int64
0 1
1 2
2 <NA>
dtype: Int64Note the capital I in Int64. Lowercase int64 is the NumPy dtype and cannot hold a missing value.
You can convert an existing column with astype, or ask pandas to pick nullable dtypes for you:
import pandas as pd
df = pd.DataFrame({"id": [1, 2, 3], "flag": [True, False, True]})
print(df.convert_dtypes().dtypes)This gives Int64 and boolean, even though the data has no missing values yet. Doing the conversion early means later operations that introduce gaps will not change the dtype.
Common errors
- Checking
df["x"].dtype == intafter a merge and findingfloat64. Look at where the gaps were introduced. - Expecting
Nonein aboollist to give a boolean column. You getobject, which is slower and larger. - Casting back with
astype("int64")while missing values remain. This raises an error, becauseNaNcannot become an integer. - Using
fillna(0)to hide gaps. The dtype may stayfloat64, and you lose the distinction between "zero" and "unknown".
Exercise
You have s = pd.Series([10, 20, 30]) with dtype int64. You run s.reindex([0, 1, 5]). What dtype does the result have, and how would you keep integers?
Show answer
The result is float64. Index label 5 does not exist in s, so reindex inserts NaN, and an int64 array cannot hold NaN. To keep integers, convert first with s.astype("Int64").reindex([0, 1, 5]). The new entry is then <NA> and the dtype stays Int64.