diff --git a/pymrio/core/mriosystem.py b/pymrio/core/mriosystem.py index 3a76b063..aa57ed3b 100644 --- a/pymrio/core/mriosystem.py +++ b/pymrio/core/mriosystem.py @@ -2040,19 +2040,38 @@ def convert(self, df_map, extension_name, raise ValueError( f"Unit in extension does not match the unit in mapping for row {row}" ) + + new_extension = Extension(name=extension_name) + if unit_column_new: ignore_columns.append(unit_column_new) - - - # for df in self.get_DataFrame: - # CONT: - # - # 1) run with one and check output - # 2) check unit aggregation if unit_column_new is not given - # - unit aggregate with str join of unique entries - # 3) make dict and build new extension - # # run convert of all dataframe and build new extension - # + + for df_name, df in zip(self.get_DataFrame(data=False, with_unit=False), + self.get_DataFrame(data=True, with_unit=False)): + setattr(new_extension, df_name, ioutil.convert( + df_orig=df, + df_map=df_map, + agg_func=agg_func, + drop_not_bridged_index=drop_not_bridged_index, + ignore_columns=ignore_columns)) + + if unit_column_new: + unit = pd.DataFrame( + columns=["unit"], + index=new_extension.get_rows()) + bridge_columns = [col for col in df_map.columns if "__" in col] + unique_new_index = ( + df_map.loc[:, bridge_columns].drop_duplicates().set_index(bridge_columns).index + ) + unique_new_index.names = [col.split("__")[0] for col in bridge_columns] + + unit.unit = df_map.set_index(bridge_columns).loc[unique_new_index].loc[:, unit_column_new] + new_extension.unit = unit + else: + new_extension.unit = None + + return new_extension + class IOSystem(BaseSystem): """Class containing a whole EE MRIO System diff --git a/pymrio/tools/ioutil.py b/pymrio/tools/ioutil.py index 4ffe4e17..04a9628f 100644 --- a/pymrio/tools/ioutil.py +++ b/pymrio/tools/ioutil.py @@ -1136,7 +1136,6 @@ def convert(df_orig, stacked_columns = [] orig_column_index = df_orig.columns for col in df_map.columns: - # TODO: other names for unit should be allowed if col in (["factor"] + ignore_columns): continue if col not in df_orig.index.names: diff --git a/tests/test_core.py b/tests/test_core.py index 759e6e04..f196bbfa 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -565,7 +565,20 @@ def test_extension_convert(fix_testmrio): pass -def test_extension_convert_test_unit(fix_testmrio): + +def test_extension_convert_test_conversion(fix_testmrio): + df_map1 = pd.DataFrame( + columns=["stressor", "compartment", "total__stressor", "factor", "unit_orig", "unit_new"], + data=[ + ["emis.*", "air|water", "total_regex", 1000, "kg", "t"], + ], + ) + + x = fix_testmrio.testmrio.emissions.convert(df_map1, extension_name="emissions_new") + # CONT: write test for conversion, including va, different units, etc. + + +def test_extension_convert_test_unit_fail(fix_testmrio): df_fail1 = pd.DataFrame( columns=["stressor", "compartment", "total__stressor", "factor", "unit_orig", "unit_new"],