qis.align_weights_to_columns

qis.align_weights_to_columns(weights, columns)[source]

align a weight specification to a set of instrument columns.

A Dict or pd.Series is aligned by name, so the order of the specification does not matter and a column the specification does not name carries nan, which every consumer treats as no position. A List or np.ndarray is positional and must match the column count, since there is no name to align on. This is the normaliser generate_static_weights_schedule() and qis.backtest_model_portfolio share, so that one weight argument means one thing.

Parameters:
  • weights (Dict[str, float] | List[float] | ndarray | Series) – target weights as a Dict or pd.Series keyed by ticker, or as a List or np.ndarray in column order

  • columns (Index) – instrument columns to align to, normally prices.columns

Returns:

weights indexed by columns, nan where the specification does not name a column

Raises:
  • ValueError – if a named specification carries a name outside columns, or a positional specification does not match the number of columns or is not one dimensional

  • NotImplementedError – if weights is of an unsupported type

Return type:

Series