Skip to content

Commit cfe8c33

Browse files
authored
Merge pull request #1 from mbk-dev/master
Sink
2 parents f40a3ad + 5221a1f commit cfe8c33

File tree

8 files changed

+106
-39
lines changed

8 files changed

+106
-39
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
# ecb
1+
[![Python](https://img.shields.io/badge/python-v3-brightgreen.svg)](https://www.python.org/)
2+
[![License](https://img.shields.io/badge/license-MIT-blue)](https://opensource.org/licenses/MIT)
3+
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
4+
5+
# ecb: Python interface to European Central Bank (ECB) API
6+
Several functions to det financial data from European Central Bank (ECB) database.
7+
8+
## Available functions
9+
10+
- `get_gdp_q()` Euro area GDP at market price quarterly timeseries (from 1995)
11+
**ECB key rates (from 1999)**:
12+
- `get_refinancing_rate()` ECB Main refinancing operations key rate time series
13+
- `get_deposit_rate()` ECB key rate on the deposit facility
14+
- `get_marginal_rate()` ECB key rate on marginal lending facility

ecb/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from importlib.metadata import version
2-
from ecb.kr import (get_main_rate,
3-
get_deposit_rate,
4-
get_marginal_rate,
5-
)
2+
from ecb.kr import (
3+
get_refinancing_rate,
4+
get_deposit_rate,
5+
get_marginal_rate,
6+
)
67
from ecb.gdp import get_gdp_q
78
from ecb.hicp import get_hicp
89
from ecb.request_data import get_data_frame
910

10-
# __version__ = version("ecb")
11+
__version__ = version("ecb")

ecb/gdp.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22
import ecb
33

44

5-
def get_gdp_q(startperiod: str = '1900-01-01', endperiod: str = None) -> pd.Series:
6-
s = ecb.request_data.get_data_frame(agency='MNA',code='Q.N.I8.W2.S1.S1.B.B1GQ._Z._Z._Z.EUR.V.N',
7-
freq='Q', startperiod=startperiod, endperiod=endperiod)
8-
s.rename("GBP", inplace=True)
5+
def get_gdp_q(start_period: str = "1900-01-01", end_period: str = None) -> pd.Series:
6+
"""
7+
Get Euro area GDP at market price quarterly timeseries.
8+
9+
GDP data is available from 1995.
10+
"""
11+
s = ecb.request_data.get_data_frame(
12+
agency="MNA",
13+
code="Q.N.I8.W2.S1.S1.B.B1GQ._Z._Z._Z.EUR.V.N",
14+
freq="Q",
15+
start_period=start_period,
16+
end_period=end_period,
17+
)
18+
s.rename("gdp", inplace=True)
919
return s

ecb/hicp.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
today = date.today()
66

77

8-
def get_hicp(startperiod: str = '1900-01-01', endperiod: str = None) -> pd.Series:
9-
s = ecb.request_data.get_data_frame("ICP", "M.U2.N.000000.4.INX", "M",
10-
startperiod=startperiod, endperiod=endperiod)
8+
def get_hicp(start_period: str = "1900-01-01", end_period: str = None) -> pd.Series:
9+
s = ecb.request_data.get_data_frame(
10+
"ICP",
11+
"M.U2.N.000000.4.INX",
12+
"M",
13+
start_period=start_period,
14+
end_period=end_period,
15+
)
1116
s.rename("HICP", inplace=True)
1217
return s

ecb/kr.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,37 @@
22
import ecb
33

44

5-
def get_main_rate() -> pd.Series:
6-
s = ecb.request_data.get_data_frame('FM', 'D.U2.EUR.4F.KR.MRR_FR.LEV')
5+
def get_refinancing_rate() -> pd.Series:
6+
"""
7+
Get ECB main refinancing operations key rate monthly time series.
8+
"""
9+
s = ecb.request_data.get_data_frame("FM", "D.U2.EUR.4F.KR.MRR_FR.LEV")
710
s.rename("main_rate", inplace=True)
811
return s / 100
912

1013

1114
def get_deposit_rate() -> pd.Series:
12-
s = ecb.request_data.get_data_frame('FM', 'D.U2.EUR.4F.KR.DFR.LEV')
15+
"""
16+
Get ECB key rate on the deposit facility monthly time series.
17+
18+
On deposit facility rate banks can make overnight deposits with the Eurosystem.
19+
"""
20+
s = ecb.request_data.get_data_frame("FM", "D.U2.EUR.4F.KR.DFR.LEV")
1321
s.rename("deposit_rate", inplace=True)
1422
return s / 100
1523

1624

17-
def get_marginal_rate(startperiod: str = '1900-01-01', endperiod: str = None) -> pd.Series:
18-
s = ecb.request_data.get_data_frame('FM', 'D.U2.EUR.4F.KR.MLFR.LEV',
19-
startperiod=startperiod, endperiod=endperiod)
25+
def get_marginal_rate(start_period: str = "1900-01-01", end_period: str = None) -> pd.Series:
26+
"""
27+
Get ECB key rate on marginal lending facility monthly time seriesю
28+
29+
On marginal lending facility rate ECB may offer overnight credit to banks from the Eurosystem.
30+
"""
31+
s = ecb.request_data.get_data_frame(
32+
"FM",
33+
"D.U2.EUR.4F.KR.MLFR.LEV",
34+
start_period=start_period,
35+
end_period=end_period,
36+
)
2037
s.rename("marginal_rate", inplace=True)
2138
return s / 100

ecb/request_data.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,43 @@
44

55
from requests import Response
66

7-
URL_begin = "https://sdw-wsrest.ecb.europa.eu/service/data/"
8-
URL_end = "/"
7+
URL_BASE = "https://sdw-wsrest.ecb.europa.eu/service/data/"
8+
URL_END = "/"
99

1010

11-
def get_data_frame(agency: str = 'FM', code: str = 'D.U2.EUR.4F.KR.MRR_FR.LEV', freq: str = 'D',
12-
detail: str = "dataonly", format: str = "csvdata",
13-
startperiod: str = '1900-01-01', endperiod: str = None) -> pd.Series:
14-
request_url = URL_begin + agency + URL_end + code
15-
params = {'detail': detail,
16-
'format': format,
17-
'startPeriod': startperiod,
18-
'endPeriod': endperiod
19-
}
20-
abc: Response = requests.get(request_url, params=params)
11+
def get_data_frame(
12+
agency: str = "FM",
13+
code: str = "D.U2.EUR.4F.KR.MRR_FR.LEV",
14+
freq: str = "D",
15+
detail: str = "dataonly",
16+
format: str = "csvdata",
17+
start_period: str = "1900-01-01",
18+
end_period: str = None,
19+
) -> pd.Series:
20+
request_url = URL_BASE + agency + URL_END + code
21+
params = {
22+
"detail": detail,
23+
"format": format,
24+
"start_period": start_period,
25+
"end_period": end_period,
26+
}
27+
try:
28+
abc: Response = requests.get(request_url, params=params)
29+
except requests.exceptions.HTTPError as err:
30+
raise requests.exceptions.HTTPError(
31+
f"HTTP error fetching data for {code}:",
32+
abc.status_code,
33+
abc.reason,
34+
URL_BASE,
35+
) from err
2136
jresp = abc.text
22-
df = pd.read_csv(StringIO(jresp),
23-
usecols=['TIME_PERIOD', 'OBS_VALUE'],
24-
dtype={'OBS_VALUE': float},
25-
parse_dates=['TIME_PERIOD'])
37+
df = pd.read_csv(
38+
StringIO(jresp),
39+
usecols=["TIME_PERIOD", "OBS_VALUE"],
40+
dtype={"OBS_VALUE": float},
41+
parse_dates=["TIME_PERIOD"],
42+
)
2643
df.rename(columns={df.columns[0]: "date"}, inplace=True)
27-
df.set_index('date', inplace=True)
44+
df.set_index("date", inplace=True)
2845
df.index = df.index.to_period(freq=freq)
2946
return df.squeeze()

main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import pandas as pd
21
import ecb
32

4-
abc = ecb.kr.get_main_rate()
3+
abc = ecb.kr.get_refinancing_rate()
54
print(abc)
65

76
abc = ecb.kr.get_deposit_rate()
@@ -16,4 +15,4 @@
1615
abc = ecb.hicp.get_hicp()
1716
print(abc)
1817

19-
18+
print(ecb.__version__)

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ python = "^3.8"
1010
pandas = "^1.5.0"
1111
requests = "^2.28.1"
1212

13+
[tool.poetry.group.dev.dependencies]
14+
black = "^22.10.0"
1315

1416
[build-system]
1517
requires = ["poetry-core"]
1618
build-backend = "poetry.core.masonry.api"
19+
20+
[tool.black]
21+
line-length = 120

0 commit comments

Comments
 (0)