From 13bcacb4a55ca15a1af87e666cb9c519c6f33628 Mon Sep 17 00:00:00 2001 From: Sara Roth Date: Thu, 15 Aug 2024 22:46:55 +0200 Subject: [PATCH] Fixing copying of data_value_type in from_pandas also changing tests to cover this change and fixing doc typo in _dataset.py --- mikeio/dataset/_dataset.py | 7 ++++--- mikeio/eum/_eum.py | 2 +- tests/test_dfs0.py | 30 +++++++++++++++++++++++++----- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/mikeio/dataset/_dataset.py b/mikeio/dataset/_dataset.py index 93f3c16d5..4296e41cc 100644 --- a/mikeio/dataset/_dataset.py +++ b/mikeio/dataset/_dataset.py @@ -2047,15 +2047,16 @@ def _parse_items( elif isinstance(items, ItemInfo): eum_type = items.type eum_unit = items.unit - item_list = [ItemInfo(name, eum_type, eum_unit) for name in column_names] + eum_data_value_type = items.data_value_type + item_list = [ItemInfo(name, eum_type, eum_unit, eum_data_value_type) for name in column_names] elif isinstance(items, Mapping): item_list = [ - ItemInfo(name, items[name].type, items[name].unit) for name in column_names + ItemInfo(name, items[name].type, items[name].unit, items[name].data_value_type) for name in column_names ] elif isinstance(items, Sequence): item_list = [ - ItemInfo(col, item.type, item.unit) + ItemInfo(col, item.type, item.unit, item.data_value_type) for col, item in zip(column_names, items) ] else: diff --git a/mikeio/eum/_eum.py b/mikeio/eum/_eum.py index 6116410ab..eb510db8f 100644 --- a/mikeio/eum/_eum.py +++ b/mikeio/eum/_eum.py @@ -1432,7 +1432,7 @@ def __init__( itemtype: EUMType | EUMUnit | None = None, unit: EUMUnit | None = None, data_value_type: Literal[ - "Instantaneous", "Accumulated", "StepAccumulated", "MeanStepBackWard" + "Instantaneous", "Accumulated", "StepAccumulated", "MeanStepBackward" ] = "Instantaneous", ) -> None: diff --git a/tests/test_dfs0.py b/tests/test_dfs0.py index d2b39f161..387320bb1 100644 --- a/tests/test_dfs0.py +++ b/tests/test_dfs0.py @@ -3,6 +3,7 @@ import pandas as pd import mikeio from mikeio import Dfs0, EUMType, EUMUnit, ItemInfo +from mikecore.DfsFile import DataValueType import pytest @@ -274,23 +275,31 @@ def test_from_pandas_mapping_eum_types() -> None: time = pd.DatetimeIndex(["2001-01-01", "2001-01-01 01:00", "2001-01-01 01:10"]) df = pd.DataFrame( - {"flow": np.array([1, np.nan, 2]), "level": np.array([2, 3.0, -1.3])} + {"flow": np.array([1, np.nan, 2]), "rain": np.array([2, 3.0, -1.3])} ) df.index = time dfr = df.resample("5min").mean().fillna(0.0) # .interpolate() + item_with_dvt = mikeio.ItemInfo( + itemtype=mikeio.EUMType.Rainfall, + unit=mikeio.EUMUnit.centimeter, + data_value_type="StepAccumulated", + ) ds = mikeio.from_pandas( dfr, items={ "flow": mikeio.ItemInfo(itemtype=mikeio.EUMType.Discharge), - "level": mikeio.ItemInfo(itemtype=mikeio.EUMType.Water_Level), + "rain": item_with_dvt, }, ) + item_with_dvt.name = "rain" assert ds.n_timesteps == 15 assert ds[0].type == mikeio.EUMType.Discharge - assert ds[1].type == mikeio.EUMType.Water_Level + assert ds[1].type == mikeio.EUMType.Rainfall + assert ds[1].item is not item_with_dvt + assert ds[1].item == item_with_dvt assert len(ds) == 2 assert ds.end_time == dfr.index[-1] assert ds.is_equidistant @@ -304,10 +313,14 @@ def test_from_pandas_same_eum_type() -> None: index=pd.date_range("2001-01-01", periods=3, freq="H"), ) - ds = mikeio.from_pandas(df, items=ItemInfo(EUMType.Water_Level)) + ds = mikeio.from_pandas( + df, + items=ItemInfo(EUMType.Water_Level, data_value_type=DataValueType.Accumulated), + ) assert ds.n_timesteps == 3 assert ds[0].type == EUMType.Water_Level + assert ds[0].item.data_value_type == DataValueType.Accumulated assert ds["station_b"].item.name == "station_b" @@ -325,13 +338,20 @@ def test_from_pandas_sequence_eum_types() -> None: dfr, items=[ mikeio.ItemInfo("Ignored", itemtype=mikeio.EUMType.Discharge), - mikeio.ItemInfo("Also Ignored", itemtype=mikeio.EUMType.Water_Level), + mikeio.ItemInfo( + "Also Ignored", + itemtype=mikeio.EUMType.Water_Level, + unit=mikeio.EUMUnit.millimeter, + data_value_type=DataValueType.Accumulated, + ), ], ) assert ds.n_timesteps == 15 assert ds[0].type == mikeio.EUMType.Discharge assert ds[1].type == mikeio.EUMType.Water_Level + assert ds[1].item.unit == mikeio.EUMUnit.millimeter + assert ds[1].item.data_value_type == DataValueType.Accumulated assert ds["level"].item.name == "level"