Get Time Series DataFrame

datacommons_pandas.build_time_series_dataframe(places, stat_var)

Returns a pandas.DataFrame with places as index and dates as columns, where each cell is the observed statistic for its place and date for the stat_var.

See the full list of StatisticalVariables.

Arguments

  • places (Iterable of str): A list of dcids of the Places to query for.

  • stat_var (str): The dcid of the StatisticalVariable.

Returns

A pandas.DataFrame with places (str) as index and dates (str) as columns, where each cell is the observed statistic (float) for that place on that date for the stat_var.

Raises

  • ValueError - If no statistical values found for the given parameters.

Be sure to initialize the library. See the datacommons_pandas 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 male population in Arkansas

>>> import datacommons_pandas as dcpd
>>> dcpd.build_time_series_dataframe("geoId/05", "Count_Person_Male")
             2001     2002     2003  ...     2016     2017     2018
place                                ...                           
geoId/05  1315210  1323840  1332910  ...  1469240  1475420  1480140

In the next example, there is no data about RetailDrugDistribution_DrugDistribution_Amphetamine for non-USA places, so the API throws ValueError for no data:

>>> import datacommons_pandas as dcpd
>>> dcpd.build_time_series_dataframe(
      ["country/MEX", "nuts/AT32"],
      "RetailDrugDistribution_DrugDistribution_Amphetamine"
    )
ValueError    Traceback (most recent call last)
...
-->    raise ValueError('No data for any of specified Places and StatisticalVariables.')

ValueError: No data for any of specified places and stat_vars.