3.5. Series Getitem

3.5.1. Rationale

3.5.2. SetUp

>>> import pandas as pd

3.5.3. 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)
>>> s[0]
1.1
>>> s[1]
2.2
>>> s[2]
3.3
>>> s[3]
nan
>>> s[4]
5.5
>>> s[5]
Traceback (most recent call last):
KeyError: 5
>>> s[-1]
Traceback (most recent call last):
KeyError: -1
>>> s[-100]
Traceback (most recent call last):
KeyError: -100

3.5.4. 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
Float64Index([1.0, 0.0, 3.3, 99.0, -1.0], dtype='float64')
>>> s[0]
2.2
>>>
>>> s[1]
1.1
>>>
>>> s[2]
Traceback (most recent call last):
KeyError: 2
>>>
>>> s[3]
Traceback (most recent call last):
KeyError: 3
>>>
>>> s[3.3]
3.3
>>>
>>> s[-1]
5.5

3.5.5. 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')
>>> s['a']
1.1
>>>
>>> s['b']
2.2
>>>
>>> s['c']
3.3
>>>
>>> s['d']
nan
>>>
>>> s['e']
5.5
>>>
>>> s['f']
Traceback (most recent call last):
KeyError: 'f'
>>> s[0]
1.1
>>>
>>> s[3]
nan
>>>
>>> s[100]
Traceback (most recent call last):
IndexError: index 100 is out of bounds for axis 0 with size 5
>>> s[-1]
5.5
>>>
>>> s[-100]
Traceback (most recent call last):
IndexError: index -100 is out of bounds for axis 0 with size 5

3.5.6. 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')
>>> s['2000-01-03']
5.5
>>> s['2000-01']
2000-01-01    3.3
2000-01-02    NaN
2000-01-03    5.5
Freq: D, dtype: float64
>>> s['1999']
1999-12-30    1.1
1999-12-31    2.2
Freq: D, dtype: float64
>>> s[0]
1.1
>>>
>>> s[1]
2.2
>>>
>>> s[2]
3.3
>>>
>>> s[3]
nan
>>>
>>> s[4]
5.5
>>>
>>> s[5]
Traceback (most recent call last):
IndexError: index 5 is out of bounds for axis 0 with size 5
>>>
>>> s[-1]
5.5
>>> s['1999']
1999-12-30    1.1
1999-12-31    2.2
Freq: D, dtype: float64
>>>
>>> s[1999]
Traceback (most recent call last):
IndexError: index 1999 is out of bounds for axis 0 with size 5

3.5.7. Assignments

Code 3.58. Solution
"""
* Assignment: Series Getitem
* Complexity: easy
* Lines of code: 5 lines
* Time: 8 min

English:
    1. Set random seed to zero
    2. Create `pd.Series` with 100 random numbers from standard normal distribution
    3. Series Index are following dates since 2000
    4. Define `result: dict` with values:
        a. at 2000-02-29,
        b. first value in the series (without using `.head()`),
        c. last value in the series (without using `.tail()`),
        d. middle value in the series.
    5. Run doctests - all must succeed

Polish:
    1. Ustaw ziarno losowości na zero
    2. Stwórz `pd.Series` z 100 losowymi liczbami z rozkładu normalnego
    3. Indeksem w serii mają być kolejne dni od 2000 roku
    4. Zdefiniuj `result: dict` ` wartościami:
        a. dnia 2000-02-29,
        b. pierwszą wartość w serii (nie używając `.head()`),
        c. ostatnią wartość w serii (nie używając `.tail()`),
        d. środkowa wartość serii.
    5. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `np.random.seed(0)`
    * `np.random.randn(10)`

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert all(type(x) is not Ellipsis for x in result.values()), \
    'Assign result to dict values in `result`'
    >>> assert type(result) is dict, \
    'Variable `result` has invalid type, should be `dict`'

    >>> result  # doctest: +NORMALIZE_WHITESPACE
    {'2000-02-29': -0.3627411659871381,
     'first': 1.764052345967664,
     'last': 0.40198936344470165,
     'middle': -0.8954665611936756}
"""

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


result = {
    '2000-02-29': ...,
    'first': ...,
    'last': ...,
    'middle': ...,
}