4. Selection

Data selection permits returning views of data contained within a container.

The two-dimensional Frame exposes three primary means of data selection: a root __getitem__ interface, as well as __getitem__ interfaces on loc and iloc attributes. While the one-dimensional Series provides the same interface, the root and loc __getitem__ are identical./

As much as possible, slices or views of underlying data will be returned from selection operations. As underlying data is immutable, there is no risk of undesirable side-effects from returning views of underlying data.

4.1. Series

Series[key]
Series.loc[key]
Series.iloc[key]

Return the values specified by key.

Parameters

key – A selector, either a label, a list of labels, a slice of labels, or a Boolean array. The root __getitem__ takes loc labels, loc takes loc labels, and iloc takes integer indices.

Returns

static_frame.Series

>>> s = sf.Series((1, 2, 67, 62, 27, 14), index=('Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'), dtype=np.int64)
>>> s
<Series>
<Index>
Earth    1
Mars     2
Jupiter  67
Saturn   62
Uranus   27
Neptune  14
<<U7>    <int64>

>>> s['Mars']
2
>>> s['Mars':]
<Series>
<Index>
Mars     2
Jupiter  67
Saturn   62
Uranus   27
Neptune  14
<<U7>    <int64>
>>> s[['Mars', 'Saturn']]
<Series>
<Index>
Mars     2
Saturn   62
<<U7>    <int64>
>>> s[s > 60]
<Series>
<Index>
Jupiter  67
Saturn   62
<<U7>    <int64>

>>> s.iloc[-2:]
<Series>
<Index>
Uranus   27
Neptune  14
<<U7>    <int64>

4.2. Frame

Frame[key]
Frame.loc[key]
Frame.iloc[key]

Return the values specified by key.

Parameters

key – A selector, either a label, a list of labels, a slice of labels, or a Boolean array. The root __getitem__ takes loc labels, loc takes loc labels, and iloc takes integer indices. The root __getitem__ interface is a column selector; loc and iloc interfaces accept one or two arguments, for either row selection or row and column selection (respectively).

Returns

static_frame.Frame

>>> index = ('Mercury', 'Venus', 'Earth', 'Mars')
>>> columns = ('diameter', 'gravity', 'temperature')
>>> records = ((4879, 3.7, 167), (12104, 8.9, 464), (12756, 9.8, 15), (6792, 3.7, -65))
>>> f = sf.Frame.from_records(records, index=index, columns=columns, dtypes=dict(diameter=np.int64, temperature=np.int64))
>>> f
<Frame>
<Index> diameter gravity   temperature <<U11>
<Index>
Mercury 4879     3.7       167
Venus   12104    8.9       464
Earth   12756    9.8       15
Mars    6792     3.7       -65
<<U7>   <int64>  <float64> <int64>

>>> f['gravity']
<Series: gravity>
<Index>
Mercury           3.7
Venus             8.9
Earth             9.8
Mars              3.7
<<U7>             <float64>
>>> f['gravity':]
<Frame>
<Index> gravity   temperature <<U11>
<Index>
Mercury 3.7       167
Venus   8.9       464
Earth   9.8       15
Mars    3.7       -65
<<U7>   <float64> <int64>
>>> f[['diameter', 'temperature']]
<Frame>
<Index> diameter temperature <<U11>
<Index>
Mercury 4879     167
Venus   12104    464
Earth   12756    15
Mars    6792     -65
<<U7>   <int64>  <int64>

>>> f.loc['Earth', 'temperature']
15
>>> f.loc['Earth':, 'temperature']
<Series: temperature>
<Index>
Earth                 15
Mars                  -65
<<U7>                 <int64>
>>> f.loc[f['temperature'] > 100, 'diameter']
<Series: diameter>
<Index>
Mercury            4879
Venus              12104
<<U7>              <int64>
>>> f.loc[sf.ILoc[-1], ['gravity', 'temperature']]
<Series: Mars>
<Index>
gravity        3.7
temperature    -65.0
<<U11>         <float64>

>>> f.iloc[-2:, -1]
<Series: temperature>
<Index>
Earth                 15
Mars                  -65
<<U7>                 <int64>

5. Selection Modifiers

StaticFrame permits using selection modifiers in loc selectors. These modifiers permit encapsulating, per axis, a different kind of selection.

ILoc[key]

A wrapper for embedding iloc specificiations within a single axis argument of a loc selection.

HLoc[key]

A wrapper for embedding hierarchical specificiations for static_frame.IndexHierarchy within a single axis argument of a loc selection.