diff --git a/ixmp4/data/db/optimization/variable/repository.py b/ixmp4/data/db/optimization/variable/repository.py index 73f88aa9..8f2fe272 100644 --- a/ixmp4/data/db/optimization/variable/repository.py +++ b/ixmp4/data/db/optimization/variable/repository.py @@ -180,13 +180,18 @@ def add_data(self, variable_id: int, data: dict[str, Any] | pd.DataFrame) -> Non f"{', '.join(missing_columns)}!" ) + # TODO Somehow, this got to main without the if index_list checks + # -> Do we need/have a test for add_data() to a scalar variable? index_list = [column.name for column in variable.columns] existing_data = pd.DataFrame(variable.data) - if not existing_data.empty: - existing_data.set_index(index_list, inplace=True) - variable.data = ( - data.set_index(index_list).combine_first(existing_data).reset_index() - ).to_dict(orient="list") + if index_list: + data = data.set_index(index_list) + if not existing_data.empty: + existing_data.set_index(index_list, inplace=True) + data = data.combine_first(existing_data) + if index_list: + data = data.reset_index() + variable.data = data.to_dict(orient="list") self.session.commit()