Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 6 additions & 5 deletions linopy/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def sum(self, use_fallback: bool = False, **kwargs: Any) -> LinearExpression:
index.names = [str(col) for col in orig_group.columns]
index.name = GROUP_DIM
new_coords = Coordinates.from_pandas_multiindex(index, GROUP_DIM)
ds = xr.Dataset(ds.assign_coords(new_coords))
ds = ds.assign_coords(new_coords)

ds = ds.rename({GROUP_DIM: final_group_name})
return LinearExpression(ds, self.model)
Expand Down Expand Up @@ -392,7 +392,7 @@ def __init__(self, data: Dataset | Any | None, model: Model) -> None:
data = assign_multiindex_safe(data, **coeffs_vars_dict)

# transpose with new Dataset to really ensure correct order
data = Dataset(data.transpose(..., TERM_DIM))
data = data.transpose(..., TERM_DIM)

# ensure helper dimensions are not set as coordinates
if drop_dims := set(HELPER_DIMS).intersection(data.coords):
Expand Down Expand Up @@ -2098,7 +2098,7 @@ def __init__(self, data: Dataset | None, model: Model) -> None:
raise ValueError(f"Size of dimension {FACTOR_DIM} must be 2.")

# transpose data to have _term as last dimension and _factor as second last
data = xr.Dataset(data.transpose(..., FACTOR_DIM, TERM_DIM))
data = data.transpose(..., FACTOR_DIM, TERM_DIM)
self._data = data

@property
Expand Down Expand Up @@ -2340,7 +2340,7 @@ def merge(
LinearExpression | QuadraticExpression | variables.Variable | Dataset
],
dim: str = TERM_DIM,
cls: type[GenericExpression] = None, # type: ignore
cls: type[GenericExpression] | None = None,
join: str | None = None,
**kwargs: Any,
) -> GenericExpression:
Expand Down Expand Up @@ -2384,7 +2384,7 @@ def merge(
has_quad_expression = any(type(e) is QuadraticExpression for e in exprs)
has_linear_expression = any(type(e) is LinearExpression for e in exprs)
if cls is None:
cls = QuadraticExpression if has_quad_expression else LinearExpression
cls = QuadraticExpression if has_quad_expression else LinearExpression # type: ignore[assignment]

if cls is QuadraticExpression and dim == TERM_DIM and has_linear_expression:
raise ValueError(
Expand Down Expand Up @@ -2445,6 +2445,7 @@ def merge(
for d in set(HELPER_DIMS) & set(ds.coords):
ds = ds.reset_index(d, drop=True)

assert cls is not None
return cls(ds, model)


Expand Down
2 changes: 1 addition & 1 deletion linopy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def parameters(self, value: Dataset | Mapping) -> None:
"""
Set the parameters of the model.
"""
self._parameters = Dataset(value)
self._parameters = value if isinstance(value, Dataset) else Dataset(value)

@property
def solution(self) -> Dataset:
Expand Down
Loading