Sub topic 9
Index alignment: why results contain NaN
This chapter assumes you can create a Series, know that every Series has an Index, and have used basic arithmetic on columns. Here you will learn why adding two Series can produce NaN (pandas' marker for a missing value) and how to control that.
Arithmetic matches labels, not positions
When you combine two Series with an operator such as +, pandas does not pair the first element with the first element. It builds the union of the two indexes and matches values that share a label. If a label exists on only one side, the other side has no value, and the result is NaN.
import pandas as pd
a = pd.Series([1, 2, 3], index=["x", "y", "z"])
b = pd.Series([10, 20, 30], index=["y", "z", "w"])
print(a + b) # x: NaN, y: 12.0, z: 23.0, w: NaN
print(a.add(b, fill_value=0)) # x: 1.0, y: 12.0, z: 23.0, w: 30.0Only y and z appear in both Series, so only those two labels get real sums. The labels x and w each appear once, so a + b gives NaN for them. The result also becomes float64, because NaN is a float and integers cannot hold it.
Controlling the result with fill_value
The method form of each operator (add, sub, mul, div) accepts fill_value. It replaces a missing side with the value you choose before the operation runs. With fill_value=0, x becomes 1 + 0 and w becomes 0 + 30.
Choose the fill value to suit the operation. Zero is neutral for addition, but for multiplication it would wipe out the value. Use 1 there.
A label that is missing on both sides stays NaN. fill_value only helps when at least one side has a value.
Alignment in DataFrames
Alignment applies to both axes. Adding two DataFrames matches row labels and column labels, so a mismatch in either produces NaN in the affected cells.
It also happens silently when you assign a Series to a column. The Series is aligned to the DataFrame's index, so rows whose labels are not in the Series get NaN. This often surprises people after filtering or sorting, when the two objects no longer share the same index.
Common errors
- Unexpected NaN after a calculation. Compare
a.indexandb.index. Typos, trailing spaces, and different types (the string"1"and the integer1) all create labels that do not match. - Duplicate labels. If an index has repeated labels, alignment can fail with an error or produce more rows than you expect. Check
index.is_uniquefirst. - You wanted positional math. Use
a.to_numpy() + b.to_numpy()for equal-length data, or reset the indexes withreset_index(drop=True). Do this only when you are sure the order is correct.
Exercise
Given s1 = pd.Series([5, 6], index=["a", "b"]) and s2 = pd.Series([1, 2], index=["b", "c"]), what does s1.sub(s2, fill_value=0) return?
Show answer
Label a is 5 - 0 = 5.0, label b is 6 - 1 = 5.0, and label c is 0 - 2 = -2.0. All values are floats, and no NaN remains because each label exists in at least one Series.
Exercise
Why does a + b give float64 results even though both Series hold integers?
Show answer
Unmatched labels produce NaN, and NaN is a float value. pandas converts the result to float64 so it can store the missing entries.