Skip to content

Commit

Permalink
adding a check (and unit test) that set_meta() must receive name (#111
Browse files Browse the repository at this point in the history
)

* adding a check (and unit test) that `set_meta()` must receive name

* add to release notes
  • Loading branch information
danielhuppmann authored and gidden committed Oct 16, 2018
1 parent 5e34dfc commit d314a5d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
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

0 comments on commit d314a5d

Please sign in to comment.