From 00b36d9f5b514a526dd37c41d38440a9f8bf3a56 Mon Sep 17 00:00:00 2001 From: Konstantin Stadler Date: Mon, 15 Jul 2024 16:43:39 +0200 Subject: [PATCH] made factor column optional --- pymrio/tools/ioutil.py | 7 ++++++- tests/test_util.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pymrio/tools/ioutil.py b/pymrio/tools/ioutil.py index 60fa0c7..3c57384 100644 --- a/pymrio/tools/ioutil.py +++ b/pymrio/tools/ioutil.py @@ -1021,6 +1021,8 @@ def convert(df_orig, df_map, agg_func="sum", drop_not_bridged=True): stressor ... original index name compartment ... original index name factor ... the factor for multiplication/characterization + If no factor is given, the factor is assumed to be 1. + This can be used, to simplify renaming/aggregation mappings. impact__stressor ... the new index name, replacing the previous index name "stressor". Thus here "stressor" will be renamed to "impact", and the row index @@ -1094,7 +1096,10 @@ def convert(df_orig, df_map, agg_func="sum", drop_not_bridged=True): # the loop for getting all (potential regex) matches and multiplication for row in df_cur_map.iterrows(): matched_entries = index_fullmatch(df_ix=df_orig, **row[1].to_dict()) - mul_entries = matched_entries * row[1].factor + try: + mul_entries = matched_entries * row[1].factor + except AttributeError: + mul_entries = matched_entries collector.append(mul_entries) df_collected = pd.concat(collector, axis=0) diff --git a/tests/test_util.py b/tests/test_util.py index 0bc69ee..1dc4cf9 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -419,6 +419,24 @@ def test_convert_rename(): ) + + # without factor should give the same result + rename_bridge_simple_wo_factor = pd.DataFrame( + columns=["em_type", "stressor__em_type"], + data=[ + ["em1", "emission-1"], + ["em2", "emission2"], + ["emA", "emission A"], + ], + ) + + char_res_keep_comp_wo_factor = convert(to_char, rename_bridge_simple_wo_factor, drop_not_bridged=False) + + pdt.assert_frame_equal( + char_res_keep_comp_wo_factor, + char_res_keep_comp) + + def test_convert_characterize(): """Testing the characterization of one table"""