linopy.variables.Variable#
- class linopy.variables.Variable(data, model, name)#
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)#
Initialize the Variable.
- Parameters:
labels (
xarray.Dataset
) – data of the variable.model (
linopy.Model
) – Underlying model.
Methods
__init__
(data, model, name)Initialize the Variable.
add
(other)Add variables to linear expressions or other variables.
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
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)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
equals
(other)ffill
(dim[, limit])Forward fill the variable along a dimension.
fillna
(fill_value)Fill missing values with a variable.
get_solver_attribute
(attr)Get an attribute from the solver model.
groupby
(group[, squeeze, 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.
mul
(other)Multiply variables with a coefficient.
pow
(other)Power of the variables with a coefficient.
print
([display_max_rows])Print the linear expression.
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
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}}
sub
(other)Subtract linear expressions or other variables from the variables.
sum
([dims])Sum the variables over all or a subset of dimensions.
to_linexpr
([coefficient])Create a linear expression from the variables.
to_pandas
()where
(cond[, other])Filter variables based on a condition.
Attributes
attrs
coords
data
Get the data of the variable.
dims
fill_value
flat
Convert the variable to a pandas DataFrame.
indexes
labels
Return the labels of the variable.
loc
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
range
Return the range of the variable.
shape
size
sizes
sol
Get the optimal values of the variable.
solution
Get the optimal values of the variable.
type
Type of the variable.
Get the upper bounds of the variables.