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

adding a check (and unit test) that set_meta() must receive name #111

Merged
merged 2 commits into from
Oct 16, 2018
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
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# Next Release

- (#111)[https://github.com/IAMconsortium/pyam/pull/111] extends `set_meta()` such that it requires a name (not None)
- (#104)[https://github.com/IAMconsortium/pyam/pull/104] fixes a bug with timeseries utils functions, ensures that years are cast as integers
- (#101)[https://github.com/IAMconsortium/pyam/pull/101] add function `cross_threshold()` to determine years where a timeseries crosses a given threshold
- (#98)[https://github.com/IAMconsortium/pyam/pull/98] add a module to compute and format summary statistics for timeseries data (wrapper for `pd.describe()`
Expand Down
5 changes: 3 additions & 2 deletions pyam/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,12 @@ def set_meta(self, meta, name=None, index=None):
column to be added to metadata
(by `['model', 'scenario']` index if possible)
name: str, optional
meta column name (defaults to meta pd.Series.name)
meta column name (defaults to meta pd.Series.name);
either a meta.name or the name kwarg must be defined
index: pyam.IamDataFrame, pd.DataFrame or pd.MultiIndex, optional
index to be used for setting meta column (`['model', 'scenario']`)
"""
if not name and not hasattr(meta, 'name'):
if (name or (hasattr(meta, 'name') and meta.name)) in [None, False]:
raise ValueError('Must pass a name or use a named pd.Series')

# check if meta has a valid index and use it for further workflow
Expand Down
8 changes: 8 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@ def test_interpolate(test_df):
assert not test_df.filter(**variable).data.duplicated().any()


def test_set_meta_no_name(meta_df):
idx = pd.MultiIndex(levels=[['a_scenario'], ['a_model'], ['a_region']],
labels=[[0], [0], [0]],
names=['scenario', 'model', 'region'])
s = pd.Series(data=[0.3], index=idx)
pytest.raises(ValueError, meta_df.set_meta, s)


def test_set_meta_as_named_series(meta_df):
idx = pd.MultiIndex(levels=[['a_scenario'], ['a_model'], ['a_region']],
labels=[[0], [0], [0]],
Expand Down