Get a Collection of Statistical Data for Multiple Places
datacommons.get_stat_all(places, stat_vars)
Returns a nested dict of all time series for places and stat_vars.
Arguments
-
places (Iterable of str): Thedcids of thePlaceto query for. -
stats_vars (Iterable of str): Thedcids of theStatisticalVariable.
Returns
A nested dict mapping Places to StatisticalVariables and all available
time series for each Place and StatisticalVariable pair.
The top level dict key is the Place dcid and the second level dict key is the
StatisticalVariable dcid, with the object being an array of time series object
with the following fields
val: a dict from date to statistical value.importName: the import name of the observations.provenanceDomain: the Provenance domain of the observations.measurementMethod: themeasurementMethodof the observations, if it exists.observationPeriod: theobservationPeriodof the observations, if it exists.unit: theunitof the observations, if it exists.scalingFactor: thescalingFactorof the observations, if it exists.
Raises
ValueError- If no statistical value found for anyPlaceandStatisticalVariablecombinations.
Be sure to initialize the library. Check the Python library setup guide for more details.
You can find a list of StatisticalVariables with human-readable names here.
Examples
We would like to get the population and the male population in Arkansas and California.
>>> import datacommons as dc
>>> dc.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,
},
"observationPeriod": "P1Y",
"importName": "CensusPEPSurvey",
"provenanceDomain": "census.gov"
}
],
"Count_Person_Male": [
{
"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"
}
],
}
}
In the next example, there is no data found so the API throws ValueError:
>>> dc.get_stat_all(['badGeoId'], ['BadStaVar'])
>>> Traceback (most recent call last):
...
raise ValueError('No data in response.')

