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.

assign_attrs(*args, **kwargs)

Wrapper for the xarray <function DataWithCoords.assign_attrs at 0x7fbd83275280> function for linopy.Variable

assign_coords([coords])

Wrapper for the xarray <function DataWithCoords.assign_coords at 0x7fbd832751f0> function for linopy.Variable

bfill(dim[, limit])

Backward fill the variable along a dimension.

broadcast_like(other[, exclude])

Wrapper for the xarray <function Dataset.broadcast_like at 0x7fbd830bc940> function for linopy.Variable

compute(**kwargs)

Wrapper for the xarray <function Dataset.compute at 0x7fbd830b5940> 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.

drop_isel([indexers])

Wrapper for the xarray <function Dataset.drop_isel at 0x7fbd830c0d30> function for linopy.Variable

drop_sel([labels, errors])

Wrapper for the xarray <function Dataset.drop_sel at 0x7fbd830c0ca0> function for linopy.Variable

equals(other)

ffill(dim[, limit])

Forward fill the variable along a dimension.

fillna(value)

Wrapper for the xarray <function Dataset.fillna at 0x7fbd830c0f70> function for linopy.Variable

groupby(group[, squeeze, restore_coord_dims])

Returns a LinearExpressionGroupBy object for performing grouped operations.

isel([indexers, drop, missing_dims])

Wrapper for the xarray <function Dataset.isel at 0x7fbd830bc5e0> function for linopy.Variable

print([display_max_rows])

Print the linear expression.

rename([name_dict])

Wrapper for the xarray <function Dataset.rename at 0x7fbd830c0040> function for linopy.Variable

roll([shifts, roll_coords])

Wrapper for the xarray <function Dataset.roll at 0x7fbd830c2ee0> 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 <function Dataset.sel at 0x7fbd830bc700> function for linopy.Variable

shift([shifts, fill_value])

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

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

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

range

Return the range of the variable.

shape

size

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.