Skip to content

Commit 338000c

Browse files
authored
Clarify parent_entity requirements for observations_dataframe (#242)
1 parent 6eb647f commit 338000c

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

datacommons_client/client.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,18 @@ def observations_dataframe(
136136
entity_type (Optional[str]): The type of entities to filter by when `entity_dcids="all"`.
137137
Required if `entity_dcids="all"`. Defaults to None.
138138
parent_entity (Optional[str]): The parent entity under which the target entities fall.
139-
Used only when `entity_dcids="all"`. Defaults to None.
139+
Required if `entity_dcids="all"`. Defaults to None.
140140
property_filters (Optional[dict[str, str | list[str]]): An optional dictionary used to filter
141141
the data by using observation properties like `measurementMethod`, `unit`, or `observationPeriod`.
142142
143143
Returns:
144144
pd.DataFrame: A DataFrame containing the requested observations.
145145
"""
146146

147-
if entity_dcids == "all" and not entity_type:
147+
if entity_dcids == "all" and not (entity_type and parent_entity):
148148
raise ValueError(
149-
"When 'entity_dcids' is 'all', 'entity_type' must be specified.")
149+
"When 'entity_dcids' is 'all', both 'parent_entity' and 'entity_type' must be specified."
150+
)
150151

151152
if entity_dcids != "all" and (entity_type or parent_entity):
152153
raise ValueError(

datacommons_client/tests/test_client.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,27 @@ def test_observations_dataframe_raises_error_when_entities_all_but_no_entity_typ
7070
"""Tests that ValueError is raised if 'entities' is 'all' but 'entity_type' is not specified."""
7171
with pytest.raises(
7272
ValueError,
73-
match="When 'entity_dcids' is 'all', 'entity_type' must be specified.",
73+
match=
74+
"When 'entity_dcids' is 'all', both 'parent_entity' and 'entity_type' must be specified.",
7475
):
7576
mock_client.observations_dataframe(variable_dcids="var1",
7677
date="2024",
77-
entity_dcids="all")
78+
entity_dcids="all",
79+
parent_entity="africa")
80+
81+
82+
def test_observations_dataframe_raises_error_when_entities_all_but_no_parent_entity(
83+
mock_client,):
84+
"""Tests that ValueError is raised if 'entities' is 'all' but 'entity_type' is not specified."""
85+
with pytest.raises(
86+
ValueError,
87+
match=
88+
"When 'entity_dcids' is 'all', both 'parent_entity' and 'entity_type' must be specified.",
89+
):
90+
mock_client.observations_dataframe(variable_dcids="var1",
91+
date="2024",
92+
entity_dcids="all",
93+
entity_type="Country")
7894

7995

8096
def test_observations_dataframe_raises_error_when_invalid_entity_type_usage(

0 commit comments

Comments
 (0)