-
Notifications
You must be signed in to change notification settings - Fork 1
/
OECDData.py
785 lines (633 loc) · 35.5 KB
/
OECDData.py
1
2
3
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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 2 12:29:41 2022
Functions to download data from OECD. Please consult documentation of individual functions below for further information.
@author: Lars E. Spreng
"""
import pandas as pd
from lxml import etree
import requests as rq
from functools import reduce
import xmltodict
from datetime import datetime
def get_var_codes_MEIArchive():
url = "https://stats.oecd.org/restsdmx/sdmx.ashx/GetDataStructure/MEI_ARCHIVE"
resp = rq.get(url)
doc = etree.fromstring(resp.content)
root ="{http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message}CodeLists/*[@id='CL_MEI_ARCHIVE_VAR']/"
var_list = doc.findall(root)
var_code = [int(var_list[i].get('value')) for i in range(2,len(var_list))]
root = "{http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure}Description"
var_description = [var_list[i].findall(root)[0].text for i in range(2,len(var_list))]
return var_code, var_description
def get_country_codes_MEIArchive():
url = "https://stats.oecd.org/restsdmx/sdmx.ashx/GetDataStructure/MEI_ARCHIVE"
resp = rq.get(url)
doc = etree.fromstring(resp.content)
root ="{http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message}CodeLists/*[@id='CL_MEI_ARCHIVE_LOCATION']/"
country_list = doc.findall(root)
country_code = [country_list[i].get('value') for i in range(2,len(country_list))]
return country_code
def get_series_first_release_MEIArchive(country_list, variable_list, frequency, startDate, endDate, startEDI, endEDI):
# Request data from OECD API and return pandas DataFrame
# =============== INPUT
# country_list: list of countries
# variable_list: list of variabes
# frequency: 'M' for monthly and 'Q' for quarterly time series
# startDate: date in YYYY-MM (2000-01) or YYYY-QQ (2000-Q1) format, None for all observations
# endDate: date in YYYY-MM (2000-01) or YYYY-QQ (2000-Q1) format, None for all observations
# startEDI: Edition of data, i.e. when it was published in YYYYMM format
# endEDI: Final edition in YYYYMM format
# =============== RAW DATA STRUCTURE
# The dataset has a total of M series which are identified through four keys in the following format: 0:0:0:0
# Position 1: Country
# Position 2: Variable
# Position 3: Edition of Data
# Position 4: Frequency
# Each series contains n observations for each time period, identified through a number t
# For example, for country "GBR", variable "201" with frequency M, between 1999-01 to 1999-12 the series for
# edition 202201 contains 12 observations. The series for edition 1999-03 will contain maximum 3 observations.
# it is possible that t is not a consecutive series of values in which case observations are missing.
# Code accounts for differences in length of time series.
# Real time data is extracted as the observations in the first published edition.
# ============= Create URL
url_base = "https://stats.oecd.org/sdmx-json/data/MEI_ARCHIVE/"
if isinstance(variable_list,list) == True:
if len(variable_list) == 1:
variable_str = str(variable_list[0])
else:
variable_str = '+'.join(str(x) for x in variable_list)
else:
variable_str = str(variable_list)
if isinstance(country_list,list) == True:
N = len(country_list)
if len(country_list) == 1:
country_str = country_list[0]
else:
country_str = '+'.join(str(x) for x in country_list)
else:
N = 1;
country_str = country_list
startTime = "startTime=" + startDate
endTime = "endTime=" + endDate
if startEDI == [] and endEDI == []:
if float(startDate.replace('-','')) >= 199902:
edition_dates = pd.date_range(datetime.strptime(startDate, '%Y-%m'),datetime.now(),freq='m').strftime('%Y%m')
else:
edition_dates = pd.date_range(datetime.strptime('1999-02', '%Y-%m'),datetime.now(),freq='m').strftime('%Y%m')
elif endEDI == []:
edition_dates = pd.date_range(datetime.strptime(startEDI, '%Y-%m'),datetime.now(),freq='m').strftime('%Y%m')
elif startEDI == []:
edition_dates = pd.date_range(datetime.strptime(startDate, '%Y-%m'),datetime.strptime(endEDI, '%Y-%m'),freq='m').strftime('%Y%m')
else:
edition_dates = pd.date_range(datetime.strptime(startEDI, '%Y-%m'),datetime.strptime(endEDI, '%Y-%m'),freq='m').strftime('%Y%m')
edition_str = '+'.join(str(x) for x in edition_dates)
url = url_base + country_str + "." + variable_str + "." + edition_str + "." + frequency + "/all?" + startTime + "&" + endTime
# ============= Download Data
response = rq.get(url = url, params = {})
if (response.status_code == 200):
responseJson = response.json()
# Get list of observations. This includes all revision to variables, not just real time vintages
series = responseJson.get('dataSets')[0].get('series')
filterKeys = lambda k: {x: series[x] for x in k}
if (len(series) > 0):
# Countries in dataset
temp = responseJson.get('structure').get('dimensions').get('series')[0].get('values')
countries = [temp[i].get('id') for i in range(len(temp))]
# All available time periods. Does NOT necessarily equal all time periods per country
temp = responseJson.get('structure').get('dimensions').get('observation')[0].get('values')
dates = [temp[i].get('id') for i in range(len(temp))]
# Remove wrong frequency (sometimes in there by accident)
if frequency == 'M':
dates = [item for item in dates if "Q" not in item]
elif frequency == 'Q':
dates = [item for item in dates if "M" not in item]
# All editions in dataset
temp = responseJson.get('structure').get('dimensions').get('series')[2].get('values')
editions = [temp[i].get('id') for i in range(len(temp))]
# Units of variables
units = responseJson.get('structure').get('attributes').get('series')[1].get('values')
if N == 1:
temp = list(series.values())
tempObs = [temp[i].get('observations') for i in range(len(temp))]
tempKeys = [list(tempObs[i].keys()) for i in range(len(tempObs))]
realObs = []
for i in range(len(tempObs)):
if i == 0:
realObs.extend([tempObs[0][j][0] for j in set(tempKeys[0])])
else:
newKey = set(tempKeys[i]) - set().union(*tempKeys[0:i]);
if len(newKey) > 0:
realObs.extend([tempObs[i][j][0] for j in newKey])
df = pd.DataFrame(dates)
df[countries[0]] = realObs
return df
elif len(countries) > 1:
# Get all keys
key = list(series.keys())
key_id = [key[i].split(':')[0] for i in range(len(key))]
# Create empty dataframe with all dates as index
df = pd.DataFrame(dates)
df.set_index(0, inplace=True)
for j in range(len(countries)):
# Get series per country
splitKeys = [key[i] for i in range(len(key)) if key_id[i]==str(j)]
tempseries = filterKeys(splitKeys)
temp = list(tempseries.values())
if len(temp) == 0:
print('Error: No results for requested variable no.' + variable_str + 'for country' + countries[j])
else:
# All observations for each edition
tempObs = [temp[i].get('observations') for i in range(len(temp))]
# Keys (t) to identify time periods
tempKeys = [list(tempObs[i].keys()) for i in range(len(tempObs))]
# Get real time data
realObs = []
for i in range(len(tempObs)):
if i == 0:
# All observations for first edition
realObs.extend([tempObs[0][k][0] for k in set(tempKeys[0])])
else:
# Get keys for observations for time periods that have not been published before
# (i.e. have been revised)
newKey = set(tempKeys[i]) - set().union(*tempKeys[0:i]);
if len(newKey) > 0:
# If current edition includes new observations, add to real time data
realObs.extend([tempObs[i][k][0] for k in newKey])
# Get keys to identify dates corresponding to real time observations
allKeys = [int(item) for item in list(set().union(*tempKeys))]
allKeys.sort()
# Get dates corresponding to real time observations
tempDates = [dates[i] for i in allKeys]
# Create dataframe for country j
df_temp = pd.DataFrame(tempDates)
df_temp.set_index(0, inplace=True)
df_temp[countries[j]] = realObs
# Combine with countries from previous iteration, fill missing dates with nan
df = df.join(df_temp)
return df, units
else:
print('Error: No results for requested variable no. ' + variable_str + ' for country ' + country_str)
elif (response.status_code == 404):
print('Error: No results for requested variable no. ' + variable_str + ' for country ' + country_str)
else:
print('Error: %s' % response.status_code)
print('Error: Check URL. Made request from: /r/n' + url)
def get_series_all_releases_MEIArchive(country_list, variable_list, frequency, startDate, endDate, startEDI, endEDI):
# Request data from OECD API and return pandas DataFrame
# =============== INPUT
# country_list: list of countries
# variable_list: list of variabes
# frequency: 'M' for monthly and 'Q' for quarterly time series
# startDate: date in YYYY-MM (2000-01) or YYYY-QQ (2000-Q1) format, None for all observations
# endDate: date in YYYY-MM (2000-01) or YYYY-QQ (2000-Q1) format, None for all observations
# startEDI: Edition of data, i.e. when it was published in YYYYMM format
# endEDI: Final edition in YYYYMM format
# =============== RAW DATA STRUCTURE
# The dataset has a total of M series which are identified through four keys in the following format: 0:0:0:0
# Position 1: Country
# Position 2: Variable
# Position 3: Edition of Data
# Position 4: Frequency
# Each series contains n observations for each time period, identified through a number t
# For example, for country "GBR", variable "201" with frequency M, between 1999-01 to 1999-12 the series for
# edition 202201 contains 12 observations. The series for edition 1999-03 will contain maximum 3 observations.
# it is possible that t is not be a consecutive series of values in which case observations are missing.
# Code accounts for differences in length of time series.
# Real time data is extracted as the observations in the first published edition.
# ============= Create URL
url_base = "https://stats.oecd.org/sdmx-json/data/MEI_ARCHIVE/"
if isinstance(variable_list,list) == True:
if len(variable_list) == 1:
variable_str = str(variable_list[0])
else:
variable_str = '+'.join(str(x) for x in variable_list)
else:
variable_str = str(variable_list)
if isinstance(country_list,list) == True:
N = len(country_list)
if len(country_list) == 1:
country_str = country_list[0]
else:
country_str = '+'.join(str(x) for x in country_list)
else:
N = 1;
country_str = country_list
startTime = "startTime=" + startDate
endTime = "endTime=" + endDate
if startEDI == [] and endEDI == []:
if float(startDate.replace('-','')) >= 199902:
edition_dates = pd.date_range(datetime.strptime(startDate, '%Y-%m'),datetime.now(),freq='m').strftime('%Y%m')
else:
edition_dates = pd.date_range(datetime.strptime('1999-02', '%Y-%m'),datetime.now(),freq='m').strftime('%Y%m')
elif endEDI == []:
edition_dates = pd.date_range(datetime.strptime(startEDI, '%Y-%m'),datetime.now(),freq='m').strftime('%Y%m')
elif startEDI == []:
edition_dates = pd.date_range(datetime.strptime(startDate, '%Y-%m'),datetime.strptime(endEDI, '%Y-%m'),freq='m').strftime('%Y%m')
else:
edition_dates = pd.date_range(datetime.strptime(startEDI, '%Y-%m'),datetime.strptime(endEDI, '%Y-%m'),freq='m').strftime('%Y%m')
edition_str = '+'.join(str(x) for x in edition_dates)
url = url_base + country_str + "." + variable_str + "." + edition_str + "." + frequency + "/all?" + startTime + "&" + endTime
# ============= Download Data
response = rq.get(url = url, params = {})
if (response.status_code == 200):
responseJson = response.json()
# Get list of observations. This includes all revision to variables, not just real time vintages
series = responseJson.get('dataSets')[0].get('series')
filterKeys = lambda k: {x: series[x] for x in k}
if (len(series) > 0):
# Countries in dataset
temp = responseJson.get('structure').get('dimensions').get('series')[0].get('values')
countries = [temp[i].get('id') for i in range(len(temp))]
# Variables in dataset
temp = responseJson.get('structure').get('dimensions').get('series')[1].get('values')
variables = [temp[i].get('id') for i in range(len(temp))]
# All available time periods. Does NOT necessarily equal all time periods per country
temp = responseJson.get('structure').get('dimensions').get('observation')[0].get('values')
dates = [temp[i].get('id') for i in range(len(temp))]
# Remove wrong frequency (sometimes in there by accident)
if frequency == 'M':
dates = [item for item in dates if "Q" not in item]
elif frequency == 'Q':
dates = [item for item in dates if "M" not in item]
# All editions in dataset (not necessarily in chronological order!!)
temp = responseJson.get('structure').get('dimensions').get('series')[2].get('values')
editions = [temp[i].get('id') for i in range(len(temp))]
editions_sort = [int(item) for item in editions]
editions_sort.sort()
# Units of variables
units = responseJson.get('structure').get('attributes').get('series')[1].get('values')
# Get all keys
key = list(series.keys())
key_id = [key[i].split(':')[2] for i in range(len(key))]
# Create empty dict with all editions as index
df_all = dict.fromkeys(editions_sort)
for j in range(len(editions)):
# Get editions per country
splitKeys = [key[i] for i in range(len(key)) if key_id[i]==str(j)]
tempseries = filterKeys(splitKeys)
if len(tempseries) == 0:
print('Error: No results for requested variable no.' + variable_str + 'for country' + countries[j])
else:
# Vintage for each country
tempVintage = [tempseries[item].get('observations') for item in splitKeys]
# Keys to identify time periods
tempVintageKeys = [list(tempVintage[i].keys()) for i in range(len(tempVintage))]
# Keys to identify countries
tempCountryKeys = [item.split(':')[0] for item in splitKeys]
# Get all vintages
for i in range(len(tempVintage)):
if i == 0:
tempDates = [dates[int(k)] for k in tempVintageKeys[i]]
df = pd.DataFrame(tempDates)
df.set_index(0, inplace=True)
tempname = countries[int(tempCountryKeys[i])] + '_' + variables[0]
df[tempname] = [tempVintage[i][k][0] for k in tempVintageKeys[i]]
else:
tempDates = [dates[int(k)] for k in tempVintageKeys[i]]
df_temp = pd.DataFrame(tempDates)
df_temp.set_index(0, inplace=True)
tempname = countries[int(tempCountryKeys[i])] + '_' + variables[0]
df_temp[tempname] = [tempVintage[i][k][0] for k in tempVintageKeys[i]]
if len(df_temp) > len(df):
df = df.join(df_temp,how='right')
else:
df = df.join(df_temp)
df_all[int(editions[j])] = df
return df_all
else:
print('Error: No results for requested variable no. ' + variable_str + ' for country ' + country_str)
elif (response.status_code == 404):
print('Error: No results for requested variable no. ' + variable_str + ' for country ' + country_str)
else:
print('Error: %s' % response.status_code)
print('Error: Check URL. Made request from: /r/n' + url)
def get_var_codes_MEI_BTS_COS():
url = "https://stats.oecd.org/restsdmx/sdmx.ashx/GetDataStructure/MEI_BTS_COS"
resp = rq.get(url)
doc = etree.fromstring(resp.content)
root = "{http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message}CodeLists/*[@id='CL_MEI_BTS_COS_SUBJECT']/"
var_list = doc.findall(root)
var_name = [var_list[i].get('value') for i in range(2,len(var_list))]
var_description = [var_list[i][0].text for i in range(2,len(var_list))]
return var_name, var_description
def get_country_codes_MEI_BTS_COS():
url = "https://stats.oecd.org/restsdmx/sdmx.ashx/GetDataStructure/MEI_BTS_COS"
resp = rq.get(url)
doc = etree.fromstring(resp.content)
root ="{http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message}CodeLists/*[@id='CL_MEI_BTS_COS_LOCATION']/"
country_list = doc.findall(root)
country_code = [country_list[i].get('value') for i in range(2,len(country_list))]
return country_code
def get_series_MEI_BTS_COS(country_list, variable_list, frequency, startDate, endDate):
# Request data from OECD API and return pandas DataFrame
# =============== INPUT
# country_list: list of countries
# variable_list: list of variabes
# frequency: 'M' for monthly and 'Q' for quarterly time series
# startDate: date in YYYY-MM (2000-01) or YYYY-QQ (2000-Q1) format, None for all observations
# endDate: date in YYYY-MM (2000-01) or YYYY-QQ (2000-Q1) format, None for all observations
# =============== RAW DATA STRUCTURE
# The dataset has a total of M series which are identified through four keys in the following format: 0:0:0:0
# Position 1: Variable
# Position 2: Country
# Position 3: Measure
# Position 4: Frequency
# Each series contains n observations for each time period, identified through a number t
# For example, for country "GBR", variable "201" with frequency M, between 1999-01 to 1999-12 the series
# contains 12 observations.
# It is possible that t is not be a consecutive series of values in which case observations are missing.
# Code accounts for differences in length of time series.
# ============= Create URL
url_base = "https://stats.oecd.org/sdmx-json/data/MEI_BTS_COS/"
if isinstance(variable_list,list) == True:
if len(variable_list) == 1:
variable_str = str(variable_list[0])
else:
variable_str = '+'.join(str(x) for x in variable_list)
else:
variable_str = str(variable_list)
if isinstance(country_list,list) == True:
N = len(country_list)
if len(country_list) == 1:
country_str = country_list[0]
else:
country_str = '+'.join(str(x) for x in country_list)
else:
N = 1;
country_str = country_list
startTime = "startTime=" + startDate
endTime = "endTime=" + endDate
measure = "BLSA"
url = url_base + variable_str + "." + country_str + "." + measure + "." + frequency + "/all?" + startTime + "&" + endTime
# ============= Download Data
response = rq.get(url = url, params = {})
if (response.status_code == 200):
responseJson = response.json()
# Get list of observations. This includes all revision to variables, not just real time vintages
series = responseJson.get('dataSets')[0].get('series')
filterKeys = lambda k: {x: series[x] for x in k}
if (len(series) > 0):
# All variables in dataset
temp = responseJson.get('structure').get('dimensions').get('series')[0].get('values')
variables = [temp[i].get('id') for i in range(len(temp))]
# All available time periods. Does NOT necessarily equal all time periods per country/variable
temp = responseJson.get('structure').get('dimensions').get('observation')[0].get('values')
dates = [temp[i].get('id') for i in range(len(temp))]
dates = [item for item in dates if "Q" not in item]
# All measure (all the same)
temp = responseJson.get('structure').get('dimensions').get('series')[2].get('values')
measure = [temp[i].get('id') for i in range(len(temp))]
# Countries
temp = responseJson.get('structure').get('dimensions').get('series')[1].get('values')
countries = [temp[i].get('id') for i in range(len(temp))]
if N == 1:
temp = list(series.values())
tempObs = [temp[i].get('observations') for i in range(len(temp))]
tempKeys = [list(tempObs[i].keys()) for i in range(len(tempObs))]
realObs = []
for i in range(len(tempObs)):
if i == 0:
realObs.extend([tempObs[0][j][0] for j in set(tempKeys[0])])
else:
newKey = set(tempKeys[i]) - set().union(*tempKeys[0:i]);
if len(newKey) > 0:
realObs.extend([tempObs[i][j][0] for j in newKey])
df = pd.DataFrame(dates)
df[countries[0]] = realObs
return df
elif len(variables) > 1:
# Get all keys
key = list(series.keys())
var_key_id = [key[i].split(':')[0] for i in range(len(key))]
# Create empty dataframe with all dates as index
df = pd.DataFrame(dates)
df.set_index(0, inplace=True)
# Combine Data per variable for each country
df_all = dict.fromkeys(variables)
for j in range(len(variables)):
df = pd.DataFrame(dates)
df.set_index(0, inplace=True)
# Get series per country
splitKeys = [key[i] for i in range(len(key)) if var_key_id[i]==str(j)]
tempseries = filterKeys(splitKeys)
temp = list(tempseries.values())
if len(temp) == 0:
print('Error: No results for requested variable' + variable_list[j])
else:
# Get country keys
subkey = list(tempseries.keys())
country_key_id = [subkey[i].split(':')[1] for i in range(len(subkey))]
# All observations for each country
tempObs = [temp[i].get('observations') for i in range(len(temp))]
# Keys (t) to identify time periods
tempKeys = [list(tempObs[i].keys()) for i in range(len(tempObs))]
for i in range(len(tempObs)):
# Get Observations
Obs = [item[0] for item in list(tempObs[i].values())]
# Get dates for observations
tempDates = [dates[int(x)] for x in tempKeys[i]]
df_temp = pd.DataFrame(tempDates)
df_temp.set_index(0, inplace=True)
# Get country name
tempname = countries[int(country_key_id[i])] + '_' + variables[j]
df_temp[tempname] = Obs
# Combine with countries from previous iteration, fill missing dates with nan
df = df.join(df_temp)
df_all[variables[j]] = df
return df_all
else:
print('Error: No results for requested variable no. ' + variable_str + ' for country ' + country_str)
elif (response.status_code == 404):
print('Error: No results for requested variable no. ' + variable_str + ' for country ' + country_str)
else:
print('Error: %s' % response.status_code)
print('Error: Check URL. Made request from: /r/n' + url)
def get_var_codes_MEI_FIN():
url = "https://stats.oecd.org/restsdmx/sdmx.ashx/GetDataStructure/MEI_FIN"
resp = rq.get(url)
doc = etree.fromstring(resp.content)
root = "{http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message}CodeLists/*[@id='CL_MEI_FIN_SUBJECT']/"
var_list = doc.findall(root)
var_name = [var_list[i].get('value') for i in range(2,len(var_list))]
var_description = [var_list[i][0].text for i in range(2,len(var_list))]
return var_name, var_description
def get_country_codes_MEI_FIN():
url = "https://stats.oecd.org/restsdmx/sdmx.ashx/GetDataStructure/MEI_FIN"
resp = rq.get(url)
doc = etree.fromstring(resp.content)
root ="{http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message}CodeLists/*[@id='CL_MEI_FIN_LOCATION']/"
country_list = doc.findall(root)
country_code = [country_list[i].get('value') for i in range(2,len(country_list))]
return country_code
def get_series_MEI_FIN(country_list, variable_list, frequency, startDate, endDate):
# Request data from OECD API and return pandas DataFrame
# =============== INPUT
# country_list: list of countries
# variable_list: list of variabes
# frequency: 'M' for monthly and 'Q' for quarterly time series
# startDate: date in YYYY-MM (2000-01) or YYYY-QQ (2000-Q1) format, None for all observations
# endDate: date in YYYY-MM (2000-01) or YYYY-QQ (2000-Q1) format, None for all observations
# =============== RAW DATA STRUCTURE
# The dataset has a total of M series which are identified through four keys in the following format: 0:0:0:0
# Position 1: Variable
# Position 2: Country
# Position 3: Measure
# Position 4: Frequency
# Each series contains n observations for each time period, identified through a number t
# For example, for country "GBR", variable "201" with frequency M, between 1999-01 to 1999-12 the series
# contains 12 observations.
# It is possible that t is not be a consecutive series of values in which case observations are missing.
# Code accounts for differences in length of time series.
# ============= Create URL
url_base = "https://stats.oecd.org/sdmx-json/data/MEI_FIN/"
if isinstance(variable_list,list) == True:
if len(variable_list) == 1:
variable_str = str(variable_list[0])
else:
variable_str = '+'.join(str(x) for x in variable_list)
else:
variable_str = str(variable_list)
if isinstance(country_list,list) == True:
N = len(country_list)
if len(country_list) == 1:
country_str = country_list[0]
else:
country_str = '+'.join(str(x) for x in country_list)
else:
N = 1;
country_str = country_list
startTime = "startTime=" + startDate
endTime = "endTime=" + endDate
url = url_base + variable_str + "." + country_str + "." + frequency + "/all?" + startTime + "&" + endTime
# ============= Download Data
response = rq.get(url = url, params = {})
if (response.status_code == 200):
responseJson = response.json()
# Get list of observations. This includes all revision to variables, not just real time vintages
series = responseJson.get('dataSets')[0].get('series')
filterKeys = lambda k: {x: series[x] for x in k}
if (len(series) > 0):
# All variables in dataset
temp = responseJson.get('structure').get('dimensions').get('series')[0].get('values')
variables = [temp[i].get('id') for i in range(len(temp))]
# All available time periods. Does NOT necessarily equal all time periods per country/variable
temp = responseJson.get('structure').get('dimensions').get('observation')[0].get('values')
dates = [temp[i].get('id') for i in range(len(temp))]
dates = [item for item in dates if "Q" not in item]
# All measure (all the same)
temp = responseJson.get('structure').get('dimensions').get('series')[2].get('values')
measure = [temp[i].get('id') for i in range(len(temp))]
# Countries
temp = responseJson.get('structure').get('dimensions').get('series')[1].get('values')
countries = [temp[i].get('id') for i in range(len(temp))]
# Get all keys
key = list(series.keys())
var_key_id = [key[i].split(':')[0] for i in range(len(key))]
# Create empty dataframe with all dates as index
df = pd.DataFrame(dates)
df.set_index(0, inplace=True)
# Combine Data per variable for each country
df_all = dict.fromkeys(variables)
for j in range(len(variables)):
df = pd.DataFrame(dates)
df.set_index(0, inplace=True)
# Get series per country
splitKeys = [key[i] for i in range(len(key)) if var_key_id[i]==str(j)]
tempseries = filterKeys(splitKeys)
temp = list(tempseries.values())
if len(temp) == 0:
print('Error: No results for requested variable' + variable_list[j])
else:
# Get country keys
subkey = list(tempseries.keys())
country_key_id = [subkey[i].split(':')[1] for i in range(len(subkey))]
# All observations for each country
tempObs = [temp[i].get('observations') for i in range(len(temp))]
# Keys (t) to identify time periods
tempKeys = [list(tempObs[i].keys()) for i in range(len(tempObs))]
for i in range(len(tempObs)):
# Get Observations
Obs = [item[0] for item in list(tempObs[i].values())]
# Get dates for observations
tempDates = [dates[int(x)] for x in tempKeys[i]]
df_temp = pd.DataFrame(tempDates)
df_temp.set_index(0, inplace=True)
# Get country name
tempname = countries[int(country_key_id[i])] + '_' + variables[j]
df_temp[tempname] = Obs
# Combine with countries from previous iteration, fill missing dates with nan
df = df.join(df_temp)
df_all[variables[j]] = df
return df_all
else:
print('Error: No results for requested variable no. ' + variable_str + ' for country ' + country_str)
elif (response.status_code == 404):
print('Error: No results for requested variable no. ' + variable_str + ' for country ' + country_str)
else:
print('Error: %s' % response.status_code)
print('Error: Check URL. Made request from: /r/n' + url)
def merge_MEI_Vintage(MEI_ALL,transform):
if transform == []:
nVar = len(MEI_ALL)
allKeys = [list(item.keys()) for item in MEI_ALL]
temp = list(set().union(*allKeys))
allEditions = [int(item) for item in temp]
allEditions.sort()
allVintages = dict.fromkeys(allEditions)
for j in allEditions:
tempEd = []
for x in MEI_ALL:
try:
tempEd.append(x[j])
except:
pass
for i in range(len(tempEd)):
if i == 0:
df = tempEd[i]
else:
if len(tempEd[i]) > len(df):
df = df.join(tempEd[i],how='right')
else:
df = df.join(tempEd[i])
allVintages[j] = df
return allVintages
else:
nVar = len(MEI_ALL)
allKeys = [list(item.keys()) for item in MEI_ALL]
temp = list(set().union(*allKeys))
allEditions = [int(item) for item in temp]
allEditions.sort()
allVintages = dict.fromkeys(allEditions)
for j in allEditions:
tempEd = []
for x in MEI_ALL:
try:
tempEd.append(x[j])
except:
pass
for i in range(len(tempEd)):
if i == 0:
df = tempEd[i]
else:
if len(tempEd[i]) > len(df):
df = df.join(tempEd[i],how='right')
else:
df = df.join(tempEd[i])
trans = [transform] * len(df.columns)
trans = pd.DataFrame([trans],columns=df.columns)
trans.index = ['Transform']
df = pd.concat([trans,df])
allVintages[j] = df
return allVintages
def merge(data):
tempKeys = list(data.keys())
for i in range(len(tempKeys)):
if i == 0:
data_new = data[tempKeys[i]]
else:
if len(data[tempKeys[i]]) > len(data_new):
data_new = data_new.join(data[tempKeys[i]],how='right')
else:
data_new = data_new.join(data[tempKeys[i]])
return data_new