diff --git a/backend/Property.py b/backend/Property.py index 4b5e1304..dc847750 100644 --- a/backend/Property.py +++ b/backend/Property.py @@ -58,7 +58,7 @@ class Property(Definitions): self.full_sap_epc = full_sap_epc self.property_dimensions = None - self.uprn = None if data is None else data["uprn"] + self.uprn = None if data is None else int(data["uprn"]) self.in_conservation_area, self.is_listed, self.is_heritage = None, None, None self.restricted_measures = False diff --git a/backend/SearchEpc.py b/backend/SearchEpc.py index 9d3ecd42..4953b04c 100644 --- a/backend/SearchEpc.py +++ b/backend/SearchEpc.py @@ -584,6 +584,11 @@ class SearchEpc: estimated_epc[key] = estimated_value + # Insert an estimated lodgement datetime, with a weighted average + estimated_epc["lodgement-datetime"] = self.calculate_weighted_lodgement_datetime(epc_data=epc_data) + # Extract logement date + estimated_epc["lodgement-date"] = estimated_epc["lodgement-datetime"].strftime("%Y-%m-%d") + estimated_epc["postcode"] = self.postcode estimated_epc["uprn"] = self.uprn # Indicate that this epc was estimated @@ -591,6 +596,24 @@ class SearchEpc: return estimated_epc + @staticmethod + def calculate_weighted_lodgement_datetime(epc_data): + numeric_dates = pd.to_datetime(epc_data['lodgement-datetime']).view('int64') + + # Calculate the weighted sum of dates + weighted_sum = (numeric_dates * epc_data['weight']).sum() + + # Calculate the sum of weights + total_weights = epc_data['weight'].sum() + + # Calculate the weighted mean in numeric format + weighted_mean_numeric = weighted_sum / total_weights + + # Convert the numeric weighted mean back to datetime + weighted_mean_datetime = pd.to_datetime(weighted_mean_numeric) + + return weighted_mean_datetime + @staticmethod def _estimate_int(estimation_data, key): return round(np.average(a=estimation_data[key], weights=estimation_data["weight"]))