When adding two Series objects with a common index, but one of them has a missing value, then what will be the default value in front of missing index?
When adding two Series objects with a common index, but one of them has a missing value, then what will be the default value in front of missing index?
Solution
Pandas aligns two Series on their index labels before adding. If a label is present in one Series but missing (or holds NaN) in the other, the result at that label is NaN, because any arithmetic with NaN gives NaN. No error is raised.
For example, pd.Series([1, 2], index=['a', 'b']) + pd.Series([10], index=['a']) gives 11 for a and NaN for b.
Option 4: the other Series' value is used only if you call s1.add(s2, fill_value=0), which treats the missing value as 0. The + operator does not do this.
Option 3: pandas does not raise an error for mismatched or missing indexes during arithmetic.
Related questions:
2025: 3 June Shift 2
2026: 23 May Shift 2