5.21. Series Recap

5.21.1. Assignments

# %% About
# - Name: Pandas Series Getitem
# - Difficulty: easy
# - Lines: 5
# - Minutes: 8

# %% 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 middle value in `DATA`
# 2. Use `.iloc[]` method and `.size` attribute
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z środkową wartością w `DATA`
# 2. Użyj metody `.iloc[]` oraz atrybut `.size`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# np.float64(-0.977277879876411)

# %% Hints
# - `Series.iloc[]`
# - `Series.size`
# - `a // b`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> 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 np.float64, \
'Variable `result` has an invalid type; expected: `np.float64`.'

>>> 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
np.float64(-0.977277879876411)
"""

# %% 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
import numpy as np

# %% Types
result: np.float64

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10),
)

# %% Result
result = ...

# %% About
# - Name: Series Create Even
# - Difficulty: easy
# - Lines: 2
# - 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. Create `result: pd.Series` with 10 even numbers
# 2. Run doctests - all must succeed

# %% Polish
# 1. Stwórz `result: pd.Series` z 10 liczbami parzystymi
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 0     0
# 1     2
# 2     4
# 3     6
# 4     8
# 5    10
# 6    12
# 7    14
# 8    16
# 9    18
# dtype: int64

# %% 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     0
1     2
2     4
3     6
4     8
5    10
6    12
7    14
8    16
9    18
dtype: int64
"""

# %% 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
import numpy as np

# %% Types
result: pd.Series

# %% Data

# %% Result
result = ...

# FIXME: na windows testy nie przechodzą z powodu np.int32 a nie np.int64

# %% About
# - Name: Series Create Randint
# - Difficulty: easy
# - Lines: 2
# - 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. Set random seed to zero
# 2. Create `result: pd.Series` with 10 random digits (`int` from `0` to `9`)
# 3. Run doctests - all must succeed

# %% Polish
# 1. Ustaw ziarno losowości na zero
# 2. Stwórz `result: pd.Series` z 10 losowymi cyframi  (`int` from `0` to `9`)
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 0    5
# 1    0
# 2    3
# 3    3
# 4    7
# 5    9
# 6    3
# 7    5
# 8    2
# 9    4
# dtype: int64

# %% 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    5
1    0
2    3
3    3
4    7
5    9
6    3
7    5
8    2
9    4
dtype: int64
"""

# %% 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
import numpy as np

# %% Types
result: pd.Series

# %% Data
np.random.seed(0)

# %% Result
result = ...

# %% About
# - Name: Series Create Dates
# - Difficulty: easy
# - Lines: 2
# - 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. Gagarin flown to space on 1961-04-12
# 2. Armstrong set foot on the Moon on 1969-07-21
# 3. Create `result: pd.Series` with days between Gagarin's launch and Armstrong's first step
# 4. How many days passed?
# 5. Run doctests - all must succeed

# %% Polish
# 1. Gagarin poleciał w kosmos w 1961-04-12
# 2. Armstrong postawił stopę na Księżycu w 1969-07-21
# 3. Stwórz `result: pd.Series` z dniami pomiędzy startem Gagarina a pierwszym krokiem Armstronga
# 4. Jak wiele dni upłynęło?
# 5. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result  # doctest: +NORMALIZE_WHITESPACE
# 0      1961-04-12
# 1      1961-04-13
# 2      1961-04-14
# 3      1961-04-15
# 4      1961-04-16
#           ...
# 3018   1969-07-17
# 3019   1969-07-18
# 3020   1969-07-19
# 3021   1969-07-20
# 3022   1969-07-21
# Length: 3023, dtype: datetime64[us]

# %% 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  # doctest: +NORMALIZE_WHITESPACE
0      1961-04-12
1      1961-04-13
2      1961-04-14
3      1961-04-15
4      1961-04-16
          ...
3018   1969-07-17
3019   1969-07-18
3020   1969-07-19
3021   1969-07-20
3022   1969-07-21
Length: 3023, dtype: datetime64[us]
"""

# %% 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

# %% Result
result = ...

# %% About
# - Name: Pandas Series Getitem
# - Difficulty: easy
# - Lines: 5
# - Minutes: 8

# %% 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 first value in `DATA`
# 2. Use `.iloc[]` method
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z pierwszą wartością w `DATA`
# 2. Użyj metody `.iloc[]`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# np.float64(1.764052345967664)

# %% Hints
# - `Series.iloc[]`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> 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 np.float64, \
'Variable `result` has an invalid type; expected: `np.float64`.'

>>> 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
np.float64(1.764052345967664)
"""

# %% 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
import numpy as np

# %% Types
result: np.float64

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10),
)

# %% Result
result = ...

# %% About
# - Name: Pandas Series Getitem
# - Difficulty: easy
# - Lines: 5
# - Minutes: 8

# %% 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 last value in `DATA`
# 2. Use `.iloc[]` method
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z ostatnią wartością w `DATA`
# 2. Użyj metody `.iloc[]`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# np.float64(0.41059850193837233)

# %% Hints
# - `Series.iloc[]`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> 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 np.float64, \
'Variable `result` has an invalid type; expected: `np.float64`.'

>>> 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
np.float64(0.41059850193837233)
"""

# %% 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
import numpy as np

# %% Types
result: np.float64

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10),
)

# %% Result
result = ...

# %% About
# - Name: Pandas Series Getitem
# - Difficulty: easy
# - Lines: 5
# - Minutes: 8

# %% 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 value at date `2000-01-05` in `DATA`
# 2. Use `.loc[]` method
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z wartościami dla daty `2000-01-05` w `DATA`
# 2. Użyj metody `.loc[]`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# np.float64(1.8675579901499675)

# %% Hints
# - `Series.loc[]`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> 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 np.float64, \
'Variable `result` has an invalid type; expected: `np.float64`.'

>>> 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
np.float64(1.8675579901499675)
"""

# %% 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
import numpy as np

# %% Types
result: np.float64

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10),
)

# %% Result
result = ...