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 = ...