4.12. Series Arithmetic

4.12.1. SetUp

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

4.12.2. Vectorized Operations

  • s + 2, s.add(2), s.__add__(2)

  • s - 2, s.sub(2), s.subtract(2), s.__sub__(2)

  • s * 2, s.mul(2), s.multiply(2), s.__mul__(2)

  • s ** 2, s.pow(2), s.__pow__(2)

  • s ** (1/2), s.pow(1/2), s.__sub__(1/2)

  • s / 2, s.div(2), s.divide(), s.__div__(2)

  • s // 2, s.truediv(2), s.__truediv__(2)

  • s % 2, s.mod(2), s.__mod__(2)

  • divmod(s, 2), s.divmod(2), s.__divmod__(2), (s//2, s%2)

>>> data = pd.Series(
...     data = [1.1, 2.2, np.nan, 4.4],
...     index = ['a', 'b', 'c', 'd'])
>>> data
a    1.1
b    2.2
c    NaN
d    4.4
dtype: float64
>>> data + 2
a    3.1
b    4.2
c    NaN
d    6.4
dtype: float64
>>> data ** 2
a     1.21
b     4.84
c      NaN
d    19.36
dtype: float64
>>> data ** (1/2)
a    1.048809
b    1.483240
c         NaN
d    2.097618
dtype: float64

4.12.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 10 to each element of `DATA`
# 2. Do not use `.add()` method
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z wynikiem
#    dodania 10 to każdego elementu z `DATA`
# 2. Nie używaj metody `.add()`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% 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    12.0
2    13.0
3    14.0
4    15.0
dtype: float64
"""

import pandas as pd


DATA = pd.Series([1.0, 2.0, 3.0, 4.0, 5.0])


# Add 10 to each element of `DATA`
# 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 10 to each element of `DATA`
# 2. Use `.add()` method
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z wynikiem
#    dodania 10 to każdego elementu z `DATA`
# 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    12.0
2     NaN
3    14.0
4    15.0
dtype: float64
"""

import pandas as pd


DATA = pd.Series([1.0, 2.0, None, 4.0, 5.0])


# Add 10 to each element of `DATA`
# 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 10 to each element of `DATA`
#    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
#    dodania 10 to każdego elementu z `DATA`
#    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    12.0
2    10.0
3    14.0
4    15.0
dtype: float64
"""

import pandas as pd


DATA = pd.Series([1.0, 2.0, None, 4.0, 5.0])


# Add 10 to each element of `DATA`
# If value is not-a-number then treat it as zero
# Use `.add()` method
# type: pd.Series
result = ...