diff --git a/backend/Property.py b/backend/Property.py index 2359ea6a..1cebc4a9 100644 --- a/backend/Property.py +++ b/backend/Property.py @@ -1,4 +1,5 @@ from datetime import datetime +from collections import Counter import re import os import pandas as pd @@ -334,8 +335,24 @@ class Property(Definitions): raise ValueError("Property does not contain data") self.construction_age_band = DataProcessor.clean_construction_age_band(self.data["construction-age-band"]) + if self.construction_age_band in self.DATA_ANOMALY_MATCHES: + if self.old_data: + # Take the most recent + max_datetime = max( + [x["lodgement-datetime"] for x in self.old_data if + x["construction-age-band"] not in self.DATA_ANOMALY_MATCHES] + ) + most_recent = [x for x in self.old_data if x["lodgement-datetime"] == max_datetime] + + self.construction_age_band = DataProcessor.clean_construction_age_band( + most_recent[0]["construction-age-band"] + ) + self.age_band = england_wales_age_band_lookup.get(self.construction_age_band) + if self.age_band is None: + raise ValueError("age_band is missing") + def set_spatial(self, spatial: pd.DataFrame): """ Sets whether the property is in a conservation area given the output of the ConservationAreaClient