Skip to content

Commit

Permalink
add errors and explicit messages
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann committed Oct 21, 2020
1 parent 07529a6 commit 7923b57
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 6 additions & 3 deletions pyam/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
META_IDX,
YEAR_IDX,
IAMC_IDX,
SORT_IDX
SORT_IDX,
ILLEGAL_COLS
)
from pyam.read_ixmp import read_ix
from pyam.timeseries import fill_series
Expand Down Expand Up @@ -618,8 +619,10 @@ def set_meta(self, meta, name=None, index=None):
if (name or (hasattr(meta, 'name') and meta.name)) in [None, False]:
raise ValueError('Must pass a name or use a named pd.Series')
name = name or meta.name
if name in self.data.columns:
raise ValueError(f'A column `{name}` already exists in `data`!')
if name in self._data.index.names:
raise ValueError(f'Column {name} already exists in `data`!')
if name in ILLEGAL_COLS:
raise ValueError(f'Name {name} is illegal for meta indicators!')

# check if meta has a valid index and use it for further workflow
if hasattr(meta, 'index') and hasattr(meta.index, 'names') \
Expand Down
13 changes: 12 additions & 1 deletion pyam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
SORT_IDX = ['model', 'scenario', 'variable', 'year', 'region']
LONG_IDX = IAMC_IDX + ['year']

# illegal terms for data/meta column names to prevent attribute conflicts
ILLEGAL_COLS = ['data', 'meta']

# dictionary to translate column count to Excel column names
NUMERIC_TO_STR = dict(zip(range(0, 702),
[i for i in string.ascii_uppercase]
Expand Down Expand Up @@ -133,7 +136,7 @@ def format_data(df, **kwargs):
df.name = df.name or 'value'
df = df.to_frame()

# Check for R-style year columns, converting where necessary
# check for R-style year columns, converting where necessary
def convert_r_columns(c):
try:
first = c[0]
Expand Down Expand Up @@ -203,6 +206,14 @@ def convert_r_columns(c):
if not list(df.index.names) == [None]:
df.reset_index(inplace=True)

# check that there is no column in the timeseries data with reserved names
conflict_cols = [i for i in df.columns if i in ILLEGAL_COLS]
if conflict_cols:
msg = f'Column name {conflict_cols} is illegal for timeseries data.\n'
_args = ', '.join([f"{i}_alt='{i}'" for i in conflict_cols])
msg += f'Use `IamDataFrame(..., {_args})` to rename at initalization.'
raise ValueError(msg)

# format columns to lower-case and check that all required columns exist
if not set(IAMC_IDX).issubset(set(df.columns)):
missing = list(set(IAMC_IDX) - set(df.columns))
Expand Down

0 comments on commit 7923b57

Please sign in to comment.