Hello I am trying to download all data for a DE_GROUP for a specific org level but I keep getting the same error.
dhis2.exceptions.RequestException: code: 409, url: https://sl.dhis2.org/hmis23/api/analytics?dimension=pe:LAST_MONTH&dimension=dx:DE_GROUP-j3ghXQXN6
5o&dimension=ou:LEVEL-6;.json, description: {"httpStatus":"Conflict","httpStatusCode":409,"status":"ERROR","message":"Data elements must be of a value and aggregation type that allow aggregation: `[lBYF7dshoF3]`","errorCode":"E7115"}
I recieve the same error if I try and download the data directly through the Data Visualizer.
Here is my code
# Import necessary libraries and modules
from dhis2 import Api # API class for connecting to DHIS2
from get_data import get_credentials # Function to get username and password
from io import StringIO # StringIO to handle string-based data as file-like objects
from tqdm import tqdm # tqdm for progress bars (not used here, but typically useful for loops)
from get_org_hier import get_org_heir # Custom module for getting organization hierarchy (spelling 'heir' likely should be 'hier')
import pandas as pd # pandas for data manipulation and analysis
import json # json for handling JSON responses (not used here)
import re # re for regular expressions (not used here)
import os # os for interacting with the operating system (not used here)
# Step 1: Get credentials (username and password) to authenticate the API connection
username, password = get_credentials()
# Step 2: Initialize the API connection to DHIS2 using the given credentials
api = Api('https://sl.dhis2.org/hmis23', username, password)
# Step 3: Define the time period, data element, and organizational level for data extraction
period = 'LAST_MONTH' # Define the time period as the last month
data_element = 'j3ghXQXN65o' # Define the data element group ID (from DHIS2)
org = 'LEVEL-6' # Specify organizational unit level (e.g., level-6 in the hierarchy)
# Step 4: Build the query string for DHIS2 analytics API to retrieve the required data
query_string = (
f'analytics?dimension=pe:{period}' # Add period dimension
f'&dimension=dx:DE_GROUP-{data_element}' # Add data element group dimension
f'&dimension=ou:{org};' # Add organizational unit dimension
)
# Step 5: Fetch the data from DHIS2 API using the constructed query string
response = api.get(query_string)
# Step 6: Load the API response into a pandas DataFrame
# The response text is assumed to be in CSV format, so we use StringIO to treat it as a file-like object
df = pd.read_csv(StringIO(response.text))
# Step 7: Display the DataFrame's column names
print(df.columns)
# Step 8: Print the entire DataFrame to inspect the fetched data
print(df)
Thank you for your help.
7 posts - 2 participants