From d314a5dd2a6baab1e05cbc402f339a6115e43a35 Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 16 Oct 2018 11:14:59 +0200 Subject: [PATCH] adding a check (and unit test) that `set_meta()` must receive name (#111) * adding a check (and unit test) that `set_meta()` must receive name * add to release notes --- RELEASE_NOTES.md | 1 + pyam/core.py | 5 +++-- tests/test_core.py | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index ea918c0d3..c6eb02784 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -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()` diff --git a/pyam/core.py b/pyam/core.py index 2ec721d2d..ce4478417 100644 --- a/pyam/core.py +++ b/pyam/core.py @@ -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 diff --git a/tests/test_core.py b/tests/test_core.py index b1ad318a1..a839a97a4 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -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]],