Skip to content
Draft
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

- [#907](https://github.com/IAMconsortium/pyam/pull/907) Add fast route for operations with non-SI units

# Release v3.1.0

## Highlights
Expand Down
2 changes: 1 addition & 1 deletion pyam/_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _op_data(df, name, method, axis, fillna=None, args=(), ignore_units=False, *
and fillna is None
and len(_unit_kwds["a"]) == 1
and len(_unit_kwds["b"]) == 1
and registry.Unit(_unit_kwds["a"][0]) == registry.Unit(_unit_kwds["b"][0])
and _unit_kwds["a"][0] == _unit_kwds["b"][0]
):
# activate ignore-units feature
ignore_units = _unit_kwds["a"][0] if method in [add, subtract] else ""
Expand Down
46 changes: 46 additions & 0 deletions tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ def test_add_variable(test_df_year, arg, df_func, fillna, ignore_units, append):
assert_iamframe_equal(exp, obs)


@pytest.mark.parametrize("append", (False, True))
def test_add_scenario_non_si_unit(test_df_year, append):
df = test_df_year.rename(unit={"EJ/yr": "foo"})
exp = df_ops_variable(operator.add, "Sum", unit="foo", meta=test_df_year.meta)
args = ("Primary Energy", "Primary Energy|Coal", "Sum")

if append:
obs = df.copy()
obs.add(*args, append=True)
assert_iamframe_equal(df.append(exp), obs)
else:
obs = df.add(*args)
assert_iamframe_equal(exp, obs)


@pytest.mark.parametrize("append", (False, True))
def test_add_scenario(test_df_year, append):
"""Verify that in-dataframe addition works on a custom axis (`scenario`)"""
Expand Down Expand Up @@ -159,6 +174,22 @@ def test_subtract_variable(test_df_year, arg, df_func, fillna, append, ignore_un
assert_iamframe_equal(exp, test_df_year.subtract(*args, **kwds))


@pytest.mark.parametrize("append", (False, True))
def test_subtract_scenario_non_si_unit_unit(test_df_year, append):
df = test_df_year.rename(unit={"EJ/yr": "foo"})
exp = df_ops_variable(operator.sub, "Diff", unit="foo", meta=test_df_year.meta)
args = ("Primary Energy", "Primary Energy|Coal", "Diff")

if append:
obs = df.copy()
obs.subtract(*args, append=True)
assert_iamframe_equal(df.append(exp), obs)
else:
obs = df.subtract(*args)
assert_iamframe_equal(exp, obs)



@pytest.mark.parametrize("append", (False, True))
def test_subtract_scenario(test_df_year, append):
"""Verify that in-dataframe subtraction works on a custom axis (`scenario`)"""
Expand Down Expand Up @@ -288,6 +319,21 @@ def test_divide_variable(test_df_year, arg, df_func, fillna, append, ignore_unit
assert_iamframe_equal(exp, test_df_year.divide(*args, **kwds))


@pytest.mark.parametrize("append", (False, True))
def test_divide_variable_non_si_unit_unit(test_df_year, append):
df = test_df_year.rename(unit={"EJ/yr": "foo"})
exp = df_ops_variable(operator.truediv, "Ratio", unit="", meta=test_df_year.meta)
args = ("Primary Energy", "Primary Energy|Coal", "Ratio")

if append:
obs = df.copy()
obs.divide(*args, append=True)
assert_iamframe_equal(df.append(exp), obs)
else:
obs = df.divide(*args)
assert_iamframe_equal(exp, obs)


@pytest.mark.parametrize("append", (False, True))
def test_divide_scenario(test_df_year, append):
"""Verify that in-dataframe addition works on a custom axis (`scenario`)"""
Expand Down
Loading