Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e05e404
Add pandas wrapper function for time series data frame.
tjann Aug 23, 2020
2f070fd
Save work so far on pandas.
tjann Aug 24, 2020
b1feeca
Minor edits.
tjann Aug 24, 2020
516033b
Add function for creating covariate pandas df.
tjann Aug 24, 2020
529aeb3
Add latest date sorting to covariate as well. Add test for covariate …
tjann Aug 24, 2020
a7868e2
stat_vars_test: make response and expected response strings consisten…
tjann Aug 24, 2020
ea3c2ff
Add an example for covariate_pd_input
tjann Aug 24, 2020
ab3f755
Make stat_var examples quoting consistent.
tjann Aug 24, 2020
e72ae4a
Create dcpandas module that uses pandas natively.
tjann Aug 24, 2020
32a0284
Do the python release in another PR.
tjann Aug 24, 2020
160eee6
Remove stale refs in datacommons library to pandas features.
tjann Aug 24, 2020
771b0a5
Update pandas readme.
tjann Aug 24, 2020
5a86466
Cleanup format.
tjann Aug 24, 2020
5780970
Remove pd-related mocks from python testing.
tjann Aug 24, 2020
cb83487
Cosmetics.
tjann Aug 24, 2020
85b3a9b
Update docstring
tjann Aug 24, 2020
4044c03
Fix import statement for pip. Always sort time series df columns.
tjann Aug 24, 2020
d6290be
Restore pandas setup to prepare for release.
tjann Aug 24, 2020
4bba808
change _group_stat_all_by_obs_options mode parameter to time_series b…
tjann Aug 24, 2020
c81eaa6
Address some documentation suggestions from cyin.
tjann Aug 24, 2020
d3a618d
Fix bug from reassigning parameter time_series value in _group_stat_a…
tjann Aug 24, 2020
a4bcf4e
Make df_builder examples more readable.
tjann Aug 24, 2020
a2202c0
Update the docstrings for both PyPI release setup*.py files. Change d…
tjann Aug 24, 2020
0ebb20f
Rename time_series parameter to keep_series for _group_stat_all_by_ob…
tjann Aug 24, 2020
efd2e0c
dcpandas to datacommons_pandas, including all datacommons functions
tjann Aug 25, 2020
f645f3f
Fix various docstrings.
tjann Aug 25, 2020
1ea347d
Merge branch 'master' into pandas-funcs
tjann Aug 25, 2020
5454a93
Merge branch 'pandas-funcs' of github.com:tjann/api-python into panda…
tjann Aug 25, 2020
18cb93e
Add optional args to pandas lib build_time_series to pass onto python…
tjann Aug 25, 2020
51582c8
Update docstrings for time series funcs.
tjann Aug 25, 2020
a49a095
Remove will from CHANGELOG.
tjann Aug 25, 2020
7f46fdd
Reference TODO for cloudbuild pandas-python sync check. Update change…
tjann Aug 25, 2020
87b13ec
Rename covariate* to multivariate*, address cyin's comments on df_bui…
tjann Aug 25, 2020
f116e42
Update docstring for _group_stat_all_by_obs_options.
tjann Aug 25, 2020
975c956
Make err msg for _group_stat_all_by_obs_options no data more general.
tjann Aug 25, 2020
9865e09
Parameterize some pandas lib example functions.
tjann Aug 26, 2020
14dea40
Released pandas.
tjann Aug 26, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.linting.enabled": true
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ understanding API usage.
For more detail on getting started with the API, please visit our
[API Overview](http://docs.datacommons.org/api/).

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

## About Data Commons

Expand Down
11 changes: 10 additions & 1 deletion datacommons/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

################################## IMPORTANT #################################
# All user-facing functions in this package must be symlinked to the #
# datacommons_pandas pkg. This is so that users do not need to import both #
# libraries for pd support. Please keep the below imports in sync with the #
# __init__.py in the datacommons_pandas/ dir, and add a symlink when #
# creating a new file. #
# TODO: https://github.com/datacommonsorg/api-python/issues/149 #
##############################################################################

# Data Commons SPARQL query support
from datacommons.query import query

Expand All @@ -22,4 +31,4 @@
from datacommons.stat_vars import get_stat_value, get_stat_series, get_stat_all

# Other utilities
from .utils import set_api_key
from datacommons.utils import set_api_key
20 changes: 10 additions & 10 deletions datacommons/examples/stat_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Basic examples for StatisticalVariable-based param_set Commons API functions."""
"""Basic examples for StatisticalVariable-based Data Commons API functions."""

from __future__ import absolute_import
from __future__ import division
Expand All @@ -25,16 +25,16 @@ def main():
param_sets = [
{
'place': 'geoId/06085',
'stat_var': 'Count_Person',
'stat_var': "Count_Person",
},
{
'place': 'geoId/06085',
'stat_var': 'Count_Person',
'stat_var': "Count_Person",
'date': '2018',
},
{
'place': 'geoId/06085',
'stat_var': 'Count_Person',
'stat_var': "Count_Person",
'date': '2018',
'measurement_method': 'CensusACS5yrSurvey',
},
Expand Down Expand Up @@ -111,20 +111,20 @@ def call_str(pvs):

pp = pprint.PrettyPrinter(indent=4)
print(
"\nget_stat_all(['geoId/06085', 'country/FRA'], ['Median_Age_Person', 'Count_Person'])"
'\nget_stat_all(["geoId/06085", "country/FRA"], ["Median_Age_Person", "Count_Person"])'
)
print('>>> ')
pp.pprint(
dc.get_stat_all(['geoId/06085', 'country/FRA'],
['Median_Age_Person', 'Count_Person']))
dc.get_stat_all(["geoId/06085", "country/FRA"],
["Median_Age_Person", "Count_Person"]))

print(
"\nget_stat_all(['badPlaceId', 'country/FRA'], ['Median_Age_Person', 'Count_Person'])"
'\nget_stat_all(["badPlaceId", "country/FRA"], ["Median_Age_Person", "Count_Person"])'
)
print('>>> ')
pp.pprint(
dc.get_stat_all(['badPlaceId', 'country/FRA'],
['Median_Age_Person', 'Count_Person']))
dc.get_stat_all(["badPlaceId", "country/FRA"],
["Median_Age_Person", "Count_Person"]))


if __name__ == '__main__':
Expand Down
108 changes: 55 additions & 53 deletions datacommons/stat_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@
from __future__ import division
from __future__ import print_function

from datacommons.utils import _API_ROOT, _API_ENDPOINTS, _ENV_VAR_API_KEY

import collections
import json
import os
import six.moves.urllib.error
import six.moves.urllib.request
import six

import datacommons.utils as utils

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

try:
res_json = utils._send_request(url, post=False, use_payload=False)
res_json = utils._send_request(url, post=False, use_payload=False)
except ValueError:
raise ValueError('No data in response.')
raise ValueError('No data in response.')
return res_json['value']


Expand All @@ -102,7 +97,7 @@ def get_stat_series(place,
scaling_factor (`int`): Optional, the preferred `scalingFactor` value.
Returns:
A `dict` mapping dates to value of `stat_var` for `place`,
filtered by optional args.
representing a time series that satisfies all input parameters.

Raises:
ValueError: If the payload returned by the Data Commons REST API is
Expand Down Expand Up @@ -148,55 +143,62 @@ def get_stat_all(places, stat_vars):
>>> get_stat_all(["geoId/05", "geoId/06"], ["Count_Person", "Count_Person_Male"])
{
"geoId/05": {
"Count_Person": [
{
"val": {
"2010": 1633,
"2011": 1509,
"2012": 1581,
},
"observationPeriod": "P1Y",
"importName": "Wikidata",
"provenanceDomain": "wikidata.org"
},
{
"val": {
"2010": 1333,
"2011": 1309,
"2012": 131,
"Count_Person": {
"sourceSeries": [
{
"val": {
"2010": 1633,
"2011": 1509,
"2012": 1581,
},
"observationPeriod": "P1Y",
"importName": "Wikidata",
"provenanceDomain": "wikidata.org"
},
"observationPeriod": "P1Y",
"importName": "CensusPEPSurvey",
"provenanceDomain": "census.gov"
{
"val": {
"2010": 1333,
"2011": 1309,
"2012": 131,
},
"observationPeriod": "P1Y",
"importName": "CensusPEPSurvey",
"provenanceDomain": "census.gov"
}
],
}
],
"Count_Person_Male": [
{
"val": {
"2010": 1633,
"2011": 1509,
"2012": 1581,
},
"observationPeriod": "P1Y",
"importName": "CensusPEPSurvey",
"provenanceDomain": "census.gov"
}
],
},
"Count_Person_Male": {
"sourceSeries": [
{
"val": {
"2010": 1633,
"2011": 1509,
"2012": 1581,
},
"observationPeriod": "P1Y",
"importName": "CensusPEPSurvey",
"provenanceDomain": "census.gov"
}
],
}
},
"geoId/02": {
"Count_Person": [],
"Count_Person_Male": [
{
"val": {
"2010": 13,
"2011": 13,
"2012": 322,
},
"observationPeriod": "P1Y",
"importName": "CensusPEPSurvey",
"provenanceDomain": "census.gov"
"Count_Person": {},
"Count_Person_Male": {
"sourceSeries": [
{
"val": {
"2010": 13,
"2011": 13,
"2012": 322,
},
"observationPeriod": "P1Y",
"importName": "CensusPEPSurvey",
"provenanceDomain": "census.gov"
}
]
}
],
}
}
"""
Expand Down
2 changes: 1 addition & 1 deletion datacommons/test/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def read(self):
return MockResponse(json.dumps({'payload': res_json}))

# Otherwise, return an empty response and a 404.
return urllib.error.HTTPError
return urllib.error.HTTPError(None, 404, None, None, None)


class TestGetPropertyLabels(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion datacommons/test/places_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def read(self):
return MockResponse(json.dumps({'payload': res_json}))

# Otherwise, return an empty response and a 404.
return urllib.error.HTTPError
return urllib.error.HTTPError(None, 404, None, None, None)

class TestGetPlacesIn(unittest.TestCase):
""" Unit stests for get_places_in. """
Expand Down
Loading