5.17. Series Arithmetic
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.17.1. SetUp
>>> import pandas as pd
>>>
>>> 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)
>>>
>>>
>>> data = [1, 2, 3, 4, 5]
>>> s = pd.Series(data)
>>>
>>> s
0 1
1 2
2 3
3 4
4 5
dtype: int64
5.17.2. Add
Series + 100Series.add(100)
>>> s + 100
0 101
1 102
2 103
3 104
4 105
dtype: int64
>>> s.add(100)
0 101
1 102
2 103
3 104
4 105
dtype: int64
5.17.3. Subtract
Series - 100Series.sub(100)Series.subtract(100)
>>> s - 100
0 -99
1 -98
2 -97
3 -96
4 -95
dtype: int64
>>> s.sub(100)
0 -99
1 -98
2 -97
3 -96
4 -95
dtype: int64
>>> s.subtract(100)
0 -99
1 -98
2 -97
3 -96
4 -95
dtype: int64
5.17.4. Multiply
Series * 100Series.mul(100)Series.multiply(100)
>>> s * 100
0 100
1 200
2 300
3 400
4 500
dtype: int64
>>> s.mul(100)
0 100
1 200
2 300
3 400
4 500
dtype: int64
>>> s.multiply(100)
0 100
1 200
2 300
3 400
4 500
dtype: int64
5.17.5. Power
Series ** 2Series.pow(2)
>>> s ** 2
0 1
1 4
2 9
3 16
4 25
dtype: int64
>>> s.pow(2)
0 1
1 4
2 9
3 16
4 25
dtype: int64
5.17.6. Root
Series ** (1/2)
>>> s ** (1/2)
0 1.0000
1 1.4142
2 1.7321
3 2.0000
4 2.2361
dtype: float64
>>> s ** 0.5
0 1.0000
1 1.4142
2 1.7321
3 2.0000
4 2.2361
dtype: float64
5.17.7. True Division
Series / 100Series.div(100)Series.divide(100)Series.truediv(100)
>>> s / 100
0 0.01
1 0.02
2 0.03
3 0.04
4 0.05
dtype: float64
>>> s.div(100)
0 0.01
1 0.02
2 0.03
3 0.04
4 0.05
dtype: float64
>>> s.divide(100)
0 0.01
1 0.02
2 0.03
3 0.04
4 0.05
dtype: float64
>>> s.truediv(100)
0 0.01
1 0.02
2 0.03
3 0.04
4 0.05
dtype: float64
5.17.8. Floor Div
Series // 2Series.floordiv(2)
>>> s
0 1
1 2
2 3
3 4
4 5
dtype: int64
>>> s // 2
0 0
1 1
2 1
3 2
4 2
dtype: int64
>>> s.floordiv(2)
0 0
1 1
2 1
3 2
4 2
dtype: int64
5.17.9. Modulo
Series % 2Series.mod(2)
>>> s
0 1
1 2
2 3
3 4
4 5
dtype: int64
>>> s % 2
0 1
1 0
2 1
3 0
4 1
dtype: int64
>>> s.mod(2)
0 1
1 0
2 1
3 0
4 1
dtype: int64
Parity check:
>>> s % 2 == 0
0 False
1 True
2 False
3 True
4 False
dtype: bool
5.17.10. Arithmetic Expressions
((s+100) * 10) ** 2
>>> ((s+100) * 10) ** 2
0 1020100
1 1040400
2 1060900
3 1081600
4 1102500
dtype: int64
5.17.11. Missing Values
>>> data = [1, 2, None, 4, 5]
>>> s = pd.Series(data)
>>>
>>> s
0 1.0
1 2.0
2 NaN
3 4.0
4 5.0
dtype: float64
>>> s + 100
0 101.0
1 102.0
2 NaN
3 104.0
4 105.0
dtype: float64
>>> s ** 2
0 1.0
1 4.0
2 NaN
3 16.0
4 25.0
dtype: float64
>>> ((s+100) * 10) ** 2
0 1020100.0
1 1040400.0
2 NaN
3 1081600.0
4 1102500.0
dtype: float64
5.17.12. Fill Values
>>> data = [1, 2, None, 4, 5]
>>> s = pd.Series(data)
>>>
>>> s
0 1.0
1 2.0
2 NaN
3 4.0
4 5.0
dtype: float64
>>> s.add(100, fill_value=0)
0 101.0
1 102.0
2 100.0
3 104.0
4 105.0
dtype: float64
5.17.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 = ...