@@ -4,7 +4,7 @@ jupytext:
44    extension : .md 
55    format_name : myst 
66    format_version : 0.13 
7-     jupytext_version : 1.16.1  
7+     jupytext_version : 1.16.7  
88kernelspec :
99  display_name : Python 3 (ipykernel) 
1010  language : python 
@@ -19,7 +19,7 @@ In addition to what's in Anaconda, this lecture will need the following librarie
1919``` {code-cell}  ipython3
2020:tags: [hide-output] 
2121
22- !pip install --upgrade yfinance pandas_datareader  
22+ !pip install --upgrade yfinance wbgapi  
2323``` 
2424
2525We use the following imports.
@@ -31,7 +31,7 @@ import yfinance as yf
3131import pandas as pd 
3232import statsmodels.api as sm 
3333
34- from pandas_datareader import  wb
34+ import wbgapi as  wb
3535from scipy.stats import norm, cauchy 
3636from pandas.plotting import register_matplotlib_converters 
3737register_matplotlib_converters() 
@@ -790,24 +790,21 @@ def empirical_ccdf(data,
790790:tags: [hide-input] 
791791
792792def extract_wb(varlist=['NY.GDP.MKTP.CD'],  
793-                c='all_countries ',  
793+                c='all ',  
794794               s=1900,  
795795               e=2021,  
796796               varnames=None): 
797-     if c == "all_countries": 
798-         # Keep countries only (no aggregated regions) 
799-         countries = wb.get_countries() 
800-         countries_name = countries[countries['region'] != 'Aggregates']['name'].values 
801-         c = "all" 
802797     
803-     df = wb.download(indicator=varlist, country=c, start=s, end=e).stack().unstack(0).reset_index() 
804-     df = df.drop(['level_1'], axis=1).transpose() 
798+     df = wb.data.DataFrame(varlist, economy=c, time=range(s, e+1, 1), skipAggs=True) 
799+     df.index.name = 'country' 
800+      
805801    if varnames is not None: 
806-         df.columns = varnames 
807-         df = df[1:] 
802+         df.columns = variable_names 
803+ 
804+     cntry_mapper = pd.DataFrame(wb.economy.info().items)[['id','value']].set_index('id').to_dict()['value'] 
805+     df.index = df.index.map(lambda x: cntry_mapper[x])  #map iso3c to name values 
808806     
809-     df1 =df[df.index.isin(countries_name)] 
810-     return df1 
807+     return df 
811808``` 
812809
813810### Firm size  
@@ -914,9 +911,9 @@ variable_code = ['NY.GDP.MKTP.CD', 'NY.GDP.PCAP.CD']
914911variable_names = ['GDP', 'GDP per capita'] 
915912
916913df_gdp1 = extract_wb(varlist=variable_code,  
917-                      c="all_countries ",  
918-                      s=" 2021" ,  
919-                      e=" 2021" ,  
914+                      c="all ",  
915+                      s=2021,  
916+                      e=2021,  
920917                     varnames=variable_names) 
921918df_gdp1.dropna(inplace=True) 
922919``` 
0 commit comments