5.16. Series Arithmetic
5.16.1. SetUp
>>> import pandas as pd
>>>
>>>
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> data
0 0.0
1 1.0
2 2.0
3 NaN
4 4.0
5 5.0
dtype: float64
5.16.2. Vectorized Operations
s + 2,s.add(2)s - 2,s.sub(2),s.subtract(2)s * 2,s.mul(2),s.multiply(2)s ** 2,s.pow(2)s ** (1/2),s.pow(1/2)s / 2,s.div(2),s.divide()s // 2,s.truediv(2)s % 2,s.mod(2)divmod(s, 2),s.divmod(2)
5.16.3. Add
Series + 100Series.add(100)
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> data + 100
0 100.0
1 101.0
2 102.0
3 NaN
4 104.0
5 105.0
dtype: float64
5.16.4. Subtract
Series - 100Series.sub(100)Series.subtract(100)
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> data - 100
0 -100.0
1 -99.0
2 -98.0
3 NaN
4 -96.0
5 -95.0
dtype: float64
5.16.5. Multiply
Series * 100Series.mul(100)Series.multiply(100)
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> data * 100
0 0.0
1 100.0
2 200.0
3 NaN
4 400.0
5 500.0
dtype: float64
5.16.6. Power
Series ** 2Series.pow(2)
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> data ** 2
0 0.0
1 1.0
2 4.0
3 NaN
4 16.0
5 25.0
dtype: float64
5.16.7. Root
Series ** (1/2)
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> data ** (1/2)
0 0.000000
1 1.000000
2 1.414214
3 NaN
4 2.000000
5 2.236068
dtype: float64
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> data ** (1/3)
0 0.000000
1 1.000000
2 1.259921
3 NaN
4 1.587401
5 1.709976
dtype: float64
5.16.8. True Division
Series / 100Series.div(100)Series.divide(100)Series.truediv(100)
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> data / 100
0 0.00
1 0.01
2 0.02
3 NaN
4 0.04
5 0.05
dtype: float64
5.16.9. Floor Div
Series // 2Series.floordiv(2)
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> data // 2
0 0.0
1 0.0
2 1.0
3 NaN
4 2.0
5 2.0
dtype: float64
5.16.10. Modulo
Series % 2Series.mod(2)
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> data % 2
0 0.0
1 1.0
2 0.0
3 NaN
4 0.0
5 1.0
dtype: float64
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> data % 2 == 0
0 True
1 False
2 True
3 False
4 True
5 False
dtype: bool
5.16.11. Complex
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> (data+100) ** 2 / 3
0 3333.333333
1 3400.333333
2 3468.000000
3 NaN
4 3605.333333
5 3675.000000
dtype: float64
5.16.12. Fill Value
>>> data = pd.Series([0, 1, 2, None, 4, 5])
>>>
>>> data.add(10, fill_value=0)
0 10.0
1 11.0
2 12.0
3 10.0
4 14.0
5 15.0
dtype: float64
5.16.13. Assignments
# %% About
# - Name: Series Arithmetic
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3
# %% 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
# %% 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ść
# %% Expected
# >>> result
# 0 11.0
# 1 12.0
# 2 13.0
# 3 14.0
# 4 15.0
# dtype: float64
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'
>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)
>>> result
0 11.0
1 12.0
2 13.0
3 14.0
4 15.0
dtype: float64
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
import pandas as pd
# %% Types
result: pd.Series
# %% Data
DATA = pd.Series([1.0, 2.0, 3.0, 4.0, 5.0])
# %% Result
result = ...
# %% About
# - Name: Series Arithmetic
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3
# %% 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
# %% 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ść
# %% Expected
# >>> result
# 0 11.0
# 1 12.0
# 2 NaN
# 3 14.0
# 4 15.0
# dtype: float64
# %% Hints
# - `Series.add(fill_value)`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'
>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)
>>> result
0 11.0
1 12.0
2 NaN
3 14.0
4 15.0
dtype: float64
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
import pandas as pd
# %% Types
result: pd.Series
# %% Data
DATA = pd.Series([1.0, 2.0, None, 4.0, 5.0])
# %% Result
result = ...
# %% About
# - Name: Series Arithmetic
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3
# %% 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
# %% 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ść
# %% Expected
# >>> result
# 0 11.0
# 1 12.0
# 2 10.0
# 3 14.0
# 4 15.0
# dtype: float64
# %% Hints
# - `Series.add(fill_value)`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'
>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)
>>> result
0 11.0
1 12.0
2 10.0
3 14.0
4 15.0
dtype: float64
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
import pandas as pd
# %% Types
result: pd.Series
# %% Data
DATA = pd.Series([1.0, 2.0, None, 4.0, 5.0])
# %% Result
result = ...