Indicator API

class wbpy.IndicatorDataset(json_resp, url=None, date_of_call=None)[source]
dates(use_datetime=False)[source]

Return list of dates used in the dataset.

Parameters:use_datetime – If True, return dates as datetime.date() objects, rather than strings.
as_dict(use_datetime=False)[source]

Return dictionary of the dataset’s data.

Keys are: data[country_code][date]

Parameters:use_datetime – Use datetime.date() object as the date key, rather than string.
class wbpy.IndicatorAPI(fetch=None)[source]

Request data from the World Bank Indicators API.

You can override the default tempfile cache by passing a function fetch, which requests a URL and returns the response as a string.

get_dataset(indicator, country_codes=None, **kwargs)[source]

Request a dataset from the API.

Parameters:
  • indicator – The API indicator code, eg. SP.POP.TOTL for total population.
  • country_codes – List of ISO 1366 alpha-2 or alpha-3 country codes. If None, returns data for all countries.
  • kwargs – The following map directly to the API query args: language date mrv gapfill frequency
Returns:

IndicatorDataset instance containing the dataset and metadata.

get_indicators(indicator_codes=None, search=None, search_full=False, common_only=False, **kwargs)[source]

Request metadata on specific World Bank indicators.

Parameters:
  • indicator_codes – A list of codes to get metadata for, eg. [“SP.POP.GROW”]. If None, all indicators are returned (~8000)
  • common_only – Many of the indicators do not have wide data coverage. If True, filter out the ~6500 indicators that do not appear on the main World Bank website (http://data.worldbank.org/indicators/all),
  • search – Regexp string to filter out non-matching results. By default, this searches the main name of the entity. If search_full is assigned True, it will search all fields for the entity.
  • kwargs – The following map directly to the API query args: language source topic
Returns:

Dictionary of indicators and their metadata, with their IDs as keys.

get_countries(country_codes=None, search=None, search_full=False, **kwargs)[source]

Request country metadata.

eg. ISO code, coordinates, capital, income level, etc.

Parameters:
  • country_codes – List of alpha-2 or alpha-3 codes. If None, queries all countries.
  • search – Regexp string to filter out non-matching results. By default, this searches the main name of the entity. If search_full is assigned True, it will search all fields for the entity.
  • kwargs – The following map directly to the API query args: language incomeLevel lendingType region
Returns:

Dictionary of metadata with alpha-2 codes as keys.

get_income_levels(income_codes=None, search=None, search_full=False, **kwargs)[source]

Request income categories.

Parameters:
  • income_codes – List of 3-letter ID codes. If None, queries all (~10).
  • search – Regexp string to filter out non-matching results. By default, this searches the main name of the entity. If search_full is assigned True, it will search all fields for the entity.
  • kwargs – The following map directly to the API query args: language
Returns:

Dictionary of income levels using ID codes as keys.

get_lending_types(lending_codes=None, search=None, search_full=False, **kwargs)[source]

Request lending type categories.

Parameters:
  • lending_codes – List of lending codes. If None, queries all (4).
  • search – Regexp string to filter out non-matching results. By default, this searches the main name of the entity. If search_full is assigned True, it will search all fields for the entity.
  • kwargs – The following map directly to the API query args: language
Returns:

Dictionary of lending types using ID codes as keys.

get_regions(region_codes=None, search=None, search_full=False, **kwargs)[source]

Request region names and codes.

Parameters:
  • region_codes – List of 3-letter codes. If None, queries all (~26).
  • search – Regexp string to filter out non-matching results. By default, this searches the main name of the entity. If search_full is assigned True, it will search all fields for the entity.
  • kwargs – The following map directly to the API query args: language
Returns:

Dictionary of regions, using ID codes as keys.

get_topics(topic_codes=None, search=None, search_full=False, **kwargs)[source]

Request API topics.

All indicators are mapped to a topic, eg. Health, Private Sector. You can use the topic ID as a kwarg to get_indicators().

Parameters:
  • topic_codes – List of topic IDs. If None, queries all (~20).
  • search – Regexp string to filter out non-matching results. By default, this searches the main name of the entity. If search_full is assigned True, it will search all fields for the entity.
  • kwargs – The following map directly to the API query args: language
Returns:

Dictionary of topics usings ID numbers as keys.

get_sources(source_codes=None, search=None, search_full=False, **kwargs)[source]

Request API source info.

Parameters:
  • source_codes – List of source IDs. If None, queries all (~27).
  • search – Regexp string to filter out non-matching results. By default, this searches the main name of the entity. If search_full is assigned True, it will search all fields for the entity.
  • kwargs – The following map directly to the API query args: language
Returns:

Dictionary of sources using ID numbers as keys.

print_codes(results, search=None, search_key=None)[source]

Print formatted list of API IDs and their corresponding values.

Parameters:
  • search – Regexp string to filter out non-matching results. By default, this searches the main name of the entity.
  • search_key – A second-level KEY in your dict, eg. {foo: {KEY: val}}. If given, will only search the value corresponding to the key. Only used if search is given.
  • results – A dictionary that was returned by one of the get functions.
search_results(regexp, results, key=None)[source]

For a given dict of get_ results, filter out all keys that do not match the given regexp in either the key or the value. The search is not case sensitive.

Parameters:
  • regexp – The regexp string, passed to re.search.
  • results – A dictionary of get_foo() results.
  • key – A second-level KEY in your dict, eg. {foo: {KEY: val}}. If given, will only search the value corresponding to the key.
Returns:

The input dictionary, with non-matching keys removed.