IAMconsortium / pyam

Analysis & visualization of energy & climate scenarios
https://pyam-iamc.readthedocs.io/
Apache License 2.0
226 stars 118 forks source link

improve error message in `set_meta_from_data()` #660

Open gidden opened 2 years ago

gidden commented 2 years ago

A naive user (read: me) can obtain an obscure error message if set_meta_from_data() is used in an unintended way.

Take for example the regional SR15 dataframe. The following code will elicit the following error message:

df.set_meta_from_data(
    name='CO2 Emissions in 2050',
    variable='Emissions|CO2',
    year=2050,
)
...
File c:\users\gidden\work\iiasa\pyam\pyam\core.py:890, in IamDataFrame.set_meta_from_data(self, name, method, column, **kwargs)
    888 else:
    889     meta = _data.groupby(META_IDX)[column].apply(method)
--> 890 self.set_meta(meta, name)

File c:\users\gidden\work\iiasa\pyam\pyam\core.py:853, in IamDataFrame.set_meta(self, meta, name, index)
    851 # raise error if index is not unique
    852 if index.duplicated().any():
--> 853     raise ValueError("Non-unique ['model', 'scenario'] index!")
    855 # create pd.Series from meta, index and name if provided
    856 meta = pd.Series(data=meta, index=index, name=name)

ValueError: Non-unique ['model', 'scenario'] index!

The reason for this is that there is regional data as well, so indeed there are non-unique model/scenario indicies (by factor 6 in this case for 5 regions + world).

We could, e.g., tell a user that they have multiple entries in one of the non-sliced columns to improve this.