Skip to content

Commit 1ca3af2

Browse files
Improve comparison function (#1310)
* Extend data out to 2030-31 * Fix bug in comparison function * Fix bug in comparison function
1 parent 67e4096 commit 1ca3af2

3 files changed

Lines changed: 44 additions & 5 deletions

File tree

changelog_entry.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- bump: patch
2+
changes:
3+
fixed:
4+
- Minor bug in comparison function.

policyengine_uk/microsimulation.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,26 @@ def calculate_dataframe(
8080
return values
8181
weights = self.get_weights(variable_names[0], period, map_to=map_to)
8282
return MicroDataFrame(values, weights=weights)
83+
84+
def compare(
85+
self,
86+
other: "Simulation",
87+
variables: list[str] = None,
88+
period: str = None,
89+
change_only: bool = False,
90+
):
91+
"""Compare two simulations for a specific variable list.
92+
93+
Args:
94+
other: Another Simulation instance to compare against
95+
variables: List of variable names to compare. If None, compares all variables.
96+
97+
Returns:
98+
DataFrame with comparison results
99+
"""
100+
df = super().compare(
101+
other, variables=variables, period=period, change_only=change_only
102+
)
103+
return MicroDataFrame(
104+
df, weights=self.get_weights(variables[0], period)
105+
)

policyengine_uk/simulation.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ def compare(
539539
other: "Simulation",
540540
variables: list[str] = None,
541541
period: str = None,
542+
change_only: bool = False,
542543
):
543544
"""Compare two simulations for a specific variable list.
544545
@@ -569,14 +570,12 @@ def compare(
569570
for variable in variables:
570571
self_col = f"{variable}_self"
571572
other_col = f"{variable}_other"
572-
if pd.api.types.is_numeric_dtype(
573-
df_combined[self_col]
574-
) and pd.api.types.is_numeric_dtype(df_combined[other_col]):
573+
try:
575574
df_combined[f"{variable}_change"] = (
576575
df_combined[other_col] - df_combined[self_col]
577576
)
578-
else:
579-
# True if different, False if same
577+
except:
578+
# For other data types, use XOR
580579
df_combined[f"{variable}_change"] = (
581580
df_combined[other_col] != df_combined[self_col]
582581
)
@@ -590,4 +589,17 @@ def compare(
590589
f"{variable}_change",
591590
]
592591
)
592+
593+
if change_only:
594+
variables_to_include = []
595+
for variable in variables:
596+
if df_combined[f"{variable}_change"].mean() > 0:
597+
variables_to_include.extend(
598+
[
599+
f"{variable}_self",
600+
f"{variable}_other",
601+
f"{variable}_change",
602+
]
603+
)
604+
columns = variables_to_include
593605
return df_combined[columns]

0 commit comments

Comments
 (0)