Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing copying of data_value_type in from_pandas #714

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions mikeio/dataset/_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion mikeio/eum/_eum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
30 changes: 25 additions & 5 deletions tests/test_dfs0.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pandas as pd
import mikeio
from mikeio import Dfs0, EUMType, EUMUnit, ItemInfo
from mikecore.DfsFile import DataValueType


import pytest
Expand Down Expand Up @@ -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
Expand All @@ -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"


Expand All @@ -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"


Expand Down
Loading