Skip to content

Commit fd6208d

Browse files
authored
Merge pull request #562 from DHI/test/no_index_name_change_in_input
Don't mutate inputs
2 parents 3e26f41 + d529df2 commit fd6208d

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/modelskill/comparison/_comparison.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,12 @@ def _matched_data_to_xarray(
341341
f"Model data: {m} is of type {df[m].dtype}, it must be numeric"
342342
)
343343

344-
df = df[items.all]
345-
df.index.name = "time"
346-
df = df.rename(columns={items.obs: "Observation"})
347-
ds = df.to_xarray()
344+
ds = (
345+
df.loc[:, items.all]
346+
.rename_axis("time")
347+
.rename(columns={items.obs: "Observation"})
348+
.to_xarray()
349+
)
348350
assert isinstance(ds, xr.Dataset)
349351

350352
ds.attrs["name"] = name if name is not None else items.obs

tests/test_comparer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def tc() -> Comparer:
9494

9595

9696
def test_matched_df(pt_df):
97+
# modelskill doesn't care about the name of the index, but it should be preserved
98+
pt_df.index.name = "dato_tid"
9799
cmp = Comparer.from_matched_data(data=pt_df)
98100
assert cmp.gtype == "point"
99101
assert "m2" in cmp.mod_names
@@ -104,6 +106,14 @@ def test_matched_df(pt_df):
104106
assert cmp.score()["m1"] == pytest.approx(0.5916079783099617)
105107
assert cmp.score()["m2"] == pytest.approx(0.15811388300841905)
106108

109+
# from_matched doesn't modify the original dataframe,. including the name of the index
110+
assert pt_df.index.name == "dato_tid"
111+
assert list(pt_df.columns) == ["Observation", "m1", "m2"]
112+
113+
# but to_dataframe always returns a dataframe with index named "time"
114+
df2 = cmp.to_dataframe()
115+
assert df2.index.name == "time"
116+
107117

108118
def test_matched_skill_geodataframe(pt_df):
109119
cmp = Comparer.from_matched_data(data=pt_df, x=10.0, y=55.0)

0 commit comments

Comments
 (0)