5.7. Series ILoc

5.7.1. SetUp

>>> import pandas as pd
>>>
>>>
>>> data = ['Alice', 'Bob', 'Carol', 'Dave', 'Eve', 'Mallory']
>>> index = ['a', 'b', 'c', 'd', 'e', 'm']
>>>
>>> s = pd.Series(data, index)
>>>
>>> s
a      Alice
b        Bob
c      Carol
d       Dave
e        Eve
m    Mallory
dtype: str

5.7.2. Positive Index

>>> s.iloc[0]
'Alice'
>>>
>>> s.iloc[1]
'Bob'
>>>
>>> s.iloc[2]
'Carol'
>>>
>>> s.iloc[3]
'Dave'

5.7.3. Negative Index

>>> s.iloc[-1]
'Mallory'
>>>
>>> s.iloc[-2]
'Eve'
>>>
>>> s.iloc[-3]
'Dave'

5.7.4. Out of Bounds

>>> s.iloc[10]
Traceback (most recent call last):
IndexError: single positional indexer is out-of-bounds
>>>
>>> s.iloc[-10]
Traceback (most recent call last):
IndexError: single positional indexer is out-of-bounds

5.7.5. 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 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
>>> import numpy

>>> 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 numpy.float64, \
'Variable `result` has an invalid type; expected: `numpy.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[]`
# - `Series.size`
# - `a // b`

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

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

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