API Reference
DHSBaseAPI
dataclass
Base Class to fetch data from the DHS API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country_ids
|
list
|
List of country IDs to filter the data. |
list()
|
indicator_ids
|
list
|
List of indicator IDs to filter the data. |
list()
|
survey_ids
|
list
|
List of survey IDs to filter the data. |
list()
|
survey_year
|
list
|
List of survey years to filter the data. |
list()
|
survey_year_start
|
list
|
List of survey year start dates to filter the data. |
list()
|
survey_year_end
|
list
|
List of survey year end dates to filter the data. |
list()
|
survey_type
|
list
|
List of survey types to filter the data. |
list()
|
survey_characteristics_ids
|
list
|
List of survey characteristics IDs to filter the data. |
list()
|
tagIds
|
list
|
List of tag IDs to filter the data. |
list()
|
filter_fields
|
list
|
List of fields to filter the data. |
list()
|
Source code in pdhs/base_api.py
10 11 12 13 14 15 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 | |
__post_init__()
Construct the API URL after initialization.
Source code in pdhs/base_api.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
get_data()
Public method to return the final processed DataFrame.
Returns:
| Type | Description |
|---|---|
Optional[DataFrame]
|
Optional[pl.DataFrame]: Polars DataFrame or None if an error occurs. |
Source code in pdhs/base_api.py
117 118 119 120 121 122 123 124 125 126 127 128 | |
GetCountries
dataclass
Bases: DHSBaseAPI
Class to fetch country data from the DHS API.
Inherits from DHSBaseAPI and uses the 'countries' endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country_ids
|
list
|
List of country IDs to filter the data. |
list()
|
indicator_ids
|
list
|
List of indicator IDs to filter the data. |
list()
|
survey_ids
|
list
|
List of survey IDs to filter the data. |
list()
|
survey_year
|
list
|
List of survey years to filter the data. |
list()
|
survey_year_start
|
list
|
List of survey year start dates to filter the data. |
list()
|
survey_year_end
|
list
|
List of survey year end dates to filter the data. |
list()
|
survey_type
|
list
|
List of survey types to filter the data. |
list()
|
survey_characteristics_ids
|
list
|
List of survey characteristics IDs to filter the data. |
list()
|
tagIds
|
list
|
List of tag IDs to filter the data. |
list()
|
filter_fields
|
list
|
List of fields to filter the data. |
list()
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A polars DataFrame containing the country data. |
Example:
from pdhs.countries import GetCountries
countries_data = GetCountries(country_ids = ["AL"])
df = countries_data.get_data()
print(df)
Source code in pdhs/countries.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
GetDatasets
dataclass
Bases: DHSBaseAPI
Class to fetch datasets from the DHS API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country_ids
|
list
|
List of country IDs to filter the data. |
list()
|
indicator_ids
|
list
|
List of indicator IDs to filter the data. |
list()
|
survey_ids
|
list
|
List of survey IDs to filter the data. |
list()
|
survey_year
|
list
|
List of survey years to filter the data. |
list()
|
survey_year_start
|
list
|
List of survey year start dates to filter the data. |
list()
|
survey_year_end
|
list
|
List of survey year end dates to filter the data. |
list()
|
survey_type
|
list
|
List of survey types to filter the data. |
list()
|
survey_characteristics_ids
|
list
|
List of survey characteristics IDs to filter the data. |
list()
|
tagIds
|
list
|
List of tag IDs to filter the data. |
list()
|
filter_fields
|
list
|
List of fields to filter the data. |
list()
|
select_surveys
|
str
|
Comma-separated list of survey IDs to select. |
None
|
file_format
|
str
|
Format of the files to retrieve (e.g., "DT" for data tables). |
None
|
file_type
|
str
|
Type of the files to retrieve (e.g., "CSV", "JSON"). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A polars DataFrame containing the dataset information. |
Example:
from pdhs.datasets import GetDatasets
indicators_data = GetDatasets(country_ids = ["NG"], file_format = "DT")
df = indicators_data.get_data()
print(df)
Source code in pdhs/datasets.py
10 11 12 13 14 15 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 | |
DHSDownloader
dataclass
A class to handle downloading datasets from the Demographic and Health Surveys (DHS) Program.
This class provides methods to authenticate with the DHS API, search for available datasets, and download selected datasets to a specified directory.
Requires Playwright for browser automation and requests for HTTP requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
str
|
DHS API username. |
required |
password
|
str
|
DHS API password. |
required |
download_dir
|
str
|
Directory where datasets will be saved. |
required |
project_name
|
str
|
Name of the project to select from the DHS dropdown. |
required |
dataframe
|
DataFrame
|
Polars DataFrame containing dataset metadata. |
required |
Methods:
| Name | Description |
|---|---|
download_all_datasets |
list): Downloads all datasets specified by their IDs. |
load_dataset_as_dataframe |
str): Loads a downloaded dataset into a Polars DataFrame. |
Example:
from pdhs.download import DHSDownloader
downloader = DHSDownloader(
email="example@email.com",
password="your_password",
project_name="Your Project Name",
dataframe=GetDatasets(country_ids=["NG"], file_format="DT").get_data()
Source code in pdhs/download.py
14 15 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 138 139 140 141 142 143 144 145 146 147 148 149 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 | |
download_all_datasets(dataset_ids)
async
Iterates over the provided dataset IDs and downloads each dataset. Args: dataset_ids (list): List of dataset IDs to download.
Source code in pdhs/download.py
56 57 58 59 60 61 62 63 64 65 66 | |
load_dataset_as_dataframe(dataset_id)
Loads a downloaded dataset into a Polars DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_id
|
str
|
The ID of the dataset to load. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pl.DataFrame: The dataset loaded as a Polars DataFrame. |
Source code in pdhs/download.py
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 | |
GetGeometry
dataclass
Bases: DHSBaseAPI
Class to fetch geometry data from the DHS API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country_ids
|
list
|
List of country IDs to filter the data. |
list()
|
indicator_ids
|
list
|
List of indicator IDs to filter the data. |
list()
|
survey_ids
|
list
|
List of survey IDs to filter the data. |
list()
|
survey_year
|
list
|
List of survey years to filter the data. |
list()
|
survey_year_start
|
list
|
List of survey year start dates to filter the data. |
list()
|
survey_year_end
|
list
|
List of survey year end dates to filter the data. |
list()
|
survey_type
|
list
|
List of survey types to filter the data. |
list()
|
survey_characteristics_ids
|
list
|
List of survey characteristics IDs to filter the data. |
list()
|
tagIds
|
list
|
List of tag IDs to filter the data. |
list()
|
filter_fields
|
list
|
List of fields to filter the data. |
list()
|
country_ids
|
list
|
List of country IDs to filter the data. |
list()
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A polars DataFrame containing the geometry data. |
Example: ```python from pdhs.geometry import GetGeometry geometry_data = GetGeometry(country_ids = ["AL"]) df = geometry_data.get_data() print(df)
```
Source code in pdhs/geometry.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
GetIndicatorsData
dataclass
Bases: DHSBaseAPI
Class to fetch indicators data from the DHS API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country_ids
|
list
|
List of country IDs to filter the data. |
list()
|
indicator_ids
|
list
|
List of indicator IDs to filter the data. |
list()
|
survey_ids
|
list
|
List of survey IDs to filter the data. |
list()
|
survey_year
|
list
|
List of survey years to filter the data. |
list()
|
survey_year_start
|
list
|
List of survey year start dates to filter the data. |
list()
|
survey_year_end
|
list
|
List of survey year end dates to filter the data. |
list()
|
survey_type
|
list
|
List of survey types to filter the data. |
list()
|
survey_characteristics_ids
|
list
|
List of survey characteristics IDs to filter the data. |
list()
|
tagIds
|
list
|
List of tag IDs to filter the data. |
list()
|
characteristic_category
|
list
|
List of characteristic categories to filter the data. |
list()
|
characteristic_label
|
list
|
List of characteristic labels to filter the data. |
list()
|
breakdown
|
str
|
Breakdown type for the indicators. |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A polars DataFrame containing the indicators data. |
Example:
from pdhs.indicators import GetIndicatorsData
indicators_data = GetIndicatorsData(
country_ids=["AL"],
characteristic_category=["wealth quintile", "region"],
characteristic_label=["middle", "second"],
breakdown="all"
)
df = indicators_data.get_data()
print(df)
Source code in pdhs/indicators.py
10 11 12 13 14 15 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 | |
GetInfo
dataclass
Bases: DHSBaseAPI
Class to fetch information from the DHS API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info_type
|
str
|
Type of information to retrieve (e.g., "version", "citation"). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A polars DataFrame containing the requested information. |
Example:
from pdhs.info import GetInfo
get_info = GetInfo(info_type="citation")
df = get_info.get_data()
print(df)
Source code in pdhs/info.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
GetPublications
dataclass
Bases: DHSBaseAPI
Class to fetch publications from the DHS API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country_ids
|
list
|
List of country IDs to filter the data. |
list()
|
indicator_ids
|
list
|
List of indicator IDs to filter the data. |
list()
|
survey_ids
|
list
|
List of survey IDs to filter the data. |
list()
|
survey_year
|
list
|
List of survey years to filter the data. |
list()
|
survey_year_start
|
list
|
List of survey year start dates to filter the data. |
list()
|
survey_year_end
|
list
|
List of survey year end dates to filter the data. |
list()
|
survey_type
|
list
|
List of survey types to filter the data. |
list()
|
survey_characteristics_ids
|
list
|
List of survey characteristics IDs to filter the data. |
list()
|
tagIds
|
list
|
List of tag IDs to filter the data. |
list()
|
filter_fields
|
list
|
List of fields to filter the data. |
list()
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A polars DataFrame containing the publications data. |
Example:
from pdhs.publications import GetPublications
get_publications = GetPublications(country_ids=["AL"])
df = get_publications.get_data()
print(df)
Source code in pdhs/publications.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
GetSurveys
dataclass
Bases: DHSBaseAPI
Class to fetch survey data from the DHS API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country_ids
|
list
|
List of country IDs to filter the data. |
list()
|
survey_status
|
str
|
Status of the surveys to filter (e.g., "completed", "ongoing"). |
None
|
indicator_ids
|
list
|
List of indicator IDs to filter the data. |
list()
|
survey_ids
|
list
|
List of survey IDs to filter the data. |
list()
|
survey_year
|
list
|
List of survey years to filter the data. |
list()
|
survey_year_start
|
list
|
List of survey year start dates to filter the data. |
list()
|
survey_year_end
|
list
|
List of survey year end dates to filter the data. |
list()
|
survey_type
|
list
|
List of survey types to filter the data. |
list()
|
survey_characteristics_ids
|
list
|
List of survey characteristics IDs to filter the data. |
list()
|
tagIds
|
list
|
List of tag IDs to filter the data. |
list()
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A polars DataFrame containing the survey data. |
Example:
from pdhs.surveys import GetSurveys
survey_data = GetSurveys(
country_ids=["NG"],
survey_status="completed",
)
df = survey_data.get_data()
print(df)
Source code in pdhs/surveys.py
6 7 8 9 10 11 12 13 14 15 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 | |
GetTags
dataclass
Bases: DHSBaseAPI
Class to fetch tags from the DHS API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indicator_ids
|
list
|
List of indicator IDs to filter the data. |
list()
|
survey_ids
|
list
|
List of survey IDs to filter the data. |
list()
|
survey_year
|
list
|
List of survey years to filter the data. |
list()
|
survey_year_start
|
list
|
List of survey year start dates to filter the data. |
list()
|
survey_year_end
|
list
|
List of survey year end dates to filter the data. |
list()
|
survey_type
|
list
|
List of survey types to filter the data. |
list()
|
survey_characteristics_ids
|
list
|
List of survey characteristics IDs to filter the data. |
list()
|
tagIds
|
list
|
List of tag IDs to filter the data. |
list()
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A polars DataFrame containing the tags data. |
Example:
from pdhs.tags import GetTags
Tags_data = GetTags(indicator_ids=["FE_FRTR_W_TFR"])
df = Tags_data.get_data()
print(df)
Source code in pdhs/tags.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
GetDataUpdates
dataclass
Bases: DHSBaseAPI
Class to fetch data updates from the DHS API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
last_update
|
str
|
The date of the last update in YYYYMMDD format. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A polars DataFrame containing the data updates. |
Example:
from pdhs.updates import GetDataUpdates
data_update = GetDataUpdates(last_update="20150901")
df = data_update.get_data()
print(df)
Source code in pdhs/updates.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
GetUIUpdates
dataclass
Bases: DHSBaseAPI
Class to fetch UI updates from the DHS API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
last_update
|
str
|
The date of the last update in YYYYMMDD format. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A polars DataFrame containing the UI updates. |
Example
ui_update = GetUIUpdates(last_update="20150901") df2 = ui_update.get_data() print(df2)
Source code in pdhs/updates.py
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 | |