Spaces and Vectors#

A Space is the labels of a state, of the observations, or of one axis of a matrix: an ordered set of named blocks, each labeled by its own pandas index. A Vector is values on a space, one for each label. fips keeps labels as they are given: it does not convert them to numbers or round them, and it refuses duplicate and missing labels.

In [1]: import pandas as pd

In [2]: import fips

In [3]: prior = fips.Vector.from_blocks(
   ...:     {
   ...:         "flux": pd.Series([1.0, 2.0], index=pd.Index([1, 2], name="cell")),
   ...:         "bias": pd.Series([0.0], index=pd.Index(["A"], name="site")),
   ...:     },
   ...:     name="prior",
   ...: )
   ...: 

In [4]: prior.space
Out[4]: Space(flux: 2 x ['cell'], bias: 1 x ['site'])

In [5]: prior.to_series()
Out[5]: 
block  cell  site
flux   1     nan     1.0
       2     nan     2.0
bias   NaN   A       0.0
Name: prior, dtype: float64

In [6]: prior["flux"].space.select("flux")
Out[6]: Index([1, 2], dtype='int64', name='cell')

A vector of one block is a single component of the state or of the observations: fips.Vector.from_series(series, block="flux").

Space#

Constructor#

Space(blocks[, levels])

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

Space.from_index(index)

Build the space an index with a block level describes.

Attributes#

Space.names

The names of the blocks, in order.

Space.levels

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

Space.index

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

Space.offsets

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

Selection#

Space.select(block)

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

Space.slice(block)

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

Alignment and labels#

Space.align(other)

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

Space.round_labels(decimals)

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

Vector#

Constructor#

Vector(values, space[, name])

Values on a Space, one for each label.

Vector.from_series(series[, block])

Build a vector from a Series, reading its labels.

Vector.from_blocks(blocks[, name])

Build a vector from one Series (or vector of one block) for each block.

Attributes#

Vector.values

The values, one for each label, in the space's order (read-only).

Vector.space

The labels of the values.

Vector.name

a string, or None.

Vector.index

The labels as one MultiIndex with the block level first (Space.index).

Selection and conversion#

Vector.__getitem__(block)

Return the vector of one block.

Vector.to_series([block])

Return the values as a Series: the whole vector, or one block on its own labels.

Vector.to_xarray()

Return the values as xarray, each block on its own levels.

Alignment and labels#

Vector.align(space)

Return the values on space, which must have exactly the same labels.

Vector.combine_first(other)

Return other with this vector's values where this vector has the label.

Vector.round_labels(decimals)

Return the vector with its float labels rounded (Space.round_labels()).