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

Add fast route for operations with non-SI units #907

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
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