Skip to content

Commit c170692

Browse files
committed
feat: pad missing periods until today if it's within first_date and last_date
1 parent 401bc75 commit c170692

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

cbrapi/currency.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def get_time_series(
149149

150150
if re.match("RUB", symbol):
151151
foreign_ccy = re.search(r"^RUB(.*)$", symbol)[1]
152-
query_symbol = foreign_ccy + "RUB"
152+
query_symbol = foreign_ccy
153153
method = "inverse"
154154
else:
155155
query_symbol = symbol
@@ -164,7 +164,7 @@ def get_time_series(
164164
try:
165165
df = pd.read_xml(rate_xml, xpath="//ValuteCursDynamic")
166166
except ValueError:
167-
return pd.Series()
167+
return pd.Series(dtype=float)
168168
cbr_cols1 = {"rowOrder", "id", "Vnom", "Vcode", "CursDate", "Vcurs"}
169169
cbr_cols2 = cbr_cols1.union({"VunitRate"})
170170
if set(df.columns) not in [cbr_cols1, cbr_cols2]:
@@ -181,7 +181,10 @@ def get_time_series(
181181
df.set_index("CursDate", inplace=True, verify_integrity=True)
182182
df.sort_index(ascending=True, inplace=True)
183183
s = df.squeeze(axis=1) # all outputs must be pd.Series
184-
s = pad_missing_periods(s, freq="D")
184+
pad_end_date = data2.date()
185+
if data1.date() < today < data2.date():
186+
pad_end_date = today
187+
s = pad_missing_periods(s, freq="D", end_date=pad_end_date)
185188
s.index.rename("date", inplace=True)
186189
if period.upper() == "M":
187190
s = s.to_timestamp().resample("ME").last()

cbrapi/helpers.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
from typing import Union
1+
from typing import Union, Optional
22
from datetime import datetime, date
33
import pandas as pd
44

55

66
def pad_missing_periods(
7-
ts: Union[pd.Series, pd.DataFrame], freq: str = "D"
7+
ts: Union[pd.Series, pd.DataFrame], freq: str = "D", end_date: Optional[date] = None
88
) -> Union[pd.Series, pd.DataFrame]:
99
"""
1010
Pad missing dates and values in the time series.
1111
"""
12+
if ts.empty:
13+
return ts
1214
name = ts.index.name
1315
if not isinstance(ts.index, pd.PeriodIndex):
1416
ts.index = ts.index.to_period(freq)
1517
ts.sort_index(
1618
ascending=True, inplace=True
1719
) # The order should be ascending to make new Period index
18-
idx = pd.period_range(start=ts.index[0], end=ts.index[-1], freq=freq)
20+
end = ts.index[-1]
21+
if end_date:
22+
end_period = pd.Period(end_date, freq=freq)
23+
if end_period > end:
24+
end = end_period
25+
idx = pd.period_range(start=ts.index[0], end=end, freq=freq)
1926
ts = ts.reindex(idx, method="pad")
2027
ts.index.rename(name, inplace=True)
2128
return ts

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
print(
1414
cbr.currency.get_time_series(
15-
symbol="USDRUB", first_date="2019-01-01", last_date="2020-12-31", period="M"
15+
symbol="USDRUB", first_date="2025-12-01", last_date="2026-01-31", period="D"
1616
)
1717
)
1818

@@ -30,4 +30,4 @@
3030

3131
print(cbr.get_currencies_list())
3232

33-
print(cbr.get_currency_code("USDRUB.CBR"))
33+
print(cbr.get_currency_code("USDRUB"))

0 commit comments

Comments
 (0)