Sub topic 8
Keeping missing values with nullable dtypes
This chapter assumes you can build a DataFrame, select columns, and have used pd.to_numeric and astype as shown earlier in this tutorial. It covers what happens to an integer column when it contains missing values, and how nullable dtypes avoid the problem.
The problem: NaN forces floats
Classic NumPy integer columns cannot hold a missing value. When you add one, pandas converts the whole column to float64.
import pandas as pd
s = pd.Series([1, 2, None])
print(s.dtype)This prints float64. Your integers are now 1.0 and 2.0, and NaN marks the gap. That is a problem for IDs, counts, and quantities, which should stay whole numbers. It also breaks code that expects integers, such as an ID that becomes 1.0 when you format it as text.
Nullable dtypes
pandas offers extension dtypes that store missing values without changing the column type. Their names start with a capital letter or are plain words, which sets them apart from the NumPy types.
| Nullable dtype | Replaces | Holds |
|---|---|---|
| `Int64` (also `Int8`, `Int16`, `Int32`) | `int64` | whole numbers |
| `Float64` | `float64` | decimals |
| `boolean` | `bool` | True, False |
| `string` | `object` for text | text |
Missing values in these columns appear as pd.NA, a single marker that works for every nullable type.
s = pd.Series([1, 2, None], dtype="Int64")
print(s)
print(s.dtype)The values print as 1, 2, and <NA>, and the dtype is Int64. Note the capital I. int64 is the NumPy type and cannot hold <NA>.
Converting an existing column
The usual pattern is to parse the text first, then cast the result. pd.to_numeric returns float64 when it meets missing values, and astype("Int64") converts that to a nullable integer.
qty = pd.Series(["3", "5", None])
qty = pd.to_numeric(qty).astype("Int64")
print(qty)The cast works only if every non-missing float is a whole number. A value such as 2.5 raises a TypeError, because pandas will not silently truncate it. That error is useful. It tells you the column holds data you did not expect.
For text columns, astype("string") gives the nullable string dtype. Missing entries become <NA> instead of staying as a mix of NaN and None inside an object column.
How pd.NA behaves
pd.NA means "unknown value", and it follows that meaning. Comparing it to anything gives another unknown, not False.
s = pd.Series([1, None, 3], dtype="Int64")
print(s > 1)
print(s.isna().sum())
print(s.fillna(0))
print(s.sum())s > 1returns abooleanSeries:False,<NA>,True.isna()counts the missing entry, so the sum is1.fillna(0)replaces the gap and keeps the dtypeInt64.sum()skips missing values by default and returns4.
Logical operators follow three-valued logic. True | pd.NA is True, because the answer is known whatever the unknown value is. False | pd.NA is <NA>.
Common errors
- Using a missing value in an
ifstatement.bool(pd.NA)raises aTypeError. Test withpd.isna(value)instead. - Filtering with a mask that contains
<NA>. Boolean indexing with abooleanmask that holds<NA>raises an error in many versions. Fill the mask first, for examplemask.fillna(False). - Expecting
NaNchecks to differ.isna()andnotna()work the same on both kinds of missing value, so keep using them. - Mixing both markers. A column that came from
objectdata may holdNone,NaN, andpd.NA. Converting withastype("string")orastype("Int64")makes them allpd.NA.
When to use them
Use nullable dtypes when the column is conceptually an integer, boolean, or text field and some entries are legitimately unknown. Plain NumPy dtypes remain fine for complete columns and for float data where NaN is acceptable. Some older libraries do not accept extension dtypes, so convert back with astype("float64") or to_numpy() at that boundary.
Exercise
You run pd.to_numeric(pd.Series(["4", "x", None]), errors="coerce").astype("Int64"). What values and dtype do you get, and what information is lost?
Show answer
The result is Int64 with values 4, <NA>, <NA>. The to_numeric step turns both "x" and None into NaN, and astype converts them to <NA>. You can no longer tell the unparseable "x" from the genuinely missing entry, so compare against the original column before converting if you need to find bad values.