8. Index Manipulation

8.1. Series

Series.reindex(index: Union[IndexBase, Iterable[Hashable], Iterable[Sequence[Hashable]], Generator[Hashable, None, None]], fill_value=nan, own_index: bool = False) → static_frame.core.series.Series[source]

Return a new Series with labels defined by the provided index. The size and ordering of the data is determined by the newly provided index, where data will continue to be aligned under labels found in both the new and the old index. Labels found only in the new index will be filled with fill_value.

Parameters
  • index – An iterable of unique, hashable values, or another Index or IndexHierarchy, to be used as the labels of the index.

  • columns – An iterable of unique, hashable values, or another Index or IndexHierarchy, to be used as the labels of the index.

  • fill_value – A value to be used to fill space created by a new index that has values not found in the previous index.

  • own_index – Flag the passed index as ownable by this Series. Primarily used by internal clients.

>>> s = sf.Series((0, 62, 13), index=('Venus', 'Saturn', 'Neptune'))

>>> s.reindex(('Venus', 'Earth', 'Mars', 'Neptune'))
<Series>
<Index>
Venus    0.0
Earth    nan
Mars     nan
Neptune  13.0
<<U7>    <float64>

Series.relabel(index: Union[Callable[[...], Any], Mapping[Hashable, Any], Series, Type[static_frame.core.index_auto.IndexAutoFactory], IndexBase, Iterable[Hashable], Iterable[Sequence[Hashable]], Generator[Hashable, None, None]]) → static_frame.core.series.Series[source]

Return a new Series with transformed labels on the index. The size and ordering of the data is never chagned in a relabeling operation. The resulting index must be unique.

Parameters

index – One of the following types, used to create a new Index with the same size as the previous index. (a) A mapping (as a dictionary or Series), used to lookup and transform the labels in the previous index. Previous labels not found in the mapping will be reused. (b) A function, returning a hashable, that is applied to each label in the previous index. (c) The IndexAutoFactory type, to apply an auto-incremented integer index. (d) An index initializer, i.e., either an iterable of hashables or an Index instance.

>>> s = sf.Series((0, 62, 13), index=('Venus', 'Saturn', 'Neptune'), dtype=np.int64)

>>> s.relabel({'Venus': 'Mercury'})
<Series>
<Index>
Mercury  0
Saturn   62
Neptune  13
<object> <int64>
>>> s.relabel(lambda x: x[:2].upper())
<Series>
<Index>
VE       0
SA       62
NE       13
<object> <int64>

Series.relabel_flat() → static_frame.core.series.Series[source]

Return a new Series, where an IndexHierarchy (if defined) is replaced with a flat, one-dimension index of tuples.

Series.relabel_add_level(level: Hashable) → static_frame.core.series.Series[source]

Return a new Series, adding a new root level to an existing IndexHierarchy, or creating an IndexHierarchy if one is not yet defined.

Parameters

level – A hashable value to be used as a new root level, extending or creating an IndexHierarchy

Series.relabel_drop_level(count: int = 1) → static_frame.core.series.Series[source]

Return a new Series, dropping one or more levels from a either the root or the leaves of an IndexHierarchy. The resulting index must be unique.

Parameters

count – A positive integer drops that many outer-most (root) levels; a negative integer drops that many inner-most (leaf)levels.

8.2. Frame

Frame.reindex(index: Union[IndexBase, Iterable[Hashable], Iterable[Sequence[Hashable]], Generator[Hashable, None, None], None] = None, columns: Union[IndexBase, Iterable[Hashable], Iterable[Sequence[Hashable]], Generator[Hashable, None, None], None] = None, fill_value=nan, own_index: bool = False, own_columns: bool = False) → static_frame.core.frame.Frame[source]

Return a new Frame with labels defined by the provided index. The size and ordering of the data is determined by the newly provided index, where data will continue to be aligned under labels found in both the new and the old index. Labels found only in the new index will be filled with fill_value.

Parameters
  • index – An iterable of unique, hashable values, or another Index or IndexHierarchy, to be used as the labels of the index.

  • columns – An iterable of unique, hashable values, or another Index or IndexHierarchy, to be used as the labels of the index.

  • fill_value – A value to be used to fill space created by a new index that has values not found in the previous index.

  • own_index – Flag the passed index as ownable by this Frame. Primarily used by internal clients.

  • own_columns – Flag the passed columns as ownable by this Frame. Primarily used by internal clients.

>>> f = sf.Frame.from_dict(dict(diameter=(12756, 6792, 142984), mass=(5.97, 0.642, 1898)), index=('Earth', 'Mars', 'Jupiter'), dtypes=dict(diameter=np.int64))
>>> f
<Frame>
<Index> diameter mass      <<U8>
<Index>
Earth   12756    5.97
Mars    6792     0.642
Jupiter 142984   1898.0
<<U7>   <int64>  <float64>

>>> f.reindex(index=('Jupiter', 'Mars', 'Mercury'), columns=('density', 'mass'))
<Frame>
<Index> density   mass      <<U7>
<Index>
Jupiter nan       1898.0
Mars    nan       0.642
Mercury nan       nan
<<U7>   <float64> <float64>

Frame.relabel(index: Union[Callable[[...], Any], Mapping[Hashable, Any], Series, Type[static_frame.core.index_auto.IndexAutoFactory], IndexBase, Iterable[Hashable], Iterable[Sequence[Hashable]], Generator[Hashable, None, None], None] = None, columns: Union[Callable[[...], Any], Mapping[Hashable, Any], Series, Type[static_frame.core.index_auto.IndexAutoFactory], IndexBase, Iterable[Hashable], Iterable[Sequence[Hashable]], Generator[Hashable, None, None], None] = None) → static_frame.core.frame.Frame[source]

Return a new Frame with transformed labels on the index. The size and ordering of the data is never chagned in a relabeling operation. The resulting index must be unique.

Parameters
  • index – One of the following types, used to create a new Index with the same size as the previous index. (a) A mapping (as a dictionary or Series), used to lookup and transform the labels in the previous index. Previous labels not found in the mapping will be reused. (b) A function, returning a hashable, that is applied to each label in the previous index. (c) The IndexAutoFactory type, to apply an auto-incremented integer index. (d) An index initializer, i.e., either an iterable of hashables or an Index instance.

  • columns – One of the following types, used to create a new Index with the same size as the previous index. (a) A mapping (as a dictionary or Series), used to lookup and transform the labels in the previous index. Previous labels not found in the mapping will be reused. (b) A function, returning a hashable, that is applied to each label in the previous index. (c) The IndexAutoFactory type, to apply an auto-incremented integer index. (d) An index initializer, i.e., either an iterable of hashables or an Index instance.

>>> f = sf.Frame.from_dict(dict(diameter=(12756, 6792, 142984), mass=(5.97, 0.642, 1898)), index=('Earth', 'Mars', 'Jupiter'), dtypes=dict(diameter=np.int64))

>>> f.relabel(index=lambda x: x[:2].upper(), columns={'mass': 'mass(1e24kg)'})
<Frame>
<Index>  diameter mass(1e24kg) <object>
<Index>
EA       12756    5.97
MA       6792     0.642
JU       142984   1898.0
<object> <int64>  <float64>

Frame.relabel_flat(index: bool = False, columns: bool = False) → static_frame.core.frame.Frame[source]

Return a new Frame, where an IndexHierarchy (if defined) is replaced with a flat, one-dimension index of tuples.

Parameters
  • index – Boolean to flag flatening on the index.

  • columns – Boolean to flag flatening on the columns.

Frame.relabel_add_level(index: Hashable = None, columns: Hashable = None) → static_frame.core.frame.Frame[source]

Return a new Frame, adding a new root level to an existing IndexHierarchy, or creating an IndexHierarchy if one is not yet defined.

Parameters
  • index – A hashable value to be used as a new root level, extending or creating an IndexHierarchy

  • columns – A hashable value to be used as a new root level, extending or creating an IndexHierarchy

Frame.relabel_drop_level(index: int = 0, columns: int = 0) → static_frame.core.frame.Frame[source]

Return a new Frame, dropping one or more levels from a either the root or the leaves of an IndexHierarchy. The resulting index must be unique.

Parameters
  • index – A positive integer drops that many outer-most (root) levels; a negative integer drops that many inner-most (leaf)levels. Default is zero.

  • columns – A positive integer drops that many outer-most (root) levels; a negative integer drops that many inner-most (leaf)levels. Default is zero.

Frame.set_index(column: Union[int, numpy.integer, slice, List[Any], None, Index, Series, numpy.ndarray], *, drop: bool = False, index_constructor=<class 'static_frame.core.index.Index'>) → static_frame.core.frame.Frame[source]

Return a new frame produced by setting the given column as the index, optionally removing that column from the new Frame.

Frame.set_index_hierarchy(columns: Union[int, numpy.integer, slice, List[Any], None, Index, Series, numpy.ndarray], *, drop: bool = False, index_constructors: Optional[Sequence[Callable[[...], IndexBase]]] = None) → static_frame.core.frame.Frame[source]

Given an iterable of column labels, return a new Frame with those columns as an IndexHierarchy on the index.

Parameters
  • columns – Iterable of column labels.

  • drop – Boolean to determine if selected columns should be removed from the data.

  • index_constructors – Optionally provide a sequence of Index constructors, of length equal to depth, to be used in converting columns Index components in the IndexHierarchy.

Returns

Frame

Deviations from Pandas

The functionality of the Pandas pd.DataFrame.rename() and pd.Series.rename() is available with Frame.relabel() and Series.relabel(), respectively. The functionality of the Pandas pd.DataFrame.reset_index() and pd.Series.reset_index() is available by providing the IndexAutoFactory type to Frame.relabel() and Series.relabel(), respectively.

8.3. Index

Index.relabel(mapper: Union[Callable[[...], Any], Mapping[Hashable, Any], Series]) → static_frame.core.index.Index[source]

Return a new Index with labels replaced by the callable or mapping; order will be retained. If a mapping is used, the mapping need not map all origin keys.

>>> index = sf.Index(('Venus', 'Saturn', 'Neptune'))
>>> index.relabel({'Venus': 'Mars'})
<Index>
Mars
Saturn
Neptune
<object>

>>> index = sf.Index(('Venus', 'Saturn', 'Neptune'))
>>> index.relabel({'Neptune': 'Uranus'})
<Index>
Venus
Saturn
Uranus
<object>

>>> index.relabel(lambda x: x[:2].upper())
<Index>
VE
SA
NE
<object>