Adding two pandas Series while keeping (non-numeric) values at non-matching indices

StackOverflow https://stackoverflow.com/questions/23658092

  •  22-07-2023
  •  | 
  •  

Question

Let's say I have the following two pandas.Series objects:

>>> s1 = Series([1, True, 3, 5], index=['a', 'b', 'c', 'e'])
>>> s2 = Series([100, 300, 'foo', 500], index=['a', 'c', 'd', 'e'])
>>> s1
a       1
b    True
c       3
e       5
dtype: object
>>> s2
a    100
c    300
d    foo
e    500
dtype: object

I would like to calculate the sum s1+s2 where elements at non-matching indices should simply be "inherited" from s1 or s2 without change. So the result should look like this:

>>> Series([101, True, 303, 'foo', 505], index=['a', 'b', 'c', 'd', 'e'])
a     101
b    True
c     303
d     foo
e     505
dtype: object

The naive approach does not work because it introduces NaN at the non-matching indices:

>>> s1 + s2
a    101
b    NaN
c    303
d    NaN
e    505
dtype: object

There are similar questions on StackOverflow that suggest solutions along the lines of s1.add(s2, fill_value=0), but this doesn't work in my case because not all values are numeric, so fill_value is of no use. Is there an option which tells pandas to simply fill in the missing values from whichever Series they are present in?

It seems like such an obvious thing that I must surely be missing something, but I've been scouring both the documentation and StackOverflow and haven't found any answers. Many thanks for any suggestions!

Was it helpful?

Solution

One way I think would be to use combine_first:

>>> s1 = pd.Series([1, True, 3, 5], index=['a', 'b', 'c', 'e'])
>>> s2 = pd.Series([100, 300, 'foo', 500], index=['a', 'c', 'd', 'e'])
>>> (s1+s2).combine_first(s1).combine_first(s2)
a     101
b    True
c     303
d     foo
e     505
dtype: object

But to be honest, Series with varying type like this don't seem to come up for me often.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top