IAMconsortium / pyam

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

Proposal: filter by model-scenario combinations #673

Closed gidden closed 2 years ago

gidden commented 2 years ago

Through our discussion on slack, we identified a desire for a feature to allow for filtering unique model-scenario combinations. Here is one proposed implementation:

# data is a pyam.IamDataFrame
# ms_df if a dataframe with columns model and scenario

def subset_model_scenario(data, ms_df):
    idxs = [data.slice(model=m, scenario=s) for m, s in list(ms_df.itertuples(index=False, name=None))]
    idx = functools.reduce(operator.or_, idxs, False)
    return data[idx]

This utilizes the new IamSlice feature, constructing a list of slices of size N where N is the number of unique combinations and then using logical_or across all entries.

Thoughts @coroa @znicholls @danielhuppmann (and others)?

znicholls commented 2 years ago

lgtm but probably @coroa is best to advise given I have no idea about slice...

danielhuppmann commented 2 years ago

I like the idea, but I think it would be more intuitive to add index as a keyword argument to filter() instead of adding a new method.

The value for the index argument should accept both a list of tuples or a pandas.MultiIndex.

gidden commented 2 years ago

Collecting our discussions on slack, we thought

Please feel free to add if I missed anything from the discussions.

coroa commented 2 years ago

Argument name for the slice method is the same as for the filter method on IamDataFrame. And i agree it makes sense to call it index.