4.13. Series Arithmetic

4.13.1. SetUp

>>> import pandas as pd
>>> import numpy as np

4.13.2. Broadcasting

  • Uses inner join

  • fill_value: If data in both corresponding Series locations is missing the result will be missing

>>> a = pd.Series([1, 2, 3])
>>> b = pd.Series([4, 5, 6])
>>>
>>> a + b
0    5
1    7
2    9
dtype: int64
>>> a = pd.Series([1, 2, 3, 4])
>>> b = pd.Series([4, 5, 6])
>>>
>>> a + b
0    5.0
1    7.0
2    9.0
3    NaN
dtype: float64
>>> a = pd.Series([1, 2, 3])
>>> b = pd.Series([4, 5, 6, 7])
>>>
>>> a + b
0    5.0
1    7.0
2    9.0
3    NaN
dtype: float64
>>> a = pd.Series([1, 2, None])
>>> b = pd.Series([4, 5, 6])
>>>
>>> a + b
0    5.0
1    7.0
2    NaN
dtype: float64
>>> a = pd.Series([1, 2, None])
>>> b = pd.Series([4, 5, None])
>>>
>>> a + b
0    5.0
1    7.0
2    NaN
dtype: float64
>>> a = pd.Series(data=[1, 2, 3], index=['a', 'b', 'c'])
>>> b = pd.Series(data=[4, 5, 6], index=['a', 'b', 'x'])
>>>
>>> a + b
a    5.0
b    7.0
c    NaN
x    NaN
dtype: float64

fill_value: If data in both corresponding Series locations is missing the result will be missing:

>>> a = pd.Series(data=[1, 2, 3], index=['a', 'b', 'c'])
>>> b = pd.Series(data=[4, 5, 6], index=['a', 'b', 'x'])
>>>
>>> a.add(b, fill_value=0)
a    5.0
b    7.0
c    3.0
x    6.0
dtype: float64

4.13.3. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Series Arithmetic
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3

# %% English
# 1. Define variable `result` with result of add elements of `A` corresponding elements of `B`
# 2. Do not use `.add()` method
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z wynikiem dodaj elementy z `A` do odpowiadających im elementów z `B`
# 2. Nie używaj metody `.add()`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `a + b`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pd.Series, \
'Variable `result` has invalid type, should be `pd.Series`'

>>> result
0    11.0
1    22.0
2    33.0
3    44.0
4    55.0
dtype: float64
"""

import pandas as pd


A = pd.Series([1.0, 2.0, 3.0, 4.0, 5.0])
B = pd.Series([10.0, 20.0, 30.0, 40.0, 50.0])

# Add elements of `A` corresponding elements of `B`
# Do not use `.add()` method
# type: pd.Series
result = ...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Series Arithmetic
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3

# %% English
# 1. Define variable `result` with result of
#    add elements of `A` corresponding elements of `B`
# 2. Use `.add()` method
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z wynikiem
#    dodaj elementy z `A` do odpowiadających im elementów z `B`
# 2. Użyj metodę `.add()`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `Series.add()`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pd.Series, \
'Variable `result` has invalid type, should be `pd.Series`'

>>> result
0    11.0
1    22.0
2     NaN
3    44.0
4    55.0
dtype: float64
"""

import pandas as pd


A = pd.Series([1.0, 2.0, 3.0, 4.0, 5.0])
B = pd.Series([10.0, 20.0, None, 40.0, 50.0])

# Add elements of `A` corresponding elements of `B`
# Use `.add()` method
# type: pd.Series
result = ...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Series Arithmetic
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3

# %% English
# 1. Define variable `result` with result of
#    add elements of `A` corresponding elements of `B`
#    if value is not-a-number then treat it as zero
# 2. Use `.add()` method
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z wynikiem
#    dodaj elementy z `A` do odpowiadających im elementów z `B`
#    jeżeli wartość nie jest liczbą, to potraktuj ją jak zero
# 2. Użyj metodę `.add()`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `Series.add(fill_value)`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pd.Series, \
'Variable `result` has invalid type, should be `pd.Series`'

>>> result
0    11.0
1    22.0
2     3.0
3    44.0
4    55.0
dtype: float64
"""

import pandas as pd


A = pd.Series([1.0, 2.0, 3.0, 4.0, 5.0])
B = pd.Series([10.0, 20.0, None, 40.0, 50.0])

# Add elements of `A` corresponding elements of `B`
# If value is not-a-number then treat it as zero
# Use `.add()` method
# type: pd.Series
result = ...