Skip to content

Commit 76d90d7

Browse files
committed
added a new dict to handle date fields depending on the endpoint
some date fields doesn't have the expected format to be parsed with to_datetime. So, it is necessary to pass the format argument to format the field correctly. Otherwise, it raises an error. A new dict has been added to check the endpoint before the parsing function is applied.
1 parent fb89bc8 commit 76d90d7

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

bcb/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,26 @@ def __call__(self, *args):
2929

3030

3131
class EndpointQuery(ODataQuery):
32+
_DATE_COLUMN_NAMES_BY_ENDPOINT = {
33+
"IfDataCadastro": {"Data": "%Y%m"}
34+
}
3235
_DATE_COLUMN_NAMES = {
3336
"Data",
3437
"DataReferencia",
3538
"dataHoraCotacao",
39+
"InicioPeriodo",
40+
"FimPeriodo",
41+
"DataVigencia",
3642
}
3743

3844
def collect(self):
3945
raw_data = super().collect()
4046
data = pd.DataFrame(raw_data["value"])
4147
for col in self._DATE_COLUMN_NAMES:
42-
if col not in data.columns:
43-
continue
44-
data[col] = pd.to_datetime(data[col])
48+
if self.entity.name in self._DATE_COLUMN_NAMES_BY_ENDPOINT and col in self._DATE_COLUMN_NAMES_BY_ENDPOINT[self.entity.name]:
49+
data[col] = pd.to_datetime(data[col], format=self._DATE_COLUMN_NAMES_BY_ENDPOINT[self.entity.name][col])
50+
elif col in data.columns:
51+
data[col] = pd.to_datetime(data[col])
4552
return data
4653

4754

@@ -75,7 +82,7 @@ def get(self, *args, **kwargs):
7582
_query.show()
7683
data = _query.collect()
7784
_query.reset()
78-
return pd.DataFrame(data["value"])
85+
return data # pd.DataFrame(data["value"])
7986

8087
def query(self):
8188
return EndpointQuery(self._entity, self._url)

0 commit comments

Comments
 (0)