pandas.Series.getitem¶
Get value(s) of Series by key.
Limitations¶
- Supported
key
can be one of the following: Integer scalar, e.g.
series[0]
An array or a list, e.g.
series[0,2,5]
A list of booleans, e.g.
series[True,False]
A slice, e.g.
series[2:5]
Another series
Examples¶
import numpy as np
import pandas as pd
from numba import njit
@njit
def series_getitem_scalar():
series = pd.Series(np.arange(10, 0, -1)) # Series of 10, 9, ..., 1
return series[0] # Accessing series by scalar index
print(series_getitem_scalar())
$ python ./series/series_getitem/series_getitem_scalar_single_result.py
0 10
dtype: int64
import numpy as np
import pandas as pd
from numba import njit
@njit
def series_getitem_scalar_many_idx():
series = pd.Series([5, 4, 3, 2, 1], index=[0, 2, 0, 6, 0])
return series[0]
print(series_getitem_scalar_many_idx())
$ python ./series/series_getitem/series_getitem_scalar_multiple_result.py
0 5
0 3
0 1
dtype: int64
import numpy as np
import pandas as pd
from numba import njit
@njit
def series_getitem_slice():
series = pd.Series(np.arange(10, 0, -1)) # Series of 10, 9, ..., 1
return series[3:7] # Accessing series by slice index
print(series_getitem_slice())
$ python ./series/series_getitem/series_getitem_slice.py
3 7
4 6
5 5
6 4
dtype: int64
import numpy as np
import pandas as pd
from numba import njit
@njit
def series_getitem_array():
series = pd.Series(np.arange(10, 0, -1)) # Series of 10, 9, ..., 1
array = np.array([True, False, True, True, False] * 2)
return series[array] # Accessing series by array
print(series_getitem_array())
$ python ./series/series_getitem/series_getitem_bool_array.py
0 10
2 8
3 7
5 5
7 3
8 2
dtype: int64
import numpy as np
import pandas as pd
from numba import njit
@njit
def series_getitem_series():
series = pd.Series(np.arange(10, 0, -1)) # Series of 10, 9, ..., 1
indices = pd.Series(np.asarray([1, 6, 7, 8, 9]))
return series[indices] # Accessing series by series
print(series_getitem_series())
$ python ./series/series_getitem/series_getitem_series.py
1 9
6 4
7 3
8 2
9 1
dtype: int64
See also
- Series.setitem
Set value to Series by index
- Series.loc
Access a group of rows and columns by label(s) or a boolean array.
- Series.iloc
Purely integer-location based indexing for selection by position.
- Series.at
Access a single value for a row/column label pair.
- Series.iat
Access a single value for a row/column pair by integer position.
- DataFrame.getitem
Get data from a DataFrame by indexer.
- DataFrame.setitem
Set value to DataFrame by index
- DataFrame.loc
Access a group of rows and columns by label(s) or a boolean array.
- DataFrame.iloc
Purely integer-location based indexing for selection by position.
- DataFrame.at
Access a single value for a row/column label pair.
- DataFrame.iat
Access a single value for a row/column pair by integer position.
Todo
Fix SDC behavior and add the expected output of the > python ./series_getitem.py to the docstring