4.7. Series Getitem

4.7.1. SetUp

>>> import pandas as pd

4.7.2. Range Index

>>> s = pd.Series([1.1, 2.2, 3.3, None, 5.5])
>>> s
0    1.1
1    2.2
2    3.3
3    NaN
4    5.5
dtype: float64
>>> s.index
RangeIndex(start=0, stop=5, step=1)

Valid:

>>> s.loc[0]
np.float64(1.1)
>>>
>>> s.loc[1]
np.float64(2.2)
>>>
>>> s.loc[2]
np.float64(3.3)
>>>
>>> s.loc[3]
np.float64(nan)
>>>
>>> s.loc[4]
np.float64(5.5)

Out of range:

>>> s.loc[5]
Traceback (most recent call last):
KeyError: 5

Invalid:

>>> s.loc[-1]
Traceback (most recent call last):
KeyError: -1
>>>
>>> s.loc[-100]
Traceback (most recent call last):
KeyError: -100

4.7.3. Float and Int Index

>>> s = pd.Series(
...     data = [1.1, 2.2, 3.3, None, 5.5],
...     index = [1, 0, 3.3, 99, -1])
>>>
>>> s
 1.0     1.1
 0.0     2.2
 3.3     3.3
 99.0    NaN
-1.0     5.5
dtype: float64
>>> s.index
Index([1.0, 0.0, 3.3, 99.0, -1.0], dtype='float64')

Valid:

>>> s.loc[0]
np.float64(2.2)
>>>
>>> s.loc[1]
np.float64(1.1)

Out of range:

>>> s.loc[2]
Traceback (most recent call last):
KeyError: 2

Invalid:

>>> s.loc[3]
Traceback (most recent call last):
KeyError: 3
>>>
>>> s.loc[3.3]
np.float64(3.3)
>>>
>>> s.loc[-1]
np.float64(5.5)

4.7.4. String Index

>>> s = pd.Series(
...     data = [1.1, 2.2, 3.3, None, 5.5],
...     index = ['a', 'b', 'c', 'd', 'e'])
>>>
>>> s
a    1.1
b    2.2
c    3.3
d    NaN
e    5.5
dtype: float64
>>> s.index
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

Valid:

>>> s.loc['a']
np.float64(1.1)
>>>
>>> s.loc['b']
np.float64(2.2)
>>>
>>> s.loc['c']
np.float64(3.3)
>>>
>>> s.loc['d']
np.float64(nan)
>>>
>>> s.loc['e']
np.float64(5.5)
>>>

Out of range:

>>> s.loc['f']
Traceback (most recent call last):
KeyError: 'f'

4.7.5. Date Index

>>> s = pd.Series(
...     data = [1.1, 2.2, 3.3, None, 5.5],
...     index = pd.date_range('1999-12-30', periods=5))
>>>
>>> s
1999-12-30    1.1
1999-12-31    2.2
2000-01-01    3.3
2000-01-02    NaN
2000-01-03    5.5
Freq: D, dtype: float64
>>> s.index
DatetimeIndex(['1999-12-30', '1999-12-31', '2000-01-01', '2000-01-02',
               '2000-01-03'],
              dtype='datetime64[ns]', freq='D')

String:

>>> s.loc['2000-01-03']
np.float64(5.5)
>>> s.loc['2000-01']
2000-01-01    3.3
2000-01-02    NaN
2000-01-03    5.5
Freq: D, dtype: float64
>>> s.loc['1999']
1999-12-30    1.1
1999-12-31    2.2
Freq: D, dtype: float64

Str vs Int:

>>> s.loc['1999']
1999-12-30    1.1
1999-12-31    2.2
Freq: D, dtype: float64
>>>
>>> s.loc[1999]
Traceback (most recent call last):
KeyError: 1999

4.7.6. Assignments

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

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

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

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

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

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

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is numpy.float64, \
'Variable `result` has invalid type, should be `numpy.float64`'

>>> result
np.float64(1.8675579901499675)
"""

import pandas as pd
import numpy as np
np.random.seed(0)

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

# Define variable `result` with value at date `2000-01-05` in `DATA`
# type: np.float64
result = ...


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

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

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

# %% English
# 1. Define variable `result_b` with first value in `DATA`
# 2. Use `.iloc[]` method
# 3. Run doctests - all must succeed

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

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

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

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is numpy.float64, \
'Variable `result` has invalid type, should be `numpy.float64`'

>>> result
np.float64(1.764052345967664)
"""

import pandas as pd
import numpy as np
np.random.seed(0)

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

# Define variable `result` with first value in `DATA`
# type: np.float64
result = ...


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

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

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

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

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

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

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is numpy.float64, \
'Variable `result` has invalid type, should be `numpy.float64`'

>>> result
np.float64(0.41059850193837233)
"""

import pandas as pd
import numpy as np
np.random.seed(0)

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

# Define variable `result` with last value in `DATA`
# type: np.float64
result = ...


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

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

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

# %% English
# 1. Define variable `result_d` with middle value in `DATA`
# 2. Use `.iloc[]` method and `.size` attribute
# 3. Run doctests - all must succeed

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

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

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

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is numpy.float64, \
'Variable `result` has invalid type, should be `numpy.float64`'

>>> result
np.float64(-0.977277879876411)
"""

import pandas as pd
import numpy as np
np.random.seed(0)

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

# Define variable `result` with middle value in `DATA`
# type: np.float64
result = ...