diff --git a/linopy/expressions.py b/linopy/expressions.py index ca491c3e..33f6d774 100644 --- a/linopy/expressions.py +++ b/linopy/expressions.py @@ -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) @@ -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): @@ -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 @@ -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: @@ -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( @@ -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) diff --git a/linopy/model.py b/linopy/model.py index 2a635680..57acecc2 100644 --- a/linopy/model.py +++ b/linopy/model.py @@ -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: