Sub topic 1
How pandas represents missing values
Prerequisites
This chapter assumes you can do the following:
- Read and write basic Python, including
try/exceptand list printing. - Create a pandas
SeriesandDataFrameand check a column'sdtype. - Recognize
np.nanfrom NumPy as "not a number".
Later chapters cover how to detect, drop and fill missing values. This chapter covers what a missing value is inside pandas, because every later behavior follows from it.
Why pandas needs a marker
Real data has gaps. A survey respondent skips a question, a sensor drops a reading, or a join finds no match. Pandas needs a way to say "there is no value here" inside a column that otherwise holds numbers, text or dates.
Python has no single built-in for this. Pandas therefore uses several markers, and which one you see depends on the column's dtype (the data type that pandas stores for the whole column). Most surprises with missing data come from not knowing which marker a column uses.
The four markers
| Marker | Where it appears | Notes |
|---|---|---|
| `np.nan` (NaN) | Float columns, and object columns that hold NaN | A float. Never equal to anything, including itself. |
| `None` | Object columns | Python's own "nothing" value. Often converted to NaN in numeric columns. |
| `pd.NaT` | Datetime, timedelta and period columns | "Not a Time". |
| `pd.NA` | Nullable dtypes such as `Int64`, `boolean` and `string` | A single marker that works for any nullable dtype. |
The sections below take them one at a time.
NaN: the float marker
np.nan is an ordinary Python float that follows the IEEE 754 floating-point standard. That standard says NaN is not equal to any number, including NaN.
import numpy as np
import pandas as pd
print(type(np.nan))
print(np.nan == np.nan)
print(np.nan is np.nan)
s = pd.Series([1, 2, None])
print(s)
print(s.dtype)The first line prints <class 'float'>. Comparing NaN with == prints False, but an identity check with is prints True because there is only one np.nan object. Never rely on is, though. NaN values produced by calculations are different objects.
The last two lines show a side effect. The list [1, 2, None] contains integers, yet the Series has dtype float64. Pandas has to store the gap somehow, and the classic integer dtype int64 has no spare bit pattern for "missing". NaN exists only in floats, so the whole column is converted to float.
That conversion has a cost. A float64 holds integers exactly only up to 2**53. An ID column with large integers and a few gaps can lose precision when it silently becomes float.
None: the Python marker
None stays None only when the column has dtype object, which stores arbitrary Python objects.
import numpy as np
import pandas as pd
s = pd.Series(["a", None, np.nan], dtype=object)
print(s.tolist())
print(s.isna().tolist())
flags = pd.Series([True, False, None])
print(flags.dtype)
print(flags.tolist())The first list prints ['a', None, nan]. One object column holds two different markers, and isna treats both as missing, so the second line prints [False, True, True].
The boolean example shows a second dtype surprise. A bool column cannot hold a gap, and a float column would turn True into 1.0. Pandas falls back to object, so the column is no longer a fast, compact boolean column.
In a numeric context, pandas converts None to NaN. In pd.Series([1, 2, None]) you get NaN, not None. If you need to know which marker a cell holds, check the dtype first.
NaT: missing time values
Datetime columns have their own marker, pd.NaT. It means "Not a Time", and it works for datetime64, timedelta64 and period dtypes.
import pandas as pd
dates = pd.Series(pd.to_datetime(["2026-01-01", None, "2026-01-03"]))
print(dates)
print(dates.dtype)
print(dates.isna().tolist())
print(pd.NaT == pd.NaT)The None in the input becomes NaT, and the column stays a datetime column. This is better than the integer case, because the datetime dtype keeps its type. The exact dtype name, such as datetime64[ns] or datetime64[us], depends on your pandas version. NaT behaves like NaN in comparisons, so pd.NaT == pd.NaT prints False.
pd.NA: the nullable marker
Older pandas had a problem. Integers, booleans and strings had no way to hold a gap without changing type. Pandas added nullable dtypes to fix this. They store a separate mask that records which cells are missing, so the values keep their real type. Their marker is pd.NA.
Nullable dtypes are named with a capital letter or a lowercase word, which tells them apart from the NumPy dtypes: Int64, Float64, boolean and string.
import pandas as pd
ints = pd.Series([1, None, 3], dtype="Int64")
bools = pd.Series([True, None, False], dtype="boolean")
text = pd.Series(["a", None, "c"], dtype="string")
print(ints.tolist())
print(bools.tolist())
print(text.tolist())
print(ints.dtype, bools.dtype, text.dtype)Each list shows <NA> where the gap is, and the integers are still integers. A later chapter covers these dtypes in more detail. For now, what matters is that pd.NA is a separate marker with its own rules.
Recent pandas versions change which string dtype you get by default. In pandas 3.0, plain string columns use a dedicated str dtype that stores gaps as NaN, not pd.NA. Request dtype="string" explicitly if you want pd.NA.
How pandas picks a marker
You rarely choose a marker directly. It follows from the dtype of the column, as the diagram shows.
The last branch of the diagram is the one that causes the most trouble. A plain int64 or bool column cannot hold a gap, so the data is converted to float64 or object when a gap appears, and the original type is lost.
One check that works for every marker
The markers behave differently, so you need one test that covers all of them. pd.isna is that test. It accepts a scalar, a Series or a DataFrame, and its inverse is pd.notna.
import numpy as np
import pandas as pd
for value in [np.nan, None, pd.NaT, pd.NA, 0, ""]:
print(repr(value), pd.isna(value))The first four values print True. The last two print False. A zero and an empty string are real values, not missing ones. Treating them as gaps is a data-cleaning decision, not something pandas does for you. A later chapter covers turning placeholder values such as -999 into missing values.
The method forms Series.isna() and DataFrame.isna() do the same job element by element. The next chapter uses them to count gaps per column.
Why == and is cannot find missing values
Comparison operators are the most common source of mistakes. Each marker is built so that comparing with it never says "yes".
import numpy as np
import pandas as pd
s = pd.Series([1.0, np.nan, 3.0])
print((s == np.nan).tolist())
print(s.isna().tolist())
print(pd.NA == 1)
print(pd.NA + 1)
print(True | pd.NA)
print(False & pd.NA)
try:
if pd.NA:
print("never reached")
except TypeError as error:
print("TypeError:", error)The comparison s == np.nan returns False for every row, including the missing one, so a filter such as df[df["x"] == np.nan] always returns an empty result. Use isna instead.
pd.NA follows three-valued logic, the same idea as NULL in SQL:
- A comparison or calculation with
pd.NAgivespd.NA, because the answer is unknown.pd.NA == 1andpd.NA + 1both print<NA>. True | pd.NAisTrue, because the result is true whatever the unknown value is.False & pd.NAisFalsefor the same reason.- Using
pd.NAin anifraisesTypeError, because "unknown" is neither true nor false. NaN does not do this.if np.nan:runs the branch, because a non-zero float is truthy.
The truthiness difference matters when you move code between float columns and nullable columns. A condition that worked silently with NaN can raise an error with pd.NA.
Edge cases to remember
- Mixed markers in one object column. A column can hold
None,np.nanandpd.NAat once.isnafinds all of them, but==andischecks do not. - Integer columns become float. A column of integers with a gap read from a file or built from a list becomes
float64unless you ask forInt64. - Boolean columns become object. A
boolcolumn with a gap becomesobject, which is slower and holds Python objects. - Empty strings are not missing.
"",0andFalseare values. Only the markers above count as missing. - Version differences. Default string handling changed in pandas 3.0, so check
df.dtypesrather than assuming.
Summary
Pandas has no single missing value. NaN is the float marker, None is the object marker, NaT is for time values, and pd.NA is for nullable dtypes. The column's dtype decides which one you get. Plain integer and boolean columns cannot hold gaps, so they change type when one appears. Always use isna and notna to find gaps, and check dtypes when a column's type changes unexpectedly.
Exercise
You build pd.Series([True, False, None]). What dtype does it get, and how would you build it so that it stays a true boolean column with a proper missing value?
Show answer
The Series gets dtype object, because a plain bool column cannot hold a gap and the values are stored as Python objects. Pass dtype="boolean" to get the nullable boolean dtype, where the gap is stored as pd.NA and the column stays boolean: pd.Series([True, False, None], dtype="boolean").
Exercise
Given s = pd.Series([1.0, np.nan, 3.0]), why does s[s == np.nan] return an empty result, and what should you write instead?
Show answer
NaN is never equal to anything, including itself, so s == np.nan is False for every row and the filter selects nothing. Write s[s.isna()] to select the missing rows, or s[s.notna()] to select the rows that have values.