-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfocus.py
More file actions
157 lines (108 loc) · 6.42 KB
/
Copy pathfocus.py
File metadata and controls
157 lines (108 loc) · 6.42 KB
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
import pandas as pd
import requests
import io, re
from datetime import date, timedelta, datetime
class Focus:
def __init__(self, time_expect="monthly"):
self.time_expect = time_expect
def period(period):
switcher = {
"monthly": "ExpectativaMercadoMensais",
"mensal": "ExpectativaMercadoMensais",
"m": "ExpectativaMercadoMensais",
"annual": "ExpectativasMercadoAnuais",
"anual": "ExpectativasMercadoAnuais",
"a": "ExpectativasMercadoAnuais"
}
return switcher.get(period, "Invalid argument.")
def rename(name):
if re.search(r"(?i)selic", str(name)):
return "SELIC"
elif re.search(r"(?i)câmbio", str(name)):
return "USDBRL"
elif re.search(r"(?i)produção industrial", str(name)):
return "INDUSTRIA"
elif re.search(r"(?i)balança comercial", str(name)):
return "BC"
elif re.search(r"(?i)pagamentos", str(name)):
return "BP"
elif re.search(r"(?i)fiscal", str(name)):
return "FISCAL"
elif re.search(r"(?i)pib agropec", str(name)):
return "PIB_AGRO"
elif re.search(r"(?i)pib industrial", str(name)):
return "PIB_INDU"
elif re.search(r"(?i)pib serviços", str(name)):
return "PIB_SERV"
elif re.search(r"(?i)pib total", str(name)):
return "PIB"
elif re.search(r"(?i)administrados", str(name)):
return "PADM"
else:
return name
url = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/"+str(period(self.time_expect))+"?$top=1000000&$format=text/csv&$select=Indicador,Data,DataReferencia,Media,Mediana,DesvioPadrao,CoeficienteVariacao,Minimo,Maximo,numeroRespondentes,baseCalculo"
request = requests.get(url)
data = io.StringIO(request.text)
self.df = pd.read_csv(data)
self.df.columns = ["indicator", "date", "reference_date", "mean", "median", "sd",
"var_coeficient", "min", "max", "respondents_number", "calc_base"]
for col in ["mean", "median", "sd", "var_coeficient", "min", "max"]:
self.df[col] = self.df[col].apply(lambda x: float(str(x).replace(",", ".")))
self.df["date"] = self.df["date"].apply(lambda x: datetime.strptime(x, "%Y-%m-%d"))
self.df["reference_date"] = self.df["reference_date"].apply(lambda x: datetime.strptime(str(x), "%Y"))
self.df["indicator"] = self.df["indicator"].apply(rename)
if period(self.time_expect) == "ExpectativaMercadoMensais":
self.df["reference_date"] = self.df["reference_date"].apply(lambda x: datetime.strptime(str(x), "%m/%Y"))
self.df.insert(3, "reference_year", self.df["reference_date"].apply(lambda x: x.year))
self.df.insert(4, "reference_month", self.df["reference_date"].apply(lambda x: x.month))
def get(self, indicator=["IGP-M", "IPCA", "SELIC", "USDBRL"], start_date="2000-01-03"):
self.indicator = list(indicator)
self.start_date = start_date
self.filter_df = self.df[(self.df["indicator"].isin(self.indicator))&(self.df["date"]>=self.start_date)]
return self.filter_df.set_index("indicator")
class FocusTop5:
def __init__(self, time_expect="monthly"):
self.time_expect = time_expect
def period(period):
swither = {
"monthly": "ExpectativasMercadoTop5Mensais",
"mensal": "ExpectativasMercadoTop5Mensais",
"m": "ExpectativasMercadoTop5Mensais",
"annual": "ExpectativasMercadoTop5Anuais",
"anual": "ExpectativasMercadoTop5Anuais",
"a": "ExpectativasMercadoTop5Anuais"
}
return swither.get(period, "Invalid argument.")
def rename(name):
if re.search(r"(?i)selic", str(name)):
return "Selic"
elif re.search(r"(?i)câmbio", str(name)):
return "USDBRL"
else:
return name
url = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/"+str(period(self.time_expect))+"?$top=2000000&$format=text/csv&$select=Indicador,Data,DataReferencia,Media,Mediana,DesvioPadrao,CoeficienteVariacao,Minimo,Maximo"
request = requests.get(url)
data = io.StringIO(request.text)
self.df = pd.read_csv(data)
self.df.columns = ["indicator", "date", "reference_date", "mean", "median", "sd", "var_coeficient", "min", "max"]
self.df["indicator"] = self.df["indicator"].apply(rename)
if period(self.time_expect) == "ExpectativasMercadoTop5Mensais":
self.df["reference_date"] = self.df["reference_date"].apply(lambda x: datetime.strptime(str(x), "%m/%Y"))
self.df.insert(3, "reference_year", self.df["reference_date"].apply(lambda x: x.year))
self.df.insert(4, "reference_month", self.df["reference_date"].apply(lambda x: x.month))
def get(self, indicator=["IGP-M", "IPCA", "SELIC", "USDBRL"], start_date="2000-01-03"):
self.indicator = list(indicator)
self.start_date = start_date
self.filter_df = self.df[(self.df["indicator"].isin(self.indicator))&(self.df["date"]>=self.start_date)]
return self.filter_df.set_index("indicator")
class FocusPIB:
def __init__(self):
url = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativasMercadoTrimestrais?$top=2000000&$format=text/csv"
request = requests.get(url)
data = io.StringIO(request.text)
self.df = pd.read_csv(data, sep=",")
self.df.columns = ["indicator", "date", "reference_date", "mean", "median", "sd", "var_coeficient",
"min", "max", "respondents_number"]
self.df["reference_date"] = self.df["reference_date"].apply(lambda x: datetime.strptime(str(x), "%m/%Y"))
def get(self):
return self.df.set_index("indicator")