Skip to content

Commit

Permalink
correct discount factor for multiple regions
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorb1 committed Nov 15, 2024
1 parent 3e1f05d commit d987e5d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/otoole/results/result_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ def discount_factor(
)

if regions and years:
discount_rate["YEAR"] = [years]
discount_rate["YEAR"] = [years] * len(discount_rate)
discount_factor = discount_rate.explode("YEAR").reset_index(level="REGION")
discount_factor["YEAR"] = discount_factor["YEAR"].astype("int64")
discount_factor["NUM"] = discount_factor["YEAR"] - discount_factor["YEAR"].min()
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ def discount_rate():
return df


@fixture
def discount_rate_multiple_regions():
df = pd.DataFrame(
data=[
["SIMPLICITY", 0.05],
["DUMMY", 0.05],
],
columns=["REGION", "VALUE"],
).set_index(["REGION"])
return df


@fixture
def discount_rate_idv():
df = pd.DataFrame(
Expand Down
33 changes: 33 additions & 0 deletions tests/results/test_results_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def region():
return pd.DataFrame(data=["SIMPLICITY"], columns=["VALUE"])


@fixture
def region_multiple():
return pd.DataFrame(data=["SIMPLICITY", "DUMMY"], columns=["VALUE"])


@fixture
def storage():
return pd.DataFrame(data=["DAM"], columns=["VALUE"])
Expand Down Expand Up @@ -1485,6 +1490,34 @@ def test_df_empty_discount_rate(self, region, year, discount_rate_empty):
with raises(ValueError):
discount_factor(regions, years, discount_rate_empty, 1.0)

def test_df_two_regions(self, region_multiple, year, discount_rate_multiple):

regions = region_multiple["VALUE"].to_list()
years = year["VALUE"].to_list()
actual = discount_factor(regions, years, discount_rate_multiple, 0.0)

expected = pd.DataFrame(
data=[
["SIMPLICITY", 2014, 1],
["SIMPLICITY", 2015, 1.05],
["SIMPLICITY", 2016, 1.1025],
["SIMPLICITY", 2017, 1.157625],
["SIMPLICITY", 2018, 1.21550625],
["SIMPLICITY", 2019, 1.276281562],
["SIMPLICITY", 2020, 1.340095640],
["DUMMY", 2014, 1],
["DUMMY", 2015, 1.05],
["DUMMY", 2016, 1.1025],
["DUMMY", 2017, 1.157625],
["DUMMY", 2018, 1.21550625],
["DUMMY", 2019, 1.276281562],
["DUMMY", 2020, 1.340095640],
],
columns=["REGION", "YEAR", "VALUE"],
).set_index(["REGION", "YEAR"])

assert_frame_equal(actual, expected)


class TestDiscountFactorStorage:
def test_dfs_start(self, region, year, discount_rate_storage):
Expand Down

0 comments on commit d987e5d

Please sign in to comment.