Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Upcoming Version

* The internal handling of `Solution` objects was improved for more consistency. Solution objects created from solver calls now preserve the exact index names from the input file.
* Multiplication of a linear expression by a constant value may now introduce new dimensions.
* Added method `unstack` to `LinearExpression`, `Variable` and `Constraint` to unstack a dimension.

Version 0.4.4
--------------
Expand Down
2 changes: 2 additions & 0 deletions linopy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,8 @@ def to_polars(self):

stack = conwrap(Dataset.stack)

unstack = conwrap(Dataset.unstack)

iterate_slices = iterate_slices


Expand Down
2 changes: 2 additions & 0 deletions linopy/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,8 @@ def to_polars(self) -> pl.DataFrame:

stack = exprwrap(Dataset.stack)

unstack = exprwrap(Dataset.unstack)

iterate_slices = iterate_slices


Expand Down
2 changes: 2 additions & 0 deletions linopy/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,8 @@ def equals(self, other: Variable) -> bool:

stack = varwrap(Dataset.stack)

unstack = varwrap(Dataset.unstack)

iterate_slices = iterate_slices


Expand Down
2 changes: 1 addition & 1 deletion test/test_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def test_constraint_wrapped_methods(x, y):
con.rename({"first": "new_labels"})
con.rename_dims({"first": "new_labels"})
con.roll({"first": 1})
con.stack(new_dim=("first", "second"))
con.stack(new_dim=("first", "second")).unstack("new_dim")


def test_anonymous_constraint_sel(x, y):
Expand Down
7 changes: 7 additions & 0 deletions test/test_linear_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,13 @@ def test_variable_stack(v):
assert result.coord_dims == ("new",)


def test_variable_unstack(v):
result = v.to_linexpr().expand_dims("new_dim").stack(new=("new_dim", "dim_2"))
result = result.unstack("new")
assert isinstance(result, LinearExpression)
assert result.coord_dims == ("new_dim", "dim_2")


def test_linear_expression_diff(v):
diff = v.to_linexpr().diff("dim_2")
assert diff.nterm == 2
Expand Down
6 changes: 6 additions & 0 deletions test/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ def test_variable_stack(x):
assert result.dims == ("new",)


def test_variable_unstack(x):
result = x.expand_dims("new_dim").stack(new=("new_dim", "first")).unstack("new")
assert isinstance(result, linopy.variables.Variable)
assert result.dims == ("new_dim", "first")


def test_variable_flat(x):
result = x.flat
assert isinstance(result, pd.DataFrame)
Expand Down
Loading