linopy.variables.Variable

linopy.variables.Variable#

class linopy.variables.Variable(data, model, name, skip_broadcast=False)#

Variable container for storing variable labels.

The Variable class is a subclass of xr.DataArray hence most xarray functions can be applied to it. However most arithmetic operations are overwritten. Like this one can easily combine variables into a linear expression.

Examples

>>> from linopy import Model
>>> import pandas as pd
>>> m = Model()
>>> x = m.add_variables(pd.Series([0, 0]), 1, name="x")
>>> y = m.add_variables(4, pd.Series([8, 10]), name="y")

Add variable together:

>>> x + y  
Linear Expression with 2 term(s):
----------------------------------

Dimensions:  (dim_0: 2, _term: 2)
Coordinates:
  * dim_0    (dim_0) int64 0 1
Dimensions without coordinates: _term
Data:
    coeffs   (dim_0, _term) int64 1 1 1 1
    vars     (dim_0, _term) int64 0 2 1 3

Multiply them with a coefficient:

>>> 3 * x  
Linear Expression with 1 term(s):
----------------------------------

Dimensions:  (dim_0: 2, _term: 1)
Coordinates:
  * dim_0    (dim_0) int64 0 1
Dimensions without coordinates: _term
Data:
    coeffs   (dim_0, _term) int64 3 3
    vars     (dim_0, _term) int64 0 1

Further operations like taking the negative and subtracting are supported.

__init__(data, model, name, skip_broadcast=False)#

Initialize the Variable.

Parameters:
  • labels (xarray.Dataset) – data of the variable.

  • model (linopy.Model) – Underlying model.

Methods

__init__(data, model, name[, skip_broadcast])

Initialize the Variable.

add(other[, join])

Add variables to linear expressions or other variables.

assign(**fields)

Wrapper for the xarray assign_multiindex_safe function for linopy.Variable

assign_attrs(*args, **kwargs)

Wrapper for the xarray DataWithCoords.assign_attrs function for linopy.Variable

assign_coords([coords])

Wrapper for the xarray DataWithCoords.assign_coords function for linopy.Variable

assign_multiindex_safe(**fields)

Wrapper for the xarray assign_multiindex_safe function for linopy.Variable

bfill(dim[, limit])

Backward fill the variable along a dimension.

broadcast_like(other[, exclude])

Wrapper for the xarray Dataset.broadcast_like function for linopy.Variable

compute(**kwargs)

Wrapper for the xarray Dataset.compute function for linopy.Variable

cumsum([dim, skipna, keep_attrs])

Cumulated sum along a given axis.

diff(dim[, n])

Calculate the n-th order discrete difference along the given dimension.

div(other[, join])

Divide variables with a coefficient.

dot(other)

Generalized dot product for linopy and compatible objects.

drop_isel([indexers])

Wrapper for the xarray Dataset.drop_isel function for linopy.Variable

drop_sel([labels, errors])

Wrapper for the xarray Dataset.drop_sel function for linopy.Variable

eq(rhs[, join])

Equality constraint.

equals(other)

Check if this Variable is equal to another.

expand_dims([dim, axis, ...])

Wrapper for the xarray Dataset.expand_dims function for linopy.Variable

ffill(dim[, limit])

Forward fill the variable along a dimension.

fillna(fill_value)

Fill missing values with a variable.

fix([value, decimals, overwrite])

Fix the variable to a given value by adding an equality constraint.

ge(rhs[, join])

Greater than or equal constraint.

get_solver_attribute(attr)

Get an attribute from the solver model.

groupby(group[, restore_coord_dims])

Returns a LinearExpressionGroupBy object for performing grouped operations.

isel([indexers, drop, missing_dims])

Wrapper for the xarray Dataset.isel function for linopy.Variable

isnull()

Get a boolean mask with true values where there is missing values.

iterate_slices([slice_size, slice_dims])

Generate slices of an xarray Dataset or DataArray with a specified soft maximum size.

le(rhs[, join])

Less than or equal constraint.

mul(other[, join])

Multiply variables with a coefficient.

pow(other)

Power of the variables with a coefficient.

print([display_max_rows])

Print the linear expression.

relax()

Relax the integrality of this variable.

rename([name_dict])

Wrapper for the xarray Dataset.rename function for linopy.Variable

roll([shifts, roll_coords])

Wrapper for the xarray Dataset.roll function for linopy.Variable

rolling([dim, min_periods, center])

Rolling window object.

sanitize()

Sanitize variable by ensuring int dtype with fill value of -1.

sel([indexers, method, tolerance, drop])

Wrapper for the xarray Dataset.sel function for linopy.Variable

set_index([indexes, append])

Wrapper for the xarray Dataset.set_index function for linopy.Variable

shift([shifts, fill_value])

Wrapper for the xarray Dataset.shift function for linopy.Variable with default arguments: {'fill_value': {'labels': -1, 'lower': nan, 'upper': nan}}

stack([dim, create_index, index_cls])

Wrapper for the xarray Dataset.stack function for linopy.Variable

sub(other[, join])

Subtract linear expressions or other variables from the variables.

sum([dim])

Sum the variables over all or a subset of dimensions.

swap_dims([dims_dict])

Wrapper for the xarray Dataset.swap_dims function for linopy.Variable

to_linexpr([coefficient])

Create a linear expression from the variables.

to_pandas()

Convert the variable labels to a pandas Series.

to_polars()

Convert all variables to a single polars DataFrame.

unfix()

Remove the fix constraint for this variable.

unrelax()

Restore the original integrality type of a relaxed variable.

unstack([dim, fill_value, sparse])

Wrapper for the xarray Dataset.unstack function for linopy.Variable

where(cond[, other])

Filter variables based on a condition.

Attributes

at

Access a single value of the variable.

attrs

Get the attributes of the variable.

coord_dims

Get the coordinate dimensions of the variable.

coord_names

Get the names of the coordinates.

coord_sizes

Get the coordinate sizes of the variable.

coords

Get the coordinates of the variable.

data

Get the data of the variable.

dims

Get the dimensions of the variable.

fixed

Return whether the variable is currently fixed.

flat

Convert the variable to a pandas DataFrame.

indexes

Get the indexes of the variable.

labels

Return the labels of the variable.

loc

Indexing the variable using coordinates.

lower

Get the lower bounds of the variables.

mask

Get the mask of the variable.

model

Return the model of the variable.

name

Return the name of the variable.

ndim

Get the number of dimensions of the variable.

range

Return the range of the variable.

relaxed

Return whether the variable is currently relaxed.

shape

Get the shape of the variable.

size

Get the size of the variable.

sizes

Get the sizes of the variable.

sol

Get the optimal values of the variable.

solution

Get the optimal values of the variable.

type

Type of the variable.

upper

Get the upper bounds of the variables.