From 31498cefc23ca249fea0c28d831ce0c58902bf14 Mon Sep 17 00:00:00 2001 From: Fridolin Glatter Date: Thu, 3 Oct 2024 14:13:49 +0200 Subject: [PATCH] Set new variable.data index only for non-scalars --- ixmp4/data/db/optimization/variable/repository.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) 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()