What will be the result when two Pandas Series being combined have mismatched indices?
What will be the result when two Pandas Series being combined have mismatched indices?
Solution
When two Series are combined (for example with +), Pandas aligns them on their index labels. A label present in only one Series has no partner to operate with, so the result for that label is NaN. The result contains the union of both indices.
For example, pd.Series([1, 2], index=['a', 'b']) + pd.Series([10, 20], index=['b', 'c']) gives:
| Index | Result |
|---|---|
| a | NaN |
| b | 12.0 |
| c | NaN |
Use s1.add(s2, fill_value=0) to treat a missing label as 0 instead of getting NaN.
Options 1 and 4: the mismatched labels are not removed. They stay in the result with NaN values, because Pandas takes the union of the indices, not the intersection.
Option 3: no error is raised. Alignment happens automatically.
Related questions:
2026: 29 May Shift 2
2025: 16 May Shift 1
2025: 22 May Shift 1