povineq (top-level)¶
povineq
¶
povineq — Python wrapper for the World Bank PIP API.
Usage::
import povineq
# Core statistics
df = povineq.get_stats(country="AGO", year=2000)
df = povineq.get_wb()
df = povineq.get_agg(aggregate="fcv")
# Country profiles
df = povineq.get_cp(country="AGO")
df = povineq.get_cp_ki(country="IDN")
# Auxiliary tables
tables = povineq.get_aux()
df = povineq.get_aux("gdp")
df = povineq.get_countries()
# Info & cache
status = povineq.check_api()
info = povineq.get_pip_info()
df_versions = povineq.get_versions()
povineq.delete_cache()
cache = povineq.get_cache_info()
PIPAPIError(status_code, error_message='', details='', valid_values='')
¶
Bases: PIPError
API returned a structured error response (4xx/5xx).
Attributes:
| Name | Type | Description |
|---|---|---|
status_code |
HTTP status code of the response. |
|
error_message |
Short error description from the API. |
|
details |
Additional detail message, if provided. |
|
valid_values |
Comma-separated valid values, if provided. |
Source code in src/povineq/_errors.py
PIPError
¶
Bases: Exception
Base exception for all PIP API errors.
PIPRateLimitError(retry_after_seconds=0)
¶
Bases: PIPError
API rate limit exceeded (HTTP 429).
Attributes:
| Name | Type | Description |
|---|---|---|
retry_after_seconds |
Suggested wait time in seconds before retrying. |
Source code in src/povineq/_errors.py
PIPResponse(url, status, content_type, content, response)
dataclass
¶
Full API response object returned when simplify=False.
Equivalent to pipr's pip_api S3 class.
Attributes:
| Name | Type | Description |
|---|---|---|
url |
str
|
The request URL as a string. |
status |
int
|
HTTP status code. |
content_type |
str
|
Content-Type header value. |
content |
DataFrame
|
Parsed data as a pandas (or polars) DataFrame. |
response |
Response
|
The underlying :class: |
delete_cache()
¶
Delete all cached HTTP responses.
Removes the entire cache directory and re-creates an empty one so subsequent calls can start fresh.
Example
import povineq povineq.delete_cache()
Source code in src/povineq/_cache.py
get_cache_info()
¶
Return statistics about the current HTTP response cache.
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
A dictionary with keys: |
dict[str, object]
|
|
dict[str, object]
|
|
dict[str, object]
|
|
Example
import povineq info = povineq.get_cache_info() print(info["n_files"])
Source code in src/povineq/_cache.py
call_aux(table=None)
¶
Retrieve a previously stored auxiliary table from memory.
Mirrors pipr::call_aux().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str | None
|
Table name to retrieve. If |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame | list[str]
|
The stored DataFrame, or a list of stored table names. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the requested table is not in the store. |
Example
import povineq povineq.get_aux("gdp", assign_tb=True) df = povineq.call_aux("gdp")
Source code in src/povineq/auxiliary.py
display_aux(version=None, ppp_version=None, release_version=None, api_version=API_VERSION, fmt='json', simplify=True, server=None)
¶
Display available auxiliary tables.
Fetches the list of auxiliary tables and prints them. Mirrors
pipr::display_aux().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
str | None
|
Data version string. |
None
|
ppp_version
|
int | None
|
PPP base year. |
None
|
release_version
|
str | None
|
Release date in |
None
|
api_version
|
str
|
API version. |
API_VERSION
|
fmt
|
str
|
Response format. |
'json'
|
simplify
|
bool
|
Passed to :func: |
True
|
server
|
str | None
|
Server target. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame | list[str]
|
List of available table name strings. |
Example
import povineq povineq.display_aux()
Source code in src/povineq/auxiliary.py
get_aux(table=None, version=None, ppp_version=None, release_version=None, api_version=API_VERSION, fmt='json', simplify=True, server=None, dataframe_type='pandas', assign_tb=False, replace=False)
¶
Fetch an auxiliary dataset from the PIP API.
When no table is specified, returns a list of available table names.
Mirrors pipr::get_aux().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str | None
|
Auxiliary table name (e.g. |
None
|
version
|
str | None
|
Data version string. |
None
|
ppp_version
|
int | None
|
PPP base year. |
None
|
release_version
|
str | None
|
Release date in |
None
|
api_version
|
str
|
API version. |
API_VERSION
|
fmt
|
str
|
Response format — |
'json'
|
simplify
|
bool
|
If |
True
|
server
|
str | None
|
Server target. |
None
|
dataframe_type
|
Literal['pandas', 'polars']
|
|
'pandas'
|
assign_tb
|
bool | str
|
If |
False
|
replace
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame | list[str] | PIPResponse | bool
|
|
DataFrame | list[str] | PIPResponse | bool
|
|
DataFrame | list[str] | PIPResponse | bool
|
|
DataFrame | list[str] | PIPResponse | bool
|
|
Example
import povineq tables = povineq.get_aux() # list of available tables df = povineq.get_aux("gdp") # fetch GDP table povineq.get_aux("cpi", assign_tb=True) # fetch and store in memory
Source code in src/povineq/auxiliary.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
get_cp(country='all', povline=2.15, version=None, ppp_version=2017, release_version=None, api_version=API_VERSION, fmt='arrow', simplify=True, server=None, dataframe_type='pandas')
¶
Download country profile data.
Mirrors pipr::get_cp().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country
|
str | list[str]
|
ISO3 country code(s) or |
'all'
|
povline
|
float | None
|
Poverty line in 2017 PPP USD per day (default 2.15).
When |
2.15
|
version
|
str | None
|
Data version string. |
None
|
ppp_version
|
int
|
PPP base year (default 2017). |
2017
|
release_version
|
str | None
|
Release date in |
None
|
api_version
|
str
|
API version. |
API_VERSION
|
fmt
|
str
|
Response format — |
'arrow'
|
simplify
|
bool
|
If |
True
|
server
|
str | None
|
Server target — |
None
|
dataframe_type
|
Literal['pandas', 'polars']
|
|
'pandas'
|
Returns:
| Type | Description |
|---|---|
DataFrame | PIPResponse
|
A DataFrame of country profile data. |
Example
import povineq df = povineq.get_cp(country="AGO") df_all = povineq.get_cp()
Source code in src/povineq/country_profiles.py
get_cp_ki(country, povline=2.15, version=None, ppp_version=2017, release_version=None, api_version=API_VERSION, simplify=True, server=None, dataframe_type='pandas')
¶
Get country profile key indicators.
Mirrors pipr::get_cp_ki(). When simplify is True,
calls :func:unnest_ki to flatten the nested response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country
|
str
|
Single ISO3 country code (required). |
required |
povline
|
float | None
|
Poverty line in 2017 PPP USD per day (default 2.15).
When |
2.15
|
version
|
str | None
|
Data version string. |
None
|
ppp_version
|
int
|
PPP base year (default 2017). |
2017
|
release_version
|
str | None
|
Release date in |
None
|
api_version
|
str
|
API version. |
API_VERSION
|
simplify
|
bool
|
If |
True
|
server
|
str | None
|
Server target. |
None
|
dataframe_type
|
Literal['pandas', 'polars']
|
|
'pandas'
|
Returns:
| Type | Description |
|---|---|
DataFrame | PIPResponse
|
A flat DataFrame of key indicators when simplify is |
DataFrame | PIPResponse
|
class: |
Raises:
| Type | Description |
|---|---|
PIPValidationError
|
If country is missing or is a list. |
Example
import povineq df = povineq.get_cp_ki(country="IDN")
Source code in src/povineq/country_profiles.py
unnest_ki(raw)
¶
Flatten nested key-indicator response into a single DataFrame.
Mirrors pipr::unnest_ki(). Extracts headcount, population, GNI,
GDP growth, MPM headcount, and shared prosperity tables from the nested
JSON structure and merges them on (country_code, reporting_year).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
dict | list
|
Parsed JSON from the |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
A flat :class: |
DataFrame
|
|
Example
import povineq df = povineq.get_cp_ki(country="IDN") # calls unnest_ki internally
Source code in src/povineq/country_profiles.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
check_api(api_version=API_VERSION, server=None)
¶
Test connectivity to the PIP API.
Hits the health-check endpoint and returns the parsed response.
Mirrors pipr::check_api().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_version
|
str
|
API version (only |
API_VERSION
|
server
|
str | None
|
Server target — |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict with the health-check response from the API. |
Raises:
| Type | Description |
|---|---|
PIPConnectionError
|
If the network is unreachable. |
PIPAPIError
|
If the health-check endpoint returns an error. |
Example
import povineq status = povineq.check_api()
Source code in src/povineq/info.py
get_pip_info(api_version=API_VERSION, server=None)
¶
Get metadata about the PIP API.
Mirrors pipr::get_pip_info().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_version
|
str
|
API version (only |
API_VERSION
|
server
|
str | None
|
Server target. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict with API metadata (version, endpoints, etc.). |
Example
import povineq info = povineq.get_pip_info()
Source code in src/povineq/info.py
get_versions(api_version=API_VERSION, server=None, simplify=True, dataframe_type='pandas')
¶
List available data versions.
Mirrors pipr::get_versions().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_version
|
str
|
API version (only |
API_VERSION
|
server
|
str | None
|
Server target. |
None
|
simplify
|
bool
|
If |
True
|
dataframe_type
|
Literal['pandas', 'polars']
|
|
'pandas'
|
Returns:
| Type | Description |
|---|---|
DataFrame | dict | list
|
A DataFrame of available versions when simplify is |
DataFrame | dict | list
|
or the raw dict/list otherwise. |
Example
import povineq df = povineq.get_versions()
Source code in src/povineq/info.py
get_agg(year='all', povline=None, version=None, ppp_version=None, release_version=None, aggregate=None, api_version=API_VERSION, fmt='json', simplify=True, server=None, dataframe_type='pandas')
¶
Get custom aggregate statistics (FCV, regional, vintage, etc.).
Mirrors pipr::get_agg().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
year
|
str | int | list[int]
|
Year(s) or |
'all'
|
povline
|
float | None
|
Poverty line in 2017 PPP USD per day. |
None
|
version
|
str | None
|
Data version string. |
None
|
ppp_version
|
int | None
|
PPP base year. |
None
|
release_version
|
str | None
|
Release date in |
None
|
aggregate
|
str | None
|
Aggregate name (e.g. |
None
|
api_version
|
str
|
API version. |
API_VERSION
|
fmt
|
str
|
Response format — |
'json'
|
simplify
|
bool
|
If |
True
|
server
|
str | None
|
Server target. |
None
|
dataframe_type
|
Literal['pandas', 'polars']
|
|
'pandas'
|
Returns:
| Type | Description |
|---|---|
DataFrame | PIPResponse
|
A DataFrame of custom aggregate statistics. |
Example
import povineq df = povineq.get_agg(aggregate="fcv", server="qa")
Source code in src/povineq/stats.py
get_stats(country='all', year='all', povline=None, popshare=None, fill_gaps=False, nowcast=False, subgroup=None, welfare_type='all', reporting_level='all', version=None, ppp_version=None, release_version=None, api_version=API_VERSION, fmt='arrow', simplify=True, server=None, dataframe_type='pandas')
¶
Get poverty and inequality statistics from the PIP API.
This is the primary function for querying household survey-based poverty
and inequality estimates. It mirrors pipr::get_stats().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country
|
str | list[str]
|
ISO3 country code(s) or |
'all'
|
year
|
str | int | list[int]
|
Survey year(s) or |
'all'
|
povline
|
float | None
|
Poverty line in 2017 PPP USD per day. |
None
|
popshare
|
float | None
|
Proportion of the population below the poverty line. When set, povline is ignored. |
None
|
fill_gaps
|
bool
|
If |
False
|
nowcast
|
bool
|
If |
False
|
subgroup
|
str | None
|
Pre-defined aggregation. Either |
None
|
welfare_type
|
str
|
Welfare concept — |
'all'
|
reporting_level
|
str
|
Geographic level — |
'all'
|
version
|
str | None
|
Data version string (see :func: |
None
|
ppp_version
|
int | None
|
PPP base year. |
None
|
release_version
|
str | None
|
Release date in |
None
|
api_version
|
str
|
API version (only |
API_VERSION
|
fmt
|
str
|
Response format — |
'arrow'
|
simplify
|
bool
|
If |
True
|
server
|
str | None
|
Server target — |
None
|
dataframe_type
|
Literal['pandas', 'polars']
|
|
'pandas'
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
DataFrame | PIPResponse
|
class: |
DataFrame | PIPResponse
|
class: |
Raises:
| Type | Description |
|---|---|
PIPValidationError
|
If parameter values are invalid. |
PIPAPIError
|
If the API returns a structured error response. |
PIPRateLimitError
|
If the rate limit is exceeded after retries. |
PIPConnectionError
|
If the network is unreachable. |
Example
import povineq df = povineq.get_stats(country="AGO", year=2000) df = povineq.get_stats(country="all", year="all", fill_gaps=True) df = povineq.get_stats(country="all", subgroup="wb_regions")
Source code in src/povineq/stats.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
get_wb(year='all', povline=None, version=None, ppp_version=None, release_version=None, api_version=API_VERSION, fmt='json', simplify=True, server=None, dataframe_type='pandas')
¶
Get World Bank regional and global aggregate statistics.
Shorthand for get_stats(subgroup="wb_regions").
Mirrors pipr::get_wb().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
year
|
str | int | list[int]
|
Year(s) or |
'all'
|
povline
|
float | None
|
Poverty line in 2017 PPP USD per day. |
None
|
version
|
str | None
|
Data version string. |
None
|
ppp_version
|
int | None
|
PPP base year. |
None
|
release_version
|
str | None
|
Release date in |
None
|
api_version
|
str
|
API version. |
API_VERSION
|
fmt
|
str
|
Response format — |
'json'
|
simplify
|
bool
|
If |
True
|
server
|
str | None
|
Server target. |
None
|
dataframe_type
|
Literal['pandas', 'polars']
|
|
'pandas'
|
Returns:
| Type | Description |
|---|---|
DataFrame | PIPResponse
|
A DataFrame of WB regional/global aggregates. |
Example
import povineq df = povineq.get_wb()
Source code in src/povineq/stats.py
change_grouped_stats_to_csv(df)
¶
Expand a deciles list column into individual decile1--decileN columns.
Mirrors pipr::change_grouped_stats_to_csv(). When the PIP API returns
grouped statistics in JSON or RDS format, the decile values are packed into
a single "deciles" column whose cells contain lists. This function
unpacks those lists into separate columns and drops the original list column.
If the DataFrame has no "deciles" column, it is returned unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame potentially containing a |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with individual |
DataFrame
|
replacing the |
Example
import pandas as pd from povineq.utils import change_grouped_stats_to_csv df = pd.DataFrame({"country": ["ALB"], "deciles": [[0.1, 0.2]]}) change_grouped_stats_to_csv(df) country decile1 decile2 0 ALB 0.1 0.2