Skip to content

Add fetch_available_statistical_variables #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025

Conversation

jm-rivera
Copy link
Contributor

@jm-rivera jm-rivera commented Mar 31, 2025

This PR is part of a group of PRs which will bring some key features from the Data Commons website API to the client library (e.g #230, #231).

Fetch available statistical variables: this is an implementation of this feature of the DC website.

In short, this PR:

  • Adds a fetch_available_statistical_variables method to the ObservationEndpoint. This method fetches all statvars which have observations for one or more entities.

It also includes tests for the method and a helper function which structures the data by entity : statvar_list.

Example usage:

from datacommons_client import DataCommonsClient

dc = DataCommonsClient(dc_instance="datacommons.one.org")

variables = dc.observation.fetch_available_statistical_variables(
    entity_dcids=["africa", "country/TGO"]
)

{'africa': ['Area_FloodEvent',
            'eia/INTL.7-1-TJ.A',
            'Annual_Emissions_CarbonDioxideEquivalent100YearGlobalWarmingPotential_SteelManufacturing',
            'eia/INTL.35-12-QBTU.A',
            'sdg/EN_MAT_DOMCMPG.PRODUCT--MF421',
            'sdg/EN_MAT_DOMCMPG.PRODUCT--MF21',
            'sdg/SG_DMK_PARLCC_LC.AGE--Y0T45__SEX--M__PARLIAMENTARY_COMMITTEES--PC_DEFENCE',
            'eia/INTL.65-2-QBTU.A',
            'eia/INTL.57-1-MT.A',
            'eia/INTL.68-2-MT.A',
            ...
],
 'country/TGO': ['sdg/SG_GEN_EQPWN',
                 'Count_Person_25OrMoreYears_Female_MastersDegreeOrHigher_AsFractionOf_Count_Person_25OrMoreYears_Female',
                 'sdg/SE_ADT_ACTS.TYPE_OF_SKILL--SKILL_ICTPST',
                 'Amount_Debt_GBP_LenderArabBankforEconomicDevinAfrica_AsAFractionOf_Amount_Debt_LenderArabBankforEconomicDevinAfrica',
                 'worldBank/UIS_R_2_GPV_G2',
                 'worldBank/UIS_GTVP_2_GPV_F',
                 'DiffRelativeToAvg_1980_2010_MaxTemp_Daily_Hist_5PctProb_Greater_Atleast1DayADecade_CMIP6_Ensemble_SSP245',
                 'MaxTemp_Daily_Hist_50PctProb_Greater_Atleast1DayAYear_CMIP6_HADGEM3-GC31-LL_SSP585',
                 'AmountPrincipalRepayment_Debt_LongTermExternalDebt_LenderEuropeanEconomicCommunity',
                 'worldBank/SE_SEC_NENR',
                 ...
]}

Note
Looking through the website code I discovered that it was possible for variable.dcids to be empty. Keeping them empty is what enables getting all the StatVars with observations. However, empty variable.dcids contradicts the official docs which say its required. FYI @kmoscoe.

Including tests for `fetch_available_statistical_variables` and the helper `group_variables_by_entity`
@jm-rivera jm-rivera requested review from dwnoble and keyurva March 31, 2025 07:43
@jm-rivera jm-rivera self-assigned this Mar 31, 2025
@kmoscoe
Copy link
Contributor

kmoscoe commented Mar 31, 2025 via email

@kmoscoe
Copy link
Contributor

kmoscoe commented Mar 31, 2025

Note Looking through the website code I discovered that it was possible for variable.dcids to be empty. Keeping them empty is what enables getting all the StatVars with observations. However, empty variable.dcids contradicts the official docs which say its required. FYI @kmoscoe.

Wow, that is amazing, never knew it. Will fix the REST docs today!

Copy link
Contributor

@dwnoble dwnoble left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this addition Jorge! This will be helpful when we introduce the new client library into the website

@jm-rivera jm-rivera merged commit 0cbfd8c into datacommonsorg:master Apr 1, 2025
2 checks passed
@jm-rivera jm-rivera deleted the add-fetch-statvars branch April 1, 2025 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants