Skip to content

Commit

Permalink
Bugfix: ResultFrameAggregator parameter override_name not working. (#91)
Browse files Browse the repository at this point in the history
* Add unit test, which shows that ResultFrameAggregator parameter override_name is not working.

* Fix override_name in ResultFrameAggregator

* Update changelog

---------

Co-authored-by: Gediminas Kirsanskas <[email protected]>
Co-authored-by: Ryan Murray <[email protected]>
  • Loading branch information
3 people authored Mar 4, 2024
1 parent d9c397f commit 3f84151
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### Fixed

- Loading MIKE IO 1D together with MIKE+Py
- Fixed override_name parameter that was not working in ResultFrameAggregator

## [0.6] - 2024-02-08

Expand Down
8 changes: 8 additions & 0 deletions mikeio1d/pandas_extension/result_frame_aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ def _finalize_quantity_index(self, quantity_index: pd.Index) -> pd.Index:
continue
quantity_index = quantity_index.droplevel(level)

if self._override_name is not None:
quantity_index = quantity_index.set_levels(
quantity_index.levels[quantity_index.names.index(self._agg_level_name)].map(
lambda _: self._override_name
),
level=self._agg_level_name,
)

quantity_index = quantity_index.map("_".join)

return quantity_index
Expand Down
13 changes: 13 additions & 0 deletions tests/test_result_frame_aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,16 @@ def test_max_aggregation(self, res1d_river_network, column_mode):
assert list(df_agg_std.values) == pytest.approx(
[32.83624888, 1.28741954, 3.25905542, 0.4820685]
)

@pytest.mark.parametrize("column_mode", ["all", "compact"])
def test_lambda_aggregation_override_name(self, res1d_river_network, column_mode):
df_reaches = res1d_river_network.reaches.read(column_mode=column_mode)
rfa = ResultFrameAggregator(lambda x: x.abs().max(), override_name="max")
df_agg = rfa.aggregate(df_reaches)

assert list(df_agg.columns.values) == [
"max_Discharge",
"max_FlowVelocity",
"max_ManningResistanceNumber",
"max_WaterLevel",
]

0 comments on commit 3f84151

Please sign in to comment.