Skip to content
Merged
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
23 changes: 10 additions & 13 deletions test/test_linear_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,7 @@ def test_separate_dims_not_stacked(self) -> None:

assert {"period", "season"} <= set(grouped.dims)
assert "group" not in grouped.dims
assert not isinstance(grouped.data.indexes.get("period"), pd.MultiIndex)
assert not isinstance(grouped.indexes.get("period"), pd.MultiIndex)

def test_sparse_combination_filled(self) -> None:
# (2020, "s") never occurs -> empty term in the grid
Expand All @@ -1468,7 +1468,7 @@ def test_dataframe_grouper_stays_compact(self) -> None:
df = expr.data[["period", "season"]].to_dataframe()[["period", "season"]]
with pytest.warns(LinopySemanticsWarning, match=r"stacked `group` MultiIndex"):
grouped = expr.groupby(df).sum()
assert isinstance(grouped.data.indexes["group"], pd.MultiIndex)
assert isinstance(grouped.indexes["group"], pd.MultiIndex)
assert grouped.sizes["group"] == 3 # observed, not the 2x2=4 grid

@pytest.mark.v1
Expand All @@ -1477,8 +1477,8 @@ def test_dataframe_grouper_stays_compact_v1(self) -> None:
expr = self._expr([2020, 2020, 2030, 2030], list("wwws"))
df = expr.data[["period", "season"]].to_dataframe()[["period", "season"]]
grouped = expr.groupby(df).sum()
assert not isinstance(grouped.data.indexes["group"], pd.MultiIndex)
assert {"period", "season"} <= set(grouped.data.coords)
assert not isinstance(grouped.indexes["group"], pd.MultiIndex)
assert {"period", "season"} <= set(grouped.coords)
assert grouped.sizes["group"] == 3

@pytest.mark.legacy
Expand All @@ -1489,14 +1489,14 @@ def test_namelist_observed_warns_legacy(self) -> None:
expr = self._expr([2020, 2020, 2030, 2030], list("wwws"))
with pytest.warns(LinopySemanticsWarning, match=r"stacked `group` MultiIndex"):
grouped = expr.groupby(["period", "season"]).sum(observed=True)
assert isinstance(grouped.data.indexes["group"], pd.MultiIndex)
assert isinstance(grouped.indexes["group"], pd.MultiIndex)

@pytest.mark.v1
def test_namelist_observed_flat_v1(self) -> None:
expr = self._expr([2020, 2020, 2030, 2030], list("wwws"))
grouped = expr.groupby(["period", "season"]).sum(observed=True)
assert not isinstance(grouped.data.indexes["group"], pd.MultiIndex)
assert {"period", "season"} <= set(grouped.data.coords)
assert not isinstance(grouped.indexes["group"], pd.MultiIndex)
assert {"period", "season"} <= set(grouped.coords)

@pytest.mark.legacy
def test_group_multiindex_reset_index_matches_v1(self) -> None:
Expand All @@ -1508,13 +1508,10 @@ def test_group_multiindex_reset_index_matches_v1(self) -> None:
warnings.simplefilter("ignore")
legacy_mi = expr.groupby(["period", "season"]).sum(observed=True)
converted = legacy_mi.reset_index("group")
prev = options["semantics"]
try:
options["semantics"] = "v1"
with options as o:
o.set_value(semantics="v1")
v1_flat = expr.groupby(["period", "season"]).sum(observed=True)
finally:
options["semantics"] = prev
assert not isinstance(converted.data.indexes["group"], pd.MultiIndex)
assert not isinstance(converted.indexes["group"], pd.MultiIndex)
assert_linequal(converted, v1_flat)

def test_blowup_warns_when_sparse(self) -> None:
Expand Down