added weighted datetime calc

This commit is contained in:
Khalim Conn-Kowlessar 2024-01-04 11:16:49 +00:00
parent 0d7479d96c
commit 41749e8557
2 changed files with 24 additions and 1 deletions

View file

@ -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

View file

@ -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"]))