Site
owid.site ¶
Tools for fetching data and configurations from the OWID website.
This module provides functions to interact with Our World in Data's live website and API endpoints. You can:
- Fetch chart configurations to understand how charts are structured
- Download chart data directly as pandas DataFrames
- Access OWID's variable data API for custom analysis
The main use case is retrieving published OWID charts for exploration, reproduction, or modification in Jupyter notebooks.
Example
API Endpoints
- Chart pages: https://ourworldindata.org/grapher/{slug}
- Variable data: https://api.ourworldindata.org/v1/indicators/{id}.data.json
- Variable metadata: https://api.ourworldindata.org/v1/indicators/{id}.metadata.json
get_chart_config ¶
Extract the chart configuration from an OWID chart page.
Fetches an OWID chart page and extracts the embedded JSON configuration that defines the chart's appearance, data sources, and behavior. This config can be used to understand chart settings or to fetch the underlying data.
| PARAMETER | DESCRIPTION |
|---|---|
url
|
Full URL to an OWID chart page (must start with 'https://ourworldindata.org/grapher/' unless force=True).
TYPE:
|
force
|
If True, skips URL validation. Useful for testing or non-standard URLs. Default is False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Dictionary containing the complete chart configuration, including: |
|
|
|
|
|
|
|
|
|
|
|
|
| RAISES | DESCRIPTION |
|---|---|
|
If the URL doesn't start with GRAPHER_PREFIX and force=False. |
|
If the HTTP request fails (non-200 status code). |
Example
Source code in owid/site/__init__.py
get_chart_data ¶
Fetch data from an OWID chart as a pandas DataFrame.
This is the primary function for retrieving chart data. It handles the complete pipeline: fetching the chart config, downloading variable data from the API, and converting it to a clean pandas DataFrame ready for analysis or visualization.
You can specify either a full URL or just the chart slug (the part after /grapher/).
| PARAMETER | DESCRIPTION |
|---|---|
url
|
Full URL to an OWID chart. Example: 'https://ourworldindata.org/grapher/life-expectancy'
TYPE:
|
slug
|
Chart slug (URL suffix). Example: 'life-expectancy'. Use this as a shorthand instead of the full URL.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
DataFrame in long format with the following structure: |
|
For yearly data: |
|
|
|
|
|
|
|
|
|
For date-based data: |
|
|
|
|
|
|
|
|
| RAISES | DESCRIPTION |
|---|---|
|
If neither url nor slug is provided. |
|
If the chart URL is invalid or data fetching fails. |
Example
Note
For charts with multiple variables, the DataFrame will contain all variables stacked in long format. Use df.variable.unique() to see available variables.
Source code in owid/site/__init__.py
owid_data_to_frame ¶
Convert OWID's internal data format to a pandas DataFrame.
Takes the raw data structure returned by OWID's API (as retrieved by get_owid_data) and transforms it into a clean, analysis-ready DataFrame. This includes: - Mapping entity IDs to human-readable names - Converting year indices to actual years or dates - Stacking multiple variables into long format - Handling date-based data (yearIsDay format)
| PARAMETER | DESCRIPTION |
|---|---|
owid_data
|
Dictionary mapping variable IDs to their data. Each variable contains arrays of years, entity IDs, and values, plus metadata. This is the structure returned by get_owid_data().
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
DataFrame in long format. For yearly data, columns are [year, entity, variable, value]. |
|
For date-based data, columns are [date, entity, variable, value]. |
Example
from owid.site import get_chart_config, get_owid_data, owid_data_to_frame
config = get_chart_config(
url='https://ourworldindata.org/grapher/life-expectancy'
)
owid_data = get_owid_data(config)
df = owid_data_to_frame(owid_data)
print(df.head())
# year entity variable value
# 0 1950 Afghanistan Life expectancy 28.0
# 1 1951 Afghanistan Life expectancy 28.4
Note
This is a lower-level function. Most users should use get_chart_data() which calls this internally.
Source code in owid/site/__init__.py
get_owid_data ¶
Fetch raw variable data from OWID's API.
Downloads the underlying data for all variables referenced in a chart configuration. This retrieves the actual time series data (years, entity IDs, values) but does not include entity names or perform any transformations. Use owid_data_to_frame() to convert this to a usable DataFrame.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Chart configuration dictionary as returned by get_chart_config(). Must contain a 'dimensions' key with variableId values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Dictionary mapping variable IDs (int) to their data dictionaries. Each |
|
variable data dict contains: |
|
|
|
|
|
|
|
|
|
The three arrays are parallel - same index corresponds to a single observation. |
Example
from owid.site import get_chart_config, get_owid_data
config = get_chart_config(
url='https://ourworldindata.org/grapher/life-expectancy'
)
owid_data = get_owid_data(config)
# Inspect the structure
for var_id, data in owid_data.items():
print(f"Variable {var_id}:")
print(f" {len(data['years'])} observations")
print(f" Years: {min(data['years'])} to {max(data['years'])}")
Note
This is a lower-level function. Most users should use get_chart_data() which handles the full pipeline including data transformation.