Skip to content

Commit

Permalink
Add helper for multi-period results processing
Browse files Browse the repository at this point in the history
  • Loading branch information
SabineHaas committed Nov 18, 2024
1 parent 0934664 commit d7005e2
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/oemof/tabular/postprocessing/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,36 @@ def group_by_element(scalars):
elements[name] = df

return elements


def get_value_by_year(data):
"""
Combines data of all columns to one column and adds column "year".
Value column is named "var_value".
Parameters
----------
data: pd.DataFrame
Transposed result of core.Calculation.depencency().
Index contains years, columns contain the corresponding data.
Multi-index columns ["source", "target", "var_name"].
Returns
-------
df : pd.DataFrame
Data in column "var_value", corresponding year in column "year". Index
is generated from the columns names of `data` (MultiIndex).
"""
# drop irrelevant years
df = data.loc[(data != 0).any(axis=1)]
# year as column and use year as identifier to melt df
# --> all values in one column, identifier is year
df["year"] = df.index
df = df.melt(id_vars=[df.columns[-1]], ignore_index=False)
# set multi-index
df.set_index(["source", "target", "var_name"], inplace=True)
# non multi-index columns
df.columns = ["year", "var_value"]
return df

0 comments on commit d7005e2

Please sign in to comment.