5.9. Series Getitem

5.9.1. SetUp

>>> import pandas as pd

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

5.9.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)

5.9.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='str')

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'

5.9.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[us]', 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

5.9.6. Loc

5.9.7. ILoc

5.9.8. Loc vs ILoc

  • Object Index

  • Range Index

  • Int64 Index (without collision)

  • Int64 Index (with collision)

Object Index:

| iloc | loc  | name    |
|------|------|---------|
|   0  |    a | Alice   |
|   1  |    b | Bob     |
|   2  |    c | Carol   |
|   3  |    d | Dave    |
|   4  |    e | Eve     |
|   5  |    f | Mallory |

Range Index:

| iloc | loc  | name    |
|------|------|---------|
|   0  |    0 | Alice   |
|   1  |    1 | Bob     |
|   2  |    2 | Carol   |
|   3  |    3 | Dave    |
|   4  |    4 | Eve     |
|   5  |    5 | Mallory |

Int64 Index (without collision):

| iloc | loc  | name    |
|------|------|---------|
|   0  |   10 | Alice   |
|   1  |   11 | Bob     |
|   2  |   12 | Carol   |
|   3  |   13 | Dave    |
|   4  |   14 | Eve     |
|   5  |   15 | Mallory |

Int64 Index (with collision):

| iloc | loc  | name    |
|------|------|---------|
|   0  |    1 | Alice   |
|   1  |    5 | Bob     |
|   2  |    0 | Carol   |
|   3  |    3 | Dave    |
|   4  |    2 | Eve     |
|   5  |    4 | Mallory |

5.9.9. Assignments