Skip to content

Commit 62ab22b

Browse files
authored
Merge pull request #390 from gro-intelligence/GAIA-30812-api
GAIA-30812 Update the python SDK to include new changes for v2/area-weighting
2 parents 341cc7f + c3e9f3c commit 62ab22b

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

groclient/client.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1995,7 +1995,7 @@ def reverse_geocode_points(self, points: list):
19951995
def get_area_weighted_series_df(
19961996
self,
19971997
series: Dict[str, int],
1998-
region_id: int,
1998+
region_ids: List[int],
19991999
weights: Optional[List[Dict[str, int]]] = None,
20002000
weight_names: Optional[List[str]] = None,
20012001
start_date: Optional[str] = None,
@@ -2011,8 +2011,8 @@ def get_area_weighted_series_df(
20112011
series: dict
20122012
A dictionary that maps required entity types to its value.
20132013
e.g. {"item_id": 321, "metric_id": 70029, "frequency_id": 3, "source_id": 3}
2014-
region_id: integer
2015-
The region for which the weighted series will be computed
2014+
region_ids: list of integer
2015+
The selected regions for which the weighted series will be computed
20162016
Supported region levels are (1, 2, 3, 4, 5, 8)
20172017
weights: list of dict
20182018
A list of dictionaries with each representing a weight object. Mutually exclusive with "weight_names".
@@ -2033,15 +2033,15 @@ def get_area_weighted_series_df(
20332033
DataFrame
20342034
20352035
Example::
2036-
start_date value end_date available_date region_id item_id metric_id frequency_id unit_id source_id weights
2037-
0 2016-04-26 0.502835 2016-04-26 2016-04-28 1215 321 70029 1 189 112 [{"weight_name": "Corn", "item_id": 274, "metric_id": 2120001, ...}, ...]
2038-
1 2016-04-27 0.509729 2016-04-27 2016-04-29 1215 321 70029 1 189 112 [{"weight_name": "Corn", "item_id": 274, "metric_id": 2120001, ...}, ...]
2036+
start_date value end_date available_date(optional) region_id item_id metric_id frequency_id unit_id source_id weights
2037+
0 2016-04-26 0.502835 2016-04-26 2016-04-28 1215 321 70029 1 189 112 [{"weight_name": "Corn", "item_id": 274, "metric_id": 2120001, ...}, ...]
2038+
1 2016-04-27 0.509729 2016-04-27 2016-04-29 1215 321 70029 1 189 112 [{"weight_name": "Corn", "item_id": 274, "metric_id": 2120001, ...}, ...]
20392039
"""
20402040
return lib.get_area_weighted_series_df(
20412041
self.access_token,
20422042
self.api_host,
20432043
series,
2044-
region_id,
2044+
region_ids,
20452045
weights,
20462046
weight_names,
20472047
start_date,

groclient/client_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ def test_get_area_weighted_series_df(self):
725725
"frequency_id": 3,
726726
"source_id": 3
727727
},
728-
"region_id": 1215,
728+
"region_ids": [1215],
729729
"weight_names": ["Wheat"],
730730
"start_date": "2023-10-01"
731731
}

groclient/lib.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -883,15 +883,15 @@ def validate_series_object(series_object):
883883

884884
def generate_payload_for_v2_area_weighting(
885885
series: Dict[str, int],
886-
region_id: int,
886+
region_ids: List[int],
887887
weights: Optional[List[Dict[str, int]]] = None,
888888
weight_names: Optional[List[str]] = None,
889889
start_date: Optional[str] = None,
890890
end_date: Optional[str] = None,
891891
method: Optional[str] = "sum",
892892
):
893893
payload = {
894-
"region_id": region_id,
894+
"region_ids": region_ids,
895895
"method": method,
896896
}
897897

@@ -926,6 +926,12 @@ def generate_payload_for_v2_area_weighting(
926926

927927

928928
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+
929935
try:
930936
data_points = response_content["data_points"]
931937
weighted_series_df = pd.DataFrame(data_points)
@@ -934,14 +940,10 @@ def format_v2_area_weighting_response(response_content: Dict[str, Any]) -> pd.Da
934940
return weighted_series_df
935941

936942
# 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")
945947

946948
# append selected fields of series metadata
947949
for key in ['item_id', 'metric_id', 'frequency_id', 'unit_id', 'source_id']:
@@ -959,7 +961,7 @@ def get_area_weighted_series_df(
959961
access_token: str,
960962
api_host: str,
961963
series: Dict[str, int],
962-
region_id: int,
964+
region_ids: List[int],
963965
weights: Optional[List[Dict[str, int]]] = None,
964966
weight_names: Optional[List[str]] = None,
965967
start_date: Optional[str] = None,
@@ -968,7 +970,7 @@ def get_area_weighted_series_df(
968970
) -> pd.DataFrame:
969971
payload = generate_payload_for_v2_area_weighting(
970972
series,
971-
region_id,
973+
region_ids,
972974
weights,
973975
weight_names,
974976
start_date,

groclient/lib_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ def test_get_area_weighted_series_df(mock_requests_post):
12281228
"frequency_id": 3,
12291229
"source_id": 3
12301230
},
1231-
"region_id": 1215,
1231+
"region_ids": [1215],
12321232
"weight_names": ["Corn", "Wheat"],
12331233
"start_date": "2023-10-01"
12341234
}

0 commit comments

Comments
 (0)