def test_wdi_get_indicators(self): result1 = get_indicators() result2 = WorldBankReader().get_indicators() session = requests.Session() result3 = get_indicators(session=session) result4 = WorldBankReader(session=session).get_indicators() for result in [result1, result2, result3, result4]: exp_col = pd.Index(['id', 'name', 'source', 'sourceNote', 'sourceOrganization', 'topics']) # assert_index_equal doesn't exists self.assertTrue(result.columns.equals(exp_col)) self.assertTrue(len(result) > 10000)
def test_wdi_get_indicators(self): result1 = get_indicators() result2 = WorldBankReader().get_indicators() session = requests.Session() result3 = get_indicators(session=session) result4 = WorldBankReader(session=session).get_indicators() for result in [result1, result2, result3, result4]: exp_col = pd.Index(['id', 'name', 'source', 'sourceNote', 'sourceOrganization', 'topics']) # assert_index_equal doesn't exists self.assertTrue(result.columns.equals(exp_col)) self.assertTrue(len(result) > 10000)
def test_wdi_get_indicators(self): result1 = get_indicators() result2 = WorldBankReader().get_indicators() session = requests.Session() result3 = get_indicators(session=session) result4 = WorldBankReader(session=session).get_indicators() for result in [result1, result2, result3, result4]: exp_col = pd.Index([ "id", "name", "unit", "source", "sourceNote", "sourceOrganization", "topics", ]) # Column order is version dependent, so check columns are present assert sorted(result.columns) == sorted(exp_col) assert len(result) > 10000
def get_indi(symbols, ret): db_indi = pddr.get_indicators() # db_indi.to_csv('wb_indi.csv', sep=';') db_indi = db_indi.loc[db_indi['id'].isin(symbols)].rename( columns={ 'id': 'Code', 'name': 'Name', 'source': 'Dataset' }) db_indi['Freq'] = frequency db_indi['Start'] = ret['time'].min() db_indi['LastUpdateDate'] = dt.datetime.now() db_indi['LastUpdateCount'] = ret.shape[0] db_indi['MULT'] = 0 db_indi['Code'] = db_indi['Code'].str.replace('\.', '_') db_indi.set_index('Code') return db_indi
import pandas as pd from pandas_datareader import wb df = wb.get_indicators()[['id', 'name']] df = df[df.name == 'Individuals using the Internet (% of population)'] print(df)
def AS(P=P(), W=0, P_e=1, Z_2=0): return P - Z_2 def AD(P=P(), M=0, G=0, T=0, Z_1=0): return -P + Z_1 # - def findIntersection(fun1, fun2, x0): return fsolve(lambda x: fun1(x) - fun2(x), x0) indicators = wb.get_indicators() indicators.head() indicators.loc[indicators.id == 'NY.GDP.PETR.RT.ZS', :] countries = wb.get_countries() countries.head() # The World Bank data-portal offers a wide array of data points used to benchmark various interventions, economic growth, prosperity, education, and healthcare. This data is extensive but often inconsistent across countries or years. For this reason, it some cases, it is required to find appropriate proxies to capture Macroeconomic variables discussed in academia. For tracking the real cost of oil over time, we will use the reserve banks measure of 'oil rents (% of GDP)' which is defined as "... the difference between the value of crude oil production at regional prices and total costs of production". Data was sourced between 1970 to 2017 for use in the analysis in order to get a long run understanding of the events which led up to the crash of 1987 and the implication monetary policy and commodity prices may have on markets today. # # The graph below shows an accurate measure for the changing economic cost of oil experienced by particular countries over time. The graph details clear spikes in oil rents in both the US and UK, starting at around 1976, at the time of the first recession. These reach their peak for either country at around 1980 and 1985 for the US and UK, before restoring to long-run norms. Zooming and panning through the data, you can observe greater detail in these movements as they changed over time and better understand the events at the time of the crash. # + # %%opts Curve [width=800, height=450] oil = wb.download(indicator='NY.GDP.PETR.RT.ZS', country=['USA', 'GBR'],
from requests.exceptions import ConnectionError import pytest import pandas as pd from pyam import IamDataFrame, IAMC_IDX, read_worldbank from pyam.testing import assert_iamframe_equal from pandas_datareader import wb try: wb.get_indicators() WB_UNAVAILABLE = False except ConnectionError: WB_UNAVAILABLE = True WB_REASON = 'World Bank API unavailable' WB_DF = pd.DataFrame( [['foo', 'WDI', 'Canada', 'GDP', 'n/a', 39473.4, 40637.4, 42266.5], ['foo', 'WDI', 'Mexico', 'GDP', 'n/a', 17288.6, 17720.0, 17874.0], ['foo', 'WDI', 'United States', 'GDP', 'n/a', 51569.8, 53035.7, 54395.4]], columns=IAMC_IDX + [2003, 2004, 2005]) @pytest.mark.skipif(WB_UNAVAILABLE, reason=WB_REASON) def test_worldbank(): obs = read_worldbank(model='foo', indicator={'NY.GDP.PCAP.PP.KD': 'GDP'}) exp = IamDataFrame(WB_DF) assert_iamframe_equal(obs, exp)
def get_wb_indicators(country_codes=None, indicator_codes=None, start_year=2007, end_year=2017): """ Gets the World Bank indicators Args: country_codes ([str]): List of ISO3 country codes indicator_codes ([str]): List of World Bank Indicator Codes Returns: pd.DataFrame: DataFrame with the columns -ISO3 -Country -Year -Indicator ID -Indicator name -Indicator category -Unit (e.g. €, no. of people, percentage, etc.) -Value -Source (World Bank Data or IDMC) """ # Download data from the World Bank wb_data = wb.download(indicator=indicator_codes, country=country_codes, start=start_year, end=end_year) wb_data_reset = wb_data.reset_index() # Extract countries information wb_countries = wb.get_countries() wb_countries = wb_countries[["iso3c", "name"]] # Merge the aforementioned dataframes and remove duplicates merge = pd.merge(wb_data_reset, wb_countries, left_on='country', right_on="name") merge = merge.drop(columns=["name"]) # Narrow the resulting dataframe merge_narrow = merge.melt(id_vars=['country', 'year', 'iso3c'], var_name='indicatorID', value_name='value') # Extract indicators information wb_indicators = wb.get_indicators() # Merge the narrowed dataframe with the World Bank indicators merge2 = pd.merge(merge_narrow, wb_indicators, left_on='indicatorID', right_on="id") # Remove duplicates, clean, and organize the data merge2 = merge2.drop(columns=["id", "source", "sourceNote"]) merge2 = merge2.rename(index=str, columns={ "name": "indicatorName", "topics": "indicatorCategory", "sourceOrganization": "source" }) # Organize dataframe resulting_df = merge2[[ 'iso3c', 'country', 'year', 'indicatorID', 'indicatorName', 'indicatorCategory', 'unit', 'value', 'source' ]] return resulting_df