Skip to content

Commit eec22b7

Browse files
Add function to apply custom growthfactors
1 parent 4b03166 commit eec22b7

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ format:
1414
test:
1515
policyengine-core test policyengine_uk/tests/policy -c policyengine_uk
1616
pytest policyengine_uk/tests/ -v
17+
python policyengine_uk/data/economic_assumptions.py
1718

1819
documentation:
1920
jb clean docs/book

policyengine_uk/data/dataset_schema.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ def __init__(
2828
self.benunit = benunit
2929
self.household = household
3030

31-
self.data_format = "time_period_arrays"
31+
self.data_format = "arrays"
3232
self.time_period = fiscal_year
33+
self.tables = (self.person, self.benunit, self.household)
3334

3435
def save(self, file_path: str):
3536
with pd.HDFStore(file_path) as f:
@@ -43,9 +44,7 @@ def load(self):
4344
data = {}
4445
for df in (self.person, self.benunit, self.household):
4546
for col in df.columns:
46-
data[col] = {
47-
self.time_period: df[col].values,
48-
}
47+
data[col] = df[col].values
4948

5049
return data
5150

policyengine_uk/data/economic_assumptions.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pandas as pd
22
from pathlib import Path
3+
from .dataset_schema import UKDataset
34

45
START_YEAR = 2020
56
END_YEAR = 2034
@@ -39,8 +40,26 @@ def create_policyengine_uprating_factors_table():
3940
df_growth[year] = round(df_growth[year] / df_growth[year - 1] - 1, 3)
4041
df_growth[START_YEAR] = 0
4142

42-
df_growth.to_csv(Path(__file__).parent / "uprating_growth_factors.csv")
43-
return df
43+
file_path = Path(__file__).parent / "uprating_growth_factors.csv"
44+
df_growth.to_csv(file_path)
45+
return pd.read_csv(file_path)
46+
47+
def apply_growth_factors(dataset: UKDataset, growth_factors: pd.DataFrame, start_year: int, end_year: int):
48+
dataset = dataset.copy()
49+
for i in range(len(growth_factors)):
50+
index = 1
51+
variable = growth_factors["Variable"].values[i]
52+
for year in range(start_year, end_year + 1):
53+
growth_factor = growth_factors[str(year)].values[i]
54+
index *= 1 + growth_factor
55+
56+
for table in dataset.tables:
57+
if variable in table.columns:
58+
table[variable] *= index
59+
60+
return dataset
61+
62+
BASELINE_GROWFACTORS = create_policyengine_uprating_factors_table()
4463

4564

4665
if __name__ == "__main__":

0 commit comments

Comments
 (0)