Skip to content

Commit

Permalink
Set new variable.data index only for non-scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
glatterf42 committed Oct 3, 2024
1 parent a367338 commit 31498ce
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions ixmp4/data/db/optimization/variable/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 31498ce

Please sign in to comment.