Skip to content

Commit ffba5fd

Browse files
authored
Add pandas helper functions for get_stat_* (#144)
Released on PyPI
1 parent ed7236c commit ffba5fd

26 files changed

+1074
-103
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.linting.enabled": true
3+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ understanding API usage.
2121
For more detail on getting started with the API, please visit our
2222
[API Overview](http://docs.datacommons.org/api/).
2323

24-
After you're ready to use the API, you can refer to `datacommons/examples` for
24+
When you are ready to use the API, you can refer to `datacommons/examples` for
2525
examples on how to use this package to perform various tasks. More tutorials and
26-
documentation can be found at [tutorials](https://datacommons.org/colab)!
26+
documentation can be found on our [tutorials page](https://datacommons.org/colab)!
2727

2828
## About Data Commons
2929

datacommons/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
################################## IMPORTANT #################################
16+
# All user-facing functions in this package must be symlinked to the #
17+
# datacommons_pandas pkg. This is so that users do not need to import both #
18+
# libraries for pd support. Please keep the below imports in sync with the #
19+
# __init__.py in the datacommons_pandas/ dir, and add a symlink when #
20+
# creating a new file. #
21+
# TODO: https://github.com/datacommonsorg/api-python/issues/149 #
22+
##############################################################################
23+
1524
# Data Commons SPARQL query support
1625
from datacommons.query import query
1726

@@ -22,4 +31,4 @@
2231
from datacommons.stat_vars import get_stat_value, get_stat_series, get_stat_all
2332

2433
# Other utilities
25-
from .utils import set_api_key
34+
from datacommons.utils import set_api_key

datacommons/examples/stat_vars.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
"""Basic examples for StatisticalVariable-based param_set Commons API functions."""
14+
"""Basic examples for StatisticalVariable-based Data Commons API functions."""
1515

1616
from __future__ import absolute_import
1717
from __future__ import division
@@ -25,16 +25,16 @@ def main():
2525
param_sets = [
2626
{
2727
'place': 'geoId/06085',
28-
'stat_var': 'Count_Person',
28+
'stat_var': "Count_Person",
2929
},
3030
{
3131
'place': 'geoId/06085',
32-
'stat_var': 'Count_Person',
32+
'stat_var': "Count_Person",
3333
'date': '2018',
3434
},
3535
{
3636
'place': 'geoId/06085',
37-
'stat_var': 'Count_Person',
37+
'stat_var': "Count_Person",
3838
'date': '2018',
3939
'measurement_method': 'CensusACS5yrSurvey',
4040
},
@@ -111,20 +111,20 @@ def call_str(pvs):
111111

112112
pp = pprint.PrettyPrinter(indent=4)
113113
print(
114-
"\nget_stat_all(['geoId/06085', 'country/FRA'], ['Median_Age_Person', 'Count_Person'])"
114+
'\nget_stat_all(["geoId/06085", "country/FRA"], ["Median_Age_Person", "Count_Person"])'
115115
)
116116
print('>>> ')
117117
pp.pprint(
118-
dc.get_stat_all(['geoId/06085', 'country/FRA'],
119-
['Median_Age_Person', 'Count_Person']))
118+
dc.get_stat_all(["geoId/06085", "country/FRA"],
119+
["Median_Age_Person", "Count_Person"]))
120120

121121
print(
122-
"\nget_stat_all(['badPlaceId', 'country/FRA'], ['Median_Age_Person', 'Count_Person'])"
122+
'\nget_stat_all(["badPlaceId", "country/FRA"], ["Median_Age_Person", "Count_Person"])'
123123
)
124124
print('>>> ')
125125
pp.pprint(
126-
dc.get_stat_all(['badPlaceId', 'country/FRA'],
127-
['Median_Age_Person', 'Count_Person']))
126+
dc.get_stat_all(["badPlaceId", "country/FRA"],
127+
["Median_Age_Person", "Count_Person"]))
128128

129129

130130
if __name__ == '__main__':

datacommons/stat_vars.py

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,8 @@
2020
from __future__ import division
2121
from __future__ import print_function
2222

23-
from datacommons.utils import _API_ROOT, _API_ENDPOINTS, _ENV_VAR_API_KEY
24-
2523
import collections
26-
import json
27-
import os
28-
import six.moves.urllib.error
29-
import six.moves.urllib.request
24+
import six
3025

3126
import datacommons.utils as utils
3227

@@ -77,9 +72,9 @@ def get_stat_value(place,
7772
url += '&scaling_factor={}'.format(scaling_factor)
7873

7974
try:
80-
res_json = utils._send_request(url, post=False, use_payload=False)
75+
res_json = utils._send_request(url, post=False, use_payload=False)
8176
except ValueError:
82-
raise ValueError('No data in response.')
77+
raise ValueError('No data in response.')
8378
return res_json['value']
8479

8580

@@ -102,7 +97,7 @@ def get_stat_series(place,
10297
scaling_factor (`int`): Optional, the preferred `scalingFactor` value.
10398
Returns:
10499
A `dict` mapping dates to value of `stat_var` for `place`,
105-
filtered by optional args.
100+
representing a time series that satisfies all input parameters.
106101
107102
Raises:
108103
ValueError: If the payload returned by the Data Commons REST API is
@@ -148,55 +143,62 @@ def get_stat_all(places, stat_vars):
148143
>>> get_stat_all(["geoId/05", "geoId/06"], ["Count_Person", "Count_Person_Male"])
149144
{
150145
"geoId/05": {
151-
"Count_Person": [
152-
{
153-
"val": {
154-
"2010": 1633,
155-
"2011": 1509,
156-
"2012": 1581,
157-
},
158-
"observationPeriod": "P1Y",
159-
"importName": "Wikidata",
160-
"provenanceDomain": "wikidata.org"
161-
},
162-
{
163-
"val": {
164-
"2010": 1333,
165-
"2011": 1309,
166-
"2012": 131,
146+
"Count_Person": {
147+
"sourceSeries": [
148+
{
149+
"val": {
150+
"2010": 1633,
151+
"2011": 1509,
152+
"2012": 1581,
153+
},
154+
"observationPeriod": "P1Y",
155+
"importName": "Wikidata",
156+
"provenanceDomain": "wikidata.org"
167157
},
168-
"observationPeriod": "P1Y",
169-
"importName": "CensusPEPSurvey",
170-
"provenanceDomain": "census.gov"
158+
{
159+
"val": {
160+
"2010": 1333,
161+
"2011": 1309,
162+
"2012": 131,
163+
},
164+
"observationPeriod": "P1Y",
165+
"importName": "CensusPEPSurvey",
166+
"provenanceDomain": "census.gov"
167+
}
168+
],
171169
}
172-
],
173-
"Count_Person_Male": [
174-
{
175-
"val": {
176-
"2010": 1633,
177-
"2011": 1509,
178-
"2012": 1581,
179-
},
180-
"observationPeriod": "P1Y",
181-
"importName": "CensusPEPSurvey",
182-
"provenanceDomain": "census.gov"
183-
}
184-
],
170+
},
171+
"Count_Person_Male": {
172+
"sourceSeries": [
173+
{
174+
"val": {
175+
"2010": 1633,
176+
"2011": 1509,
177+
"2012": 1581,
178+
},
179+
"observationPeriod": "P1Y",
180+
"importName": "CensusPEPSurvey",
181+
"provenanceDomain": "census.gov"
182+
}
183+
],
184+
}
185185
},
186186
"geoId/02": {
187-
"Count_Person": [],
188-
"Count_Person_Male": [
189-
{
190-
"val": {
191-
"2010": 13,
192-
"2011": 13,
193-
"2012": 322,
194-
},
195-
"observationPeriod": "P1Y",
196-
"importName": "CensusPEPSurvey",
197-
"provenanceDomain": "census.gov"
187+
"Count_Person": {},
188+
"Count_Person_Male": {
189+
"sourceSeries": [
190+
{
191+
"val": {
192+
"2010": 13,
193+
"2011": 13,
194+
"2012": 322,
195+
},
196+
"observationPeriod": "P1Y",
197+
"importName": "CensusPEPSurvey",
198+
"provenanceDomain": "census.gov"
199+
}
200+
]
198201
}
199-
],
200202
}
201203
}
202204
"""

datacommons/test/core_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def read(self):
311311
return MockResponse(json.dumps({'payload': res_json}))
312312

313313
# Otherwise, return an empty response and a 404.
314-
return urllib.error.HTTPError
314+
return urllib.error.HTTPError(None, 404, None, None, None)
315315

316316

317317
class TestGetPropertyLabels(unittest.TestCase):

datacommons/test/places_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def read(self):
212212
return MockResponse(json.dumps({'payload': res_json}))
213213

214214
# Otherwise, return an empty response and a 404.
215-
return urllib.error.HTTPError
215+
return urllib.error.HTTPError(None, 404, None, None, None)
216216

217217
class TestGetPlacesIn(unittest.TestCase):
218218
""" Unit stests for get_places_in. """

0 commit comments

Comments
 (0)