fips.Space#

class fips.Space(blocks, levels=None)[source]#

An ordered set of named blocks, each labeled by its own pandas index.

A space labels a state vector, the observations, or one axis of a matrix. Each block keeps the index it was given, with its own levels and dtypes; index puts them together as one pandas.MultiIndex with a block level first. A level that one block has and another does not is missing (NaN) on the other block’s rows. The labels are never changed: they are not converted to numbers and not rounded (see round_labels()).

Parameters:
  • blocks (Mapping[str, Index]) – The blocks in order, each name mapped to its labels. A name that is not a string is converted to one. Every level must be named, no level may be called block, and a block’s labels must be unique and not missing.

  • levels (Sequence[Hashable] | None) – The order of the levels after block in index: every level of every block, once. By default, the order in which they first appear.

Raises:

ValueError – If two blocks have the same name, a block has no labels, an unnamed level, a block level, a missing label or a label twice, or levels does not name each level once.

See also

Vector

Values on a space.

Examples

>>> import pandas as pd
>>> from fips import Space
>>> space = Space(
...     {
...         "flux": pd.Index([1, 2], name="cell"),
...         "bias": pd.Index(["A"], name="site"),
...     }
... )
>>> space.index
MultiIndex([('flux', 1.0, nan),
            ('flux', 2.0, nan),
            ('bias', nan, 'A')],
           names=['block', 'cell', 'site'])

The padding shows cell as floats, but the level keeps its integers, and a block comes back as it was given:

>>> space.index.levels[1]
Index([1, 2], dtype='int64', name='cell')
>>> space.select("flux")
Index([1, 2], dtype='int64', name='cell')
>>> space.slice("bias")
slice(2, 3, None)

Attributes

index

The labels of every block as one MultiIndex, with the block level first.

levels

The names of the levels after block in index, in order.

names

The names of the blocks, in order.

offsets

Where each block starts in index, and the total length last.

Methods

align(other)

Return where each label of this space is in other: an exact join.

from_index(index)

Build the space an index with a block level describes.

round_labels(decimals)

Return the space with every float level rounded to decimals places.

select(block)

Return the labels of block, as the block was given.

slice(block)

Return the positions of block in index, as a slice.