Skip to content

Commit

Permalink
Merge pull request #77 from sedos-project/fix/bug_refactoring_multiin…
Browse files Browse the repository at this point in the history
…dex_in_timeseries

Multiindex/timeindex bug
  • Loading branch information
FelixMau authored Mar 15, 2024
2 parents 1aca1fc + c97fd63 commit 211c1cc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
17 changes: 12 additions & 5 deletions data_adapter_oemof/build_datapackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import warnings
from typing import Optional, Type

import numpy as np
import pandas as pd
import tsam.timeseriesaggregation as tsam
from data_adapter.preprocessing import Adapter
Expand Down Expand Up @@ -479,6 +480,13 @@ def build_datapackage(
DataPackage
"""

def _reduce_lists(x):
"""Unnest list of single tuple or list of single list"""
if isinstance(x[0], (list, tuple)):
x = x.map(lambda x: x[0])
return x

parametrized_elements = {"bus": []}
parametrized_sequences = {}
foreign_keys = {}
Expand All @@ -487,11 +495,10 @@ def build_datapackage(
process_data = adapter.get_process(process_name)
timeseries = process_data.timeseries
if isinstance(timeseries.columns, pd.MultiIndex):
# FIXME: Will Regions be lists of strings or strings?
timeseries.columns = (
timeseries.columns.get_level_values(0)
_reduce_lists(timeseries.columns.get_level_values(0))
+ "_"
+ [x[0] for x in timeseries.columns.get_level_values(1).values]
+ _reduce_lists(timeseries.columns.get_level_values(1))
)
facade_adapter_name: str = process_adapter_map[process_name]
facade_adapter: Type[FacadeAdapter] = FACADE_ADAPTERS[facade_adapter_name]
Expand All @@ -515,7 +522,7 @@ def build_datapackage(
# Fill with all buses occurring, needed for foreign keys as well!
process_busses += list(component_adapter.get_busses().values())

process_busses = list(pd.unique(process_busses))
process_busses = list(np.unique(process_busses))
parametrized_elements["bus"] += process_busses

# getting foreign keys with last component
Expand All @@ -532,7 +539,7 @@ def build_datapackage(
# Create Bus Element from all unique `busses` found in elements
parametrized_elements["bus"] = pd.DataFrame(
{
"name": (names := pd.unique(parametrized_elements["bus"])),
"name": (names := np.unique(parametrized_elements["bus"])),
"type": ["bus" for i in names],
"balanced": [True for i in names],
}
Expand Down
2 changes: 1 addition & 1 deletion tests/_files/build_datapackage_goal/data/elements/bus.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name;type;balanced
ch4;bus;True
electricity;bus;True
heat;bus;True
ch4;bus;True
2 changes: 1 addition & 1 deletion tests/_files/tsam_goal/data/elements/bus.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name;type;balanced
ch4;bus;True
electricity;bus;True
heat;bus;True
ch4;bus;True

0 comments on commit 211c1cc

Please sign in to comment.