Skip to content

Commit

Permalink
explicitly use from_pandas_multiindex in groupby when
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianHofmann committed Oct 27, 2023
1 parent 094f8ad commit 29edc1a
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions linopy/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,17 @@ def sum(self, use_fallback=False, **kwargs):
idx = pd.MultiIndex.from_arrays(
arrays, names=[group_name, GROUPED_TERM_DIM]
)
ds = self.data.assign_coords({group_dim: idx})
coords = xr.Coordinates.from_pandas_multiindex(idx, group_dim)
ds = self.data.assign_coords(coords)
ds = ds.unstack(group_dim, fill_value=LinearExpression._fill_value)
ds = LinearExpression._sum(ds, dims=GROUPED_TERM_DIM)

if int_map is not None:
index = ds.indexes["group"].map({v: k for k, v in int_map.items()})
index.names = orig_group.columns
index.name = group_name
ds = xr.Dataset(ds.assign_coords({group_name: index}))
coords = xarray.Coordinates.from_pandas_multiindex(index, group_name)
ds = xr.Dataset(ds.assign_coords(coords))

return LinearExpression(ds, self.model)

Expand Down

0 comments on commit 29edc1a

Please sign in to comment.