wbdata.api library reference

The api library is where all the nuts-and-bolts functions are defined, as well as those which are imported into the main namespace.

wbdata.api: Where all the functions go

wbdata.api.cast_float(value)

Return a floated value or none

wbdata.api.convert_dates_to_datetime(data)

Return a datetime.datetime object from a date string as provided by the World Bank

wbdata.api.convert_month_to_datetime(monthstr)

return datetime.datetime object from %YM%m formatted string

wbdata.api.convert_quarter_to_datetime(quarterstr)

return datetime.datetime object from %YQ%# formatted string, where # is the desired quarter

wbdata.api.convert_to_dataframe(data, column_name)

Convert a set of values to a dataframe with columns for country and date

wbdata.api.convert_year_to_datetime(yearstr)

return datetime.datetime object from %Y formatted string

wbdata.api.get_country(country_id=None, incomelevel=None, lendingtype=None, display=None)

Retrieve information on a country or regional aggregate. Can specify either country_id, or the aggregates, but not both

Country_id:a country id or sequence thereof. None returns all countries and aggregates.
Incomelevel:desired incomelevel id or ids.
Lendingtype:desired lendingtype id or ids.
Display:if True,print ids and names instead of returning results. Defaults to True if in interactive prompt, or False otherwise.
Returns:if display is False, a dictionary describing an lending type aggregate.
wbdata.api.get_data(indicator, country=u'all', data_date=None, convert_date=False, pandas=False, column_name=u'value', keep_levels=False)

Retrieve indicators for given countries and years

Indicator:the desired indicator code
Country:a country code, sequence of country codes, or “all” (default)
Date:the desired date as a datetime object or a 2-tuple with start and end dates
Convert_date:if True, convert date field to a datetime.datetime object.
Pandas:if True, return results as a pandas Series. The index will be the date of the data if only one country is specified, the countries if only one date is specified, or a multi-index of country and date otherwise.
Column_name:the desired name for the pandas column
Keep_levels:if True and pandas is True, don’t reduce the number of index levels returned if only getting one date or country
Returns:list of dictionaries or pandas Series
wbdata.api.get_dataframe(indicators, country=u'all', data_date=None, convert_date=False, keep_levels=False)
Convenience function to download a set of indicators and merge them into a
pandas DataFrame. The index will be the same as if calls were made to get_data separately.
Indicators:An dictionary where the keys are desired indicators and the values are the desired column names
Country:a country code, sequence of country codes, or “all” (default)
Data_date:the desired date as a datetime object or a 2-sequence with start and end dates
Convert_date:if True, convert date field to a datetime.datetime object.
Keep_levels:if True and pandas is True, don’t reduce the number of index levels returned if only getting one date or country
Returns:a pandas dataframe
wbdata.api.get_incomelevel(level_id=None, display=None)

Retrieve information on an income level aggregate

Level_id:a level id or sequence thereof. None returns all income level aggregates
Display:if True,print ids and names instead of returning results. Defaults to True if in interactive prompt, or False otherwise
Returns:if display is False a dictionary describing an income level aggregate
wbdata.api.get_indicator(indicator=None, source=None, topic=None, display=None)

Retrieve information about an indicator or indicators. Only one of indicator, source, and topic can be specified. Specifying none of the three will return all indicators.

Indicator:an indicator code or sequence thereof
Source:a source id or sequence thereof
Topic:a topic id or sequence thereof
Display:if True,print ids and names instead of returning results. Defaults to True if in interactive prompt, or False otherwise
Returns:if display is False, a list of dictionary objects representing indicators
wbdata.api.get_lendingtype(type_id=None, display=None)

Retrieve information on an income level aggregate

Level_id:lending type id or sequence thereof. None returns all lending type aggregates
Display:if True,print ids and names instead of returning results. Defaults to True if in interactive prompt, or False otherwise
Returns:if display is False, a dictionary describing an lending type aggregate
wbdata.api.get_panel(indicators, country=u'all', data_date=None, convert_date=False, items=u'indicators', major_axis=u'dates')
Convenience function to download a set of indicators and merge them into a
pandas Panel.
Indicators:An dictionary where the keys are desired indicators and the values are the desired column names
Country:a country code, sequence of country codes, or “all” (default)
Data_date:a 2-sequence with start and end dates
Convert_date:if True, convert date field to a datetime.datetime object.
Items:which values to use as the Panel items. One of “indicators”, “countries”, “dates”
Major_axis:which values to use as major axis for each item. One of “indicators”, “countries”, “dates”
Returns:a pandas panel
wbdata.api.get_source(source_id=None, display=None)

Retrieve information on a source

Source_id:a source id or sequence thereof. None returns all sources
Display:if True,print ids and names instead of returning results. Defaults to True if in interactive prompt, or False otherwise
Returns:if display is False, a dictionary describing a source
wbdata.api.get_topic(topic_id=None, display=None)

Retrieve information on a topic

Topic_id:a topic id or sequence thereof. None returns all topics
Display:if True,print ids and names instead of returning results. Defaults to True if in interactive prompt, or False otherwise
Returns:if display is False, a dictionary describing an income level aggregate
wbdata.api.id_only_query(query_url, query_id, display)

Retrieve information when ids are the only arguments

Query_url:the base url to use for the query
Query_id:an id number. None returns all sources
Display:if True,print ids and names instead of returning results. Defaults to True if in interactive prompt, or False otherwise
Returns:if display is False, a dictionary describing a source
wbdata.api.parse_value_or_iterable(arg)

If arg is a single value, return it as a string; if an iterable, return a ;-joined string of all values

wbdata.api.print_ids_and_names(objs)

Courtesy function to display ids and names from lists returned by wbdata. This will mostly be useful in interactive mode.

Objs:a list of dictionary objects as returned by wbdata
wbdata.api.search_countries(query, incomelevel=None, lendingtype=None, display=None)

Search countries by name. Very simple search.

Query:the string to match against country names
Incomelevel:if present, search only the matching incomelevel
Lendingtype:if present, search only the matching lendingtype
Display:if True,print ids and names instead of returning results. Defaults to True if in interactive prompt, or False otherwise
Returns:a list of dictionaries representing countries if display is False
wbdata.api.search_indicators(query, source=None, topic=None, display=None)

Search indicators for a certain term. Very simple. Only one of source or topic can be specified. In interactive mode, will return None and print ids and names unless suppress_printing is True.

Query:the term to match against indicator names
Source:if present, id of desired source
Topic:if present, id of desired topic
Display:if True,print ids and names instead of returning results. Defaults to True if in interactive prompt, or False otherwise
Returns:a list of dictionaries representing indicators if display is False
wbdata.api.uses_pandas(f)

Raise ValueError if pandas is not loaded