-
Notifications
You must be signed in to change notification settings - Fork 679
/
Copy pathtest_jsdmx.py
176 lines (162 loc) · 4.91 KB
/
test_jsdmx.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
# pylint: disable-msg=E1101,W0613,W0603
import os
import numpy as np
import pandas as pd
from pandas import testing as tm
import pytest
from pandas_datareader.compat import PANDAS_0210
from pandas_datareader.io import read_jsdmx
pytestmark = pytest.mark.stable
@pytest.fixture
def dirpath(datapath):
return datapath("io", "data")
@pytest.mark.skipif(not PANDAS_0210, reason="Broken on old pandas")
def test_tourism(dirpath):
# OECD -> Industry and Services -> Inbound Tourism
result = read_jsdmx(os.path.join(dirpath, "jsdmx", "tourism.json"))
assert isinstance(result, pd.DataFrame)
jp = result["Japan"]
visitors = [
"China",
"Hong Kong, China",
"Total international arrivals",
"Korea",
"Chinese Taipei",
"United States",
]
exp_col = pd.Index(
[
"China",
"Hong Kong, China",
"Total international arrivals",
"Korea",
"Chinese Taipei",
"United States",
],
name="Variable",
)
exp_idx = pd.DatetimeIndex(
[
"2008-01-01",
"2009-01-01",
"2010-01-01",
"2011-01-01",
"2012-01-01",
"2013-01-01",
"2014-01-01",
"2015-01-01",
"2016-01-01",
],
name="Year",
)
values = [
[1000000.0, 550000.0, 8351000.0, 2382000.0, 1390000.0, 768000.0],
[1006000.0, 450000.0, 6790000.0, 1587000.0, 1024000.0, 700000.0],
[1413000.0, 509000.0, 8611000.0, 2440000.0, 1268000.0, 727000.0],
[1043000.0, 365000.0, 6219000.0, 1658000.0, 994000.0, 566000.0],
[1430000.0, 482000.0, 8368000.0, 2044000.0, 1467000.0, 717000.0],
[1314000.0, 746000.0, 10364000.0, 2456000.0, 2211000.0, 799000.0],
[2409000.0, 926000.0, 13413000.0, 2755000.0, 2830000.0, 892000.0],
[4993689.0, 1524292.0, 19737409.0, 4002095.0, 3677075.0, 1033258.0],
[6373564.0, 1839193.0, 24039700.0, 5090302.0, 4167512.0, 1242719.0],
]
values = np.array(values, dtype="object")
expected = pd.DataFrame(values, index=exp_idx, columns=exp_col)
tm.assert_frame_equal(jp[visitors], expected)
@pytest.mark.skipif(not PANDAS_0210, reason="Broken on old pandas")
def test_land_use(dirpath):
# OECD -> Environment -> Resources Land Use
result = read_jsdmx(os.path.join(dirpath, "jsdmx", "land_use.json"))
assert isinstance(result, pd.DataFrame)
result = result.loc["2010":"2011"]
cols = [
"Arable land and permanent crops",
"Arable and cropland % land area",
"Total area",
"Forest",
"Forest % land area",
"Land area",
"Permanent meadows and pastures",
"Meadows and pastures % land area",
"Other areas",
"Other % land area",
]
exp_col = pd.MultiIndex.from_product(
[["Japan", "United States"], cols], names=["Country", "Variable"]
)
exp_idx = pd.DatetimeIndex(["2010", "2011"], name="Year")
values = [
[
53790.0,
14.753154141525,
377800.0,
np.nan,
np.nan,
364600.0,
5000.0,
1.3713658804169,
np.nan,
np.nan,
1897990.0,
20.722767650476,
9629090.0,
np.nan,
np.nan,
9158960.0,
2416000.0,
26.378540795025,
np.nan,
np.nan,
],
[
53580.0,
14.691527282698,
377800.0,
np.nan,
np.nan,
364700.0,
5000.0,
1.3709898546751,
np.nan,
np.nan,
1897990.0,
20.722767650476,
9629090.0,
np.nan,
np.nan,
9158960.0,
2416000.0,
26.378540795025,
np.nan,
np.nan,
],
]
values = np.array(values)
expected = pd.DataFrame(values, index=exp_idx, columns=exp_col)
tm.assert_frame_equal(result[exp_col], expected)
@pytest.mark.skipif(not PANDAS_0210, reason="Broken on old pandas")
def test_quartervalue(dirpath):
# https://stats.oecd.org/sdmx-json/data/QNA/AUS+AUT+BEL+CAN+CHL.GDP+B1_
# GE.CUR+VOBARSA.Q/all?startTime=2009-Q1&endTime=2011-Q4
result = read_jsdmx(os.path.join(dirpath, "jsdmx", "oecd1.json"))
assert isinstance(result, pd.DataFrame)
expected = pd.DatetimeIndex(
[
"2009-01-01",
"2009-04-01",
"2009-07-01",
"2009-10-01",
"2010-01-01",
"2010-04-01",
"2010-07-01",
"2010-10-01",
"2011-01-01",
"2011-04-01",
"2011-07-01",
"2011-10-01",
],
dtype="datetime64[ns]",
name="Period",
freq=None,
)
tm.assert_index_equal(result.index, expected)