-
Notifications
You must be signed in to change notification settings - Fork 679
/
Copy patheurostat.py
47 lines (34 loc) · 1.3 KB
/
eurostat.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
import pandas as pd
from pandas_datareader.base import _BaseReader
from pandas_datareader.io.sdmx import _read_sdmx_dsd, read_sdmx
class EurostatReader(_BaseReader):
"""Get data for the given name from Eurostat."""
_URL = "http://ec.europa.eu/eurostat/SDMX/diss-web/rest"
@property
def url(self):
"""API URL"""
if not isinstance(self.symbols, str):
raise ValueError("data name must be string")
q = "{0}/data/{1}/?startperiod={2}&endperiod={3}"
return q.format(self._URL, self.symbols, self.start.year, self.end.year)
@property
def dsd_url(self):
"""API DSD URL"""
if not isinstance(self.symbols, str):
raise ValueError("data name must be string")
return f"{self._URL}/datastructure/ESTAT/DSD_{self.symbols}"
def _read_one_data(self, url, params):
resp_dsd = self._get_response(self.dsd_url)
dsd = _read_sdmx_dsd(resp_dsd.content)
resp = self._get_response(url)
data = read_sdmx(resp.content, dsd=dsd)
try:
data.index = pd.to_datetime(data.index)
data = data.sort_index()
except ValueError:
pass
try:
data = data.truncate(self.start, self.end)
except TypeError:
pass
return data