3.5. Series Getitem

3.5.1. Range Index

import pandas as pd


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[3]        # nan
s[100]      # KeyError: 100

s[-1]       # KeyError: -1
s[-100]     # KeyError: -100

3.5.2. Float and Int Index

import pandas as pd


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[3]        # KeyError: 3
s[100]      # KeyError: 100

s[-1]       # 5.5
s[-100]     # KeyError: -100

3.5.3. String Index

import pandas as pd


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['d']      # nan
s['e']      # 5.5
s['x']      # KeyError: 'x'

s[0]        # 1.1
s[3]        # nan
s[100]      # IndexError: index 100 is out of bounds for axis 0 with size 5
s[-1]       # 5.5
s[-100]     # IndexError: index -100 is out of bounds for axis 0 with size 5

3.5.4. Date Index

import pandas as pd


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[3]        # nan
s[100]      # IndexError: index 100 is out of bounds for axis 0 with size 5
s[-1]       # 5.5
s[-100]     # IndexError: index -100 is out of bounds for axis 0 with size 5

s['a']      # KeyError: 'a'

3.5.5. Assignments

Code 3.37. 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

    >>> type(result) is dict
    True
    >>> 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': ...,
}