Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Df load meta #679

Merged
merged 8 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Next Release

## Individual updates

- [#679](https://github.com/IAMconsortium/pyam/pull/679) `set_meta()` now supports pandas.DataFrame as an argument
- [#674](https://github.com/IAMconsortium/pyam/pull/674) Support filtering data by model-scenario pairs with the `index` argument to `filter()` and `slice()`

# Release v1.5.0
Expand Down
15 changes: 12 additions & 3 deletions pyam/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ def _init(self, data, meta=None, index=DEFAULT_META_INDEX, **kwargs):

# if given explicitly, merge meta dataframe after downselecting
if meta is not None:
meta = meta.loc[self.meta.index.intersection(meta.index)]
self.meta = merge_meta(meta, self.meta, ignore_conflict=True)
self.set_meta(meta)

# if initializing from xlsx, try to load `meta` table from file
if meta_sheet and isinstance(data, Path) and data.suffix == ".xlsx":
Expand Down Expand Up @@ -808,7 +807,7 @@ def set_meta(self, meta, name=None, index=None):

Parameters
----------
meta : pandas.Series, list, int, float or str
meta : pandas.DataFrame, pandas.Series, list, int, float or str
column to be added to 'meta'
(by `['model', 'scenario']` index if possible)
name : str, optional
Expand All @@ -817,6 +816,16 @@ def set_meta(self, meta, name=None, index=None):
index : IamDataFrame, pandas.DataFrame or pandas.MultiIndex, optional
index to be used for setting meta column (`['model', 'scenario']`)
"""
if isinstance(meta, pd.DataFrame):
if meta.index.names != self.meta.index.names:
# catch Model, Scenario instead of model, scenario
meta = meta.rename(
columns={i.capitalize(): i for i in META_IDX}
).set_index(self.meta.index.names)
meta = meta.loc[self.meta.index.intersection(meta.index)]
self.meta = merge_meta(meta, self.meta, ignore_conflict=True)
return

# check that name is valid and doesn't conflict with data columns
if (name or (hasattr(meta, "name") and meta.name)) in [None, False]:
raise ValueError("Must pass a name or use a named pd.Series")
Expand Down