Skip to content

DataFrame.grb

The namespace is registered on every polars DataFrame when polarobi is imported. It is the class polarobi.GurobiFrame, so GurobiFrame(df), or grb(df) with from polarobi import GurobiFrame as grb, is the same namespace in a form type checkers understand; see Type Checking. All methods take the gurobipy model to work on as their first argument.

Variables and constraints are identified by an idx column holding their Gurobi column or row index at creation. Like Var.index, these shift when elements before them are removed. If the model has pending deletions from model.remove(...), polarobi calls model.update() first so its indices stay aligned with Gurobi's; otherwise it leaves the model lazy. The types used in the signatures are documented under polarobi.

add_vars

add_vars(
    model: Model,
    *,
    by: Sequence[str] | None = None,
    lb: IntoColumn | None = None,
    ub: IntoColumn | None = None,
    obj: IntoColumn | None = None,
    vtype: VType | None = None,
    name: str | Expr | None = None,
    idx: str = "idx",
) -> pl.DataFrame

Add one variable per row, returning this frame with the variables' column index idx. Like Model.addVars, raises KeyError for duplicate keys, but before adding anything.

Parameters:

Name Type Description Default
model Model

The Gurobi model to add variables to.

required
by Sequence[str] | None

Columns to use as keys for variable names.

None
lb IntoColumn | None

Lower bound for variables.

None
ub IntoColumn | None

Upper bound for variables.

None
obj IntoColumn | None

Objective coefficient for variables.

None
vtype VType | None

Variable type to create: C (continuous), B (binary), I (integer), S (semi-continuous) or N (semi-integer).

None
name str | Expr | None

Name for variables.

None
idx str

Column name for variable indices.

'idx'

Returns:

Type Description
DataFrame

DataFrame with added variables.

add_constr

add_constr(
    model: Model,
    *,
    sense: Sense,
    rhs: float = 0.0,
    name: str | None = None,
    var: str = "idx",
    coef: IntoColumn = 1.0,
    idx: str = "idx",
) -> pl.DataFrame

Add a single linear constraint over all of this frame's terms, sum(coef * x[var]) <sense> rhs, returning a one-row frame with its row index idx.

Parameters:

Name Type Description Default
model Model

The Gurobi model to add the constraint to.

required
sense Sense

Constraint sense: < or <= (less than or equal), > or >= (greater than or equal), or = or == (equal).

required
rhs float

Right-hand side for the constraint.

0.0
name str | None

Name for the constraint.

None
var str

Column name for variable indices in this frame.

'idx'
coef IntoColumn

Coefficient for each term.

1.0
idx str

Column name for the constraint index.

'idx'

Returns:

Type Description
DataFrame

One-row DataFrame with the constraint's index, or an empty DataFrame if this frame has no rows.

add_constrs

add_constrs(
    model: Model,
    rows: DataFrame | None = None,
    *,
    by: str | Sequence[str],
    sense: Sense | Series | Expr,
    rhs: IntoColumn = 0.0,
    name: str | Expr | None = None,
    var: str = "idx",
    coef: IntoColumn = 1.0,
    drop_empty: bool = True,
    idx: str = "idx",
) -> pl.DataFrame

Add one linear constraint per group of this frame's terms, sum(coef * x[var]) <sense> rhs.

Parameters:

Name Type Description Default
model Model

The Gurobi model to add constraints to.

required
rows DataFrame | None

DataFrame with one row per constraint containing the keys by. Defaults to distinct keys of the terms in order of appearance.

None
by str | Sequence[str]

Columns to use as keys for grouping terms into constraints.

required
sense Sense | Series | Expr

Constraint sense: < or <= (less than or equal), > or >= (greater than or equal), or = or == (equal).

required
rhs IntoColumn

Right-hand side for constraints.

0.0
name str | Expr | None

Name for constraints.

None
var str

Column name for variable indices in this frame.

'idx'
coef IntoColumn

Coefficient for each term.

1.0
drop_empty bool

Whether to drop rows in rows that have no matching terms.

True
idx str

Column name for constraint indices.

'idx'

Returns:

Type Description
DataFrame

DataFrame with added constraints.

get

get(
    model: Model,
    attr: str,
    *,
    idx: str = "idx",
    dtype: type[Float64] | type[Int32] = pl.Float64,
) -> pl.DataFrame

Return this frame with attr (e.g. "X", "Pi", "UB") of its variables or constraints added. Values at Gurobi's infinity read as inf, as in gurobipy.

Parameters:

Name Type Description Default
model Model

The Gurobi model to read the attribute from.

required
attr str

Name of the Gurobi attribute to retrieve.

required
idx str

Column name for variable or constraint indices.

'idx'
dtype type[Float64] | type[Int32]

Polars DType for the returned attribute column (pl.Float64 or pl.Int32).

Float64

Returns:

Type Description
DataFrame

DataFrame with the attribute column added.

set

set(
    model: Model, attr: str, values: Series | str | float, *, idx: str = "idx"
) -> None

Set attr of this frame's variables or constraints. Integer values use Gurobi's int attribute API.

Parameters:

Name Type Description Default
model Model

The Gurobi model to set the attribute on.

required
attr str

Name of the Gurobi attribute to set.

required
values Series | str | float

Attribute values as a scalar, a Series, or a column name.

required
idx str

Column name for variable or constraint indices.

'idx'

handles

handles(
    model: Model, *, idx: str = "idx", kind: Literal["var"] = "var"
) -> list[gp.Var]
handles(
    model: Model, *, idx: str = "idx", kind: Literal["constr"]
) -> list[gp.Constr]
handles(
    model: Model, *, idx: str = "idx", kind: Literal["var", "constr"] = "var"
) -> list[gp.Var] | list[gp.Constr]

Get the gurobipy Var or Constr handles of this frame's rows.

Parameters:

Name Type Description Default
model Model

The Gurobi model the variables or constraints belong to.

required
idx str

Column name for variable or constraint indices.

'idx'
kind Literal['var', 'constr']

Handle type to return: var (variable) or constr (constraint).

'var'

Returns:

Type Description
list[Var] | list[Constr]

List of Gurobi handles.

tupledict

tupledict(
    model: Model,
    by: str | Sequence[str],
    *,
    idx: str = "idx",
    kind: Literal["var"] = "var",
) -> gp.tupledict[Any, gp.Var]
tupledict(
    model: Model,
    by: str | Sequence[str],
    *,
    idx: str = "idx",
    kind: Literal["constr"],
) -> gp.tupledict[Any, gp.Constr]
tupledict(
    model: Model,
    by: str | Sequence[str],
    *,
    idx: str = "idx",
    kind: Literal["var", "constr"] = "var",
) -> gp.tupledict[Any, gp.Var] | gp.tupledict[Any, gp.Constr]

Return a gurobipy tupledict of this frame's handles keyed like Model.addVars keys it: by the values of by for one column, else by tuples.

Parameters:

Name Type Description Default
model Model

The Gurobi model the variables or constraints belong to.

required
by str | Sequence[str]

Column or columns to use as keys.

required
idx str

Column name for variable or constraint indices.

'idx'
kind Literal['var', 'constr']

Handle type to return: var (variable) or constr (constraint).

'var'

Returns:

Type Description
tupledict[Any, Var] | tupledict[Any, Constr]

tupledict mapping keys to Gurobi handles.