Skip to content

Commit 01d2578

Browse files
authored
Check and filter out NaN in dcids for input arguments (#122)
* Remove NaN values from API input dcids * Add unittest
1 parent e3f2aa5 commit 01d2578

7 files changed

Lines changed: 21 additions & 3 deletions

File tree

datacommons/core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def get_property_labels(dcids, out=True):
9696
}
9797
"""
9898
# Generate the GetProperty query and send the request
99+
dcids = filter(lambda v: v==v, dcids) # Filter out NaN values
99100
dcids = list(dcids)
100101
url = utils._API_ROOT + utils._API_ENDPOINTS['get_property_labels']
101102
payload = utils._send_request(url, req_json={'dcids': dcids})
@@ -152,6 +153,7 @@ def get_property_values(dcids,
152153
}
153154
"""
154155
# Convert the dcids field and format the request to GetPropertyValue
156+
dcids = filter(lambda v: v==v, dcids) # Filter out NaN values
155157
dcids = list(dcids)
156158
if out:
157159
direction = 'out'
@@ -232,6 +234,7 @@ def get_triples(dcids, limit=utils._MAX_LIMIT):
232234
}
233235
"""
234236
# Generate the GetTriple query and send the request.
237+
dcids = filter(lambda v: v==v, dcids) # Filter out NaN values
235238
dcids = list(dcids)
236239
url = utils._API_ROOT + utils._API_ENDPOINTS['get_triples']
237240
payload = utils._send_request(url, req_json={'dcids': dcids, 'limit': limit})

datacommons/places.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def get_places_in(dcids, place_type):
6060
]
6161
}
6262
"""
63+
dcids = filter(lambda v: v==v, dcids) # Filter out NaN values
6364
dcids = list(dcids)
6465
url = utils._API_ROOT + utils._API_ENDPOINTS['get_places_in']
6566
payload = utils._send_request(url, req_json={
@@ -119,6 +120,7 @@ def get_related_places(dcids, population_type, measured_property,
119120
]
120121
}
121122
"""
123+
dcids = filter(lambda v: v==v, dcids) # Filter out NaN values
122124
dcids = list(dcids)
123125
url = utils._API_ROOT + utils._API_ENDPOINTS['get_related_places']
124126
pvs = []

datacommons/populations.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def get_populations(dcids, population_type, constraining_properties={}):
9797
}
9898
"""
9999
# Convert the dcids field and format the request to GetPopulations
100+
dcids = filter(lambda v: v==v, dcids) # Filter out NaN values
100101
dcids = list(dcids)
101102
pv = [{'property': k, 'value': v} for k, v in constraining_properties.items()]
102103
url = utils._API_ROOT + utils._API_ENDPOINTS['get_populations']
@@ -176,6 +177,7 @@ def get_observations(dcids,
176177
"dc/p/lr52m1yr46r44": 3075662.0
177178
}
178179
"""
180+
dcids = filter(lambda v: v==v, dcids) # Filter out NaN values
179181
dcids = list(dcids)
180182
req_json = {
181183
'dcids': dcids,

datacommons/test/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ py_test(
77
deps = [
88
"//datacommons:datacommons",
99
requirement("mock"),
10+
requirement("six"),
1011
],
1112
python_version = "PY3"
1213
)
@@ -17,6 +18,7 @@ py_test(
1718
deps = [
1819
"//datacommons:datacommons",
1920
requirement("mock"),
21+
requirement("six"),
2022
],
2123
python_version = "PY3"
2224
)
@@ -27,6 +29,7 @@ py_test(
2729
deps = [
2830
"//datacommons:datacommons",
2931
requirement("mock"),
32+
requirement("six"),
3033
],
3134
python_version = "PY3"
3235
)
@@ -37,6 +40,7 @@ py_test(
3740
deps = [
3841
"//datacommons:datacommons",
3942
requirement("mock"),
43+
requirement("six"),
4044
],
4145
python_version = "PY3"
4246
)

datacommons/test/core_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,15 @@ def test_multiple_dcids(self, urlopen_mock):
411411
'geoId/24031': ['geoId/2462850']
412412
})
413413

414+
dcids = ['geoId/06085', 'geoId/24031', float('nan')]
415+
# Handle NaN values
416+
towns = dc.get_property_values(
417+
dcids, 'containedInPlace', out=False, value_type='Town')
418+
self.assertDictEqual(towns, {
419+
'geoId/06085': ['geoId/0643294', 'geoId/0644112'],
420+
'geoId/24031': ['geoId/2462850']
421+
})
422+
414423
# Get the name of Santa Clara and Montgomery County.
415424
names = dc.get_property_values(dcids, 'name')
416425
self.assertDictEqual(names, {

requirements.bazel.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
six
2-
mock
3-
httplib2
2+
mock

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
REQUIRED = [
3131
'six',
32-
'httplib2',
3332
]
3433

3534
PACKAGES = ['datacommons']

0 commit comments

Comments
 (0)