6. Dictionary-Like Interface

Series and Frame provide dictionary-like interfaces.

For more flexible iteration of keys and values, see Iterators, below.

6.1. Series

Series.keys() → static_frame.core.index.Index[source]

Iterator of index labels.

Series.__iter__()[source]

Iterator of index labels, same as Series.keys().

Series.__contains__(value) → bool[source]

Inclusion of value in index labels.

Series.values

1D NumPy array of values

Series.items() → Iterator[Tuple[Any, Any]][source]

Iterator of pairs of index label and value.

Series.__len__() → int[source]

Length of values.

Series.get(key: Hashable, default=None) → Any[source]

Return the value found at the index key, else the default if the key is not found.

6.1.1. Examples

>>> 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>
>>> len(s)
6
>>> [k for k, v in s.items() if v > 60]
['Jupiter', 'Saturn']
>>> [s.get(k, None) for k in ('Mercury', 'Neptune', 'Pluto')]
[None, 14, None]
>>> 'Pluto' in s
False

6.2. Frame

Frame.keys()[source]

Iterator of column labels.

Frame.__iter__()[source]

Iterator of column labels, same as Frame.keys().

Frame.__contains__(value) → bool[source]

Inclusion of value in column labels.

Frame.values

2D NumPy array of Frame values

Frame.items() → Iterator[Tuple[Any, static_frame.core.series.Series]][source]

Iterator of pairs of column label and corresponding column Series.

Frame.__len__() → int[source]

Length of rows in values.

Frame.get(key, default=None)[source]

Return the value found at the columns key, else the default if the key is not found. This method is implemented to complete the dictionary-like interface.

6.2.1. Examples

>>> f = sf.Frame.from_dict(dict(diameter=(12756, 142984, 120536), temperature=(15, -110, -140)), index=('Earth', 'Jupiter', 'Saturn'), dtypes=dict(temperature=np.int64, diameter=np.int64))
>>> f
<Frame>
<Index> diameter temperature <<U11>
<Index>
Earth   12756    15
Jupiter 142984   -110
Saturn  120536   -140
<<U7>   <int64>  <int64>
>>> len(f)
3
>>> [k for k, v in f.items() if (v < 0).any()]
['temperature']
>>> f.get('diameter')
<Series: diameter>
<Index>
Earth              12756
Jupiter            142984
Saturn             120536
<<U7>              <int64>
>>> f.get('mass', np.nan)
nan
>>> 'temperature' in f
True
>>> f.values.tolist()
[[12756, 15], [142984, -110], [120536, -140]]

Deviations from Pandas

For consistency, the iterator returned by Series.keys() and Frame.keys() is the same as the iterator returned by iterating the object itself. This deviates from Pandas, where iterating a Series iterates pd.Series.values while iterating a DataFrame iterates pd.DataFrame.keys().