Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a corner case in region-aggregation with missing data #813

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Next Release

- [#813](https://github.com/IAMconsortium/pyam/pull/813) Fix a corner case in region-aggregation with missing data

# Release v2.1.0

## Highlights
Expand Down
2 changes: 1 addition & 1 deletion pyam/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _aggregate_region(
_df.index = replace_index_values(_df.index, "variable", mapping)
_data = _data.add(_group_and_agg(_df, "region"), fill_value=0)

return _data
return _data.dropna()


def _aggregate_time(df, variable, column, value, components, method="sum"):
Expand Down
48 changes: 28 additions & 20 deletions tests/test_feature_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,46 +344,54 @@ def test_aggregate_region_with_negative_weights(simple_df, caplog):


@pytest.mark.parametrize(
"filter_arg,log_message",
"filter_arg, log_message, values",
(
(dict(year=2010), ""),
(dict(), "model_a scen_a reg_b 2005\n1 "),
(
dict(region="reg_b"),
"0 model_a scen_a reg_b 2005\n1 model_a scen_a reg_b 2010",
(1, 30),
),
(
dict(year=2010, region="reg_b"),
"0 model_a scen_a reg_b 2010",
(4, 30),
),
(
dict(year=2010),
"0 model_a scen_a reg_a 2010\n1 model_a scen_a reg_b 2010",
4,
),
),
)
def test_aggregate_region_with_weights_inconsistent_index(
simple_df, caplog, filter_arg, log_message
simple_df, caplog, filter_arg, log_message, values
):
# carbon price shouldn't be summed but be weighted by emissions
v = "Price|Carbon"
w = "Emissions|CO2"

log_message = "\n0 " + log_message + "model_a scen_a reg_b 2010"
if simple_df.time_domain == "datetime":
time_col = " time"
log_message = log_message.replace(" 2005", "2005-06-17").replace(
" 2010", "2010-07-21"
)
if simple_df.time_domain == "year":
log_message = "model scenario region year\n" + log_message
else:
time_col = "year"
log_message = "model scenario region time\n" + (
log_message.replace(" 2005", "2005-06-17").replace(" 2010", "2010-07-21")
)

# missing weight row raises an error
_df = simple_df.filter(variable=w, region="reg_b", keep=False, **filter_arg)
match = r"Missing weights for the following data.*\n.*" + re.escape(log_message)
with pytest.raises(ValueError, match=match):
_df = simple_df.filter(variable=w, keep=False, **filter_arg)
_df.aggregate_region(v, weight=w)

# missing data row prints a warning (data-index is a subset of weight-index)
exp = simple_df.filter(variable=v, region="World")
if not filter_arg:
exp._data[0] = 1.0
exp._data[1] = 30.0
_df = simple_df.filter(variable=v, region="reg_b", keep=False, **filter_arg)
if filter_arg == dict(year=2010):
exp.filter(year=2005, inplace=True) # in this case, there are no 2010 data
exp._data.iloc[:] = values
_df = simple_df.filter(variable=v, keep=False, **filter_arg)
assert_iamframe_equal(_df.aggregate_region(v, weight=w), exp)

msg = (
"Ignoring weights for the following missing data rows:\n"
f" model scenario region {time_col}" + log_message
)
msg = "Ignoring weights for the following missing data rows:\n " + log_message

idx = caplog.messages.index(msg)
assert caplog.records[idx].levelname == "WARNING"
Expand Down
Loading