Skip to content

Commit 6a788ac

Browse files
add unstack method (#350)
* add unstack method * Update test/test_linear_expression.py
1 parent ecdc1da commit 6a788ac

File tree

7 files changed

+21
-1
lines changed

7 files changed

+21
-1
lines changed

doc/release_notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Upcoming Version
66

77
* 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.
88
* Multiplication of a linear expression by a constant value may now introduce new dimensions.
9+
* Added method `unstack` to `LinearExpression`, `Variable` and `Constraint` to unstack a dimension.
910

1011
Version 0.4.4
1112
--------------

linopy/constraints.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,8 @@ def to_polars(self):
675675

676676
stack = conwrap(Dataset.stack)
677677

678+
unstack = conwrap(Dataset.unstack)
679+
678680
iterate_slices = iterate_slices
679681

680682

linopy/expressions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,8 @@ def to_polars(self) -> pl.DataFrame:
14721472

14731473
stack = exprwrap(Dataset.stack)
14741474

1475+
unstack = exprwrap(Dataset.unstack)
1476+
14751477
iterate_slices = iterate_slices
14761478

14771479

linopy/variables.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,8 @@ def equals(self, other: Variable) -> bool:
10951095

10961096
stack = varwrap(Dataset.stack)
10971097

1098+
unstack = varwrap(Dataset.unstack)
1099+
10981100
iterate_slices = iterate_slices
10991101

11001102

test/test_constraint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def test_constraint_wrapped_methods(x, y):
197197
con.rename({"first": "new_labels"})
198198
con.rename_dims({"first": "new_labels"})
199199
con.roll({"first": 1})
200-
con.stack(new_dim=("first", "second"))
200+
con.stack(new_dim=("first", "second")).unstack("new_dim")
201201

202202

203203
def test_anonymous_constraint_sel(x, y):

test/test_linear_expression.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,13 @@ def test_variable_stack(v):
669669
assert result.coord_dims == ("new",)
670670

671671

672+
def test_linear_expression_unstack(v):
673+
result = v.to_linexpr().expand_dims("new_dim").stack(new=("new_dim", "dim_2"))
674+
result = result.unstack("new")
675+
assert isinstance(result, LinearExpression)
676+
assert result.coord_dims == ("new_dim", "dim_2")
677+
678+
672679
def test_linear_expression_diff(v):
673680
diff = v.to_linexpr().diff("dim_2")
674681
assert diff.nterm == 2

test/test_variable.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ def test_variable_stack(x):
267267
assert result.dims == ("new",)
268268

269269

270+
def test_variable_unstack(x):
271+
result = x.expand_dims("new_dim").stack(new=("new_dim", "first")).unstack("new")
272+
assert isinstance(result, linopy.variables.Variable)
273+
assert result.dims == ("new_dim", "first")
274+
275+
270276
def test_variable_flat(x):
271277
result = x.flat
272278
assert isinstance(result, pd.DataFrame)

0 commit comments

Comments
 (0)