Skip to content

Commit b5bb5df

Browse files
committed
refactor(endpoints): format known date columns in odata endpoints
Signed-off-by: Antonio Pedro Vieira <aps.vieira95@gmail.com>
1 parent f8d80d0 commit b5bb5df

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

bcb/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
)
1010
import pandas as pd
1111

12-
1312
OLINDA_BASE_URL = "https://olinda.bcb.gov.br/olinda/servico"
1413

1514

@@ -30,9 +29,19 @@ def __call__(self, *args):
3029

3130

3231
class EndpointQuery(ODataQuery):
32+
_DATE_COLUMN_NAMES = {
33+
"Data",
34+
"DataReferencia",
35+
}
36+
3337
def collect(self):
34-
data = super().collect()
35-
return pd.DataFrame(data["value"])
38+
raw_data = super().collect()
39+
data = pd.DataFrame(raw_data["value"])
40+
for col in self._DATE_COLUMN_NAMES:
41+
if col not in data.columns:
42+
continue
43+
data[col] = pd.to_datetime(data[col])
44+
return data
3645

3746

3847
class Endpoint(metaclass=EndpointMeta):

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ matplotlib = "^3.5.3"
2424
ipython = "^8.14.0"
2525
furo = "^2022.6.21"
2626

27-
2827
[tool.poetry.group.dev.dependencies]
2928
pycodestyle = "^2.9.1"
3029
ipykernel = "^6.15.2"

tests/test_expectativas.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pandas as pd
2+
from datetime import datetime
3+
from pytest import fixture
4+
5+
from bcb import Expectativas
6+
7+
8+
@fixture
9+
def endpoints():
10+
ep = Expectativas()
11+
return [ep.get_endpoint(e.data["name"]) for e in ep.service.endpoints]
12+
13+
14+
def test_expectativas_date_format(endpoints):
15+
for endpoint in endpoints:
16+
query = endpoint.query().limit(1)
17+
data = query.collect()
18+
assert isinstance(data, pd.DataFrame)
19+
assert data.shape[0] == 1
20+
assert isinstance(data["Data"].iloc[0], datetime)
21+
if "DataReferencia" in data.columns:
22+
assert isinstance(data["DataReferencia"].iloc[0], datetime)

0 commit comments

Comments
 (0)