@@ -926,6 +926,12 @@ def generate_payload_for_v2_area_weighting(
926
926
927
927
928
928
def format_v2_area_weighting_response (response_content : Dict [str , Any ]) -> pd .DataFrame :
929
+ DATETIME_COL_MAPPINGS = {
930
+ "start_date" : "timestamp" , # add start_date col which is equivalent to end_date
931
+ "end_date" : "timestamp" ,
932
+ "available_date" : "available_timestamp" ,
933
+ }
934
+
929
935
try :
930
936
data_points = response_content ["data_points" ]
931
937
weighted_series_df = pd .DataFrame (data_points )
@@ -934,14 +940,10 @@ def format_v2_area_weighting_response(response_content: Dict[str, Any]) -> pd.Da
934
940
return weighted_series_df
935
941
936
942
# convert unix timestamps and rename date cols
937
- datetime_col_mappings = {
938
- "start_date" : "timestamp" , # add start_date col which is equivalent to end_date
939
- "end_date" : "timestamp" ,
940
- "available_date" : "available_timestamp" ,
941
- }
942
- for new_col , col in datetime_col_mappings .items ():
943
- weighted_series_df [new_col ] = pd .to_datetime (weighted_series_df [col ], unit = 's' ).dt .strftime ('%Y-%m-%d' )
944
- weighted_series_df = weighted_series_df .drop (columns = datetime_col_mappings .values ())
943
+ for new_col , col in DATETIME_COL_MAPPINGS .items ():
944
+ if col in weighted_series_df .columns :
945
+ weighted_series_df [new_col ] = pd .to_datetime (weighted_series_df [col ], unit = 's' ).dt .strftime ('%Y-%m-%d' )
946
+ weighted_series_df = weighted_series_df .drop (columns = DATETIME_COL_MAPPINGS .values (), errors = "ignore" )
945
947
946
948
# append selected fields of series metadata
947
949
for key in ['item_id' , 'metric_id' , 'frequency_id' , 'unit_id' , 'source_id' ]:
0 commit comments