Skip to content

Commit

Permalink
initial impl for giving default pyam dataframes
Browse files Browse the repository at this point in the history
  • Loading branch information
gidden committed Feb 12, 2019
1 parent b665608 commit e3426bf
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pyam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,29 @@ def to_int(x, index=False):
return x
else:
return _x


def df_to_pyam(df, **kwargs):
numeric_cols = ['year']
defaults = {
x: x if x not in numeric_cols else 0 for x in pyam.utils.GROUP_IDX
}
defaults.update(kwargs)

def _apply_defaults(df, defaults):
for col, val in defaults.items():
df[col] = val
return df

# maybe only needed if variable is missing?
cols = list(set(df.columns) - set(pyam.utils.LONG_IDX))
idx = list(set(df.columns) & set(pyam.utils.LONG_IDX))
df = df.set_index(idx)
dfs = []
for col in cols:
_df = df[col].to_frame().rename(columns={col: 'value'})
_df['variable'] = col
dfs.append(_df.reset_index())
df = pd.concat(dfs)
df = _apply_defaults(df, defaults)
return pyam.IamDataFrame(df)

0 comments on commit e3426bf

Please sign in to comment.