Skip to content

Commit

Permalink
Fix for wrong name of currency rate column for import after downloading.
Browse files Browse the repository at this point in the history
  • Loading branch information
titov-vv committed Jun 30, 2024
1 parent 1e565e1 commit fcf8259
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
22 changes: 11 additions & 11 deletions jal/net/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,32 +186,32 @@ def CBR_DataReader(self, currency, start_timestamp, end_timestamp):
s_date = node.attrib['Date'] if node is not None else None
s_val = node.find("Value").text if node is not None else None
s_multiplier = node.find("Nominal").text if node is not None else 1
rows.append({"Date": s_date, "Rate": s_val, "Multiplier": s_multiplier})
data = pd.DataFrame(rows, columns=["Date", "Rate", "Multiplier"])
rows.append({"Date": s_date, "Close": s_val, "Multiplier": s_multiplier})
data = pd.DataFrame(rows, columns=["Date", "Close", "Multiplier"])
data['Date'] = pd.to_datetime(data['Date'], format="%d.%m.%Y", utc=True)
data['Rate'] = [x.replace(',', '.') for x in data['Rate']]
data['Rate'] = data['Rate'].apply(Decimal)
data['Close'] = [x.replace(',', '.') for x in data['Close']]
data['Close'] = data['Close'].apply(Decimal)
data['Multiplier'] = data['Multiplier'].apply(Decimal)
data['Rate'] = data['Rate'] / data['Multiplier']
data['Close'] = data['Close'] / data['Multiplier']
data.drop('Multiplier', axis=1, inplace=True)
rates = data.set_index("Date")
return rates

def ECB_DataReader(self, currency, start_timestamp, end_timestamp):
date1 = datetime.fromtimestamp(start_timestamp, tz=timezone.utc).strftime('%Y-%m-%d')
date2 = datetime.fromtimestamp(end_timestamp, tz=timezone.utc).strftime('%Y-%m-%d')
url = f"https://sdw-wsrest.ecb.europa.eu/service/data/EXR/D.{currency.symbol()}.EUR.SP00.A?startPeriod={date1}&endPeriod={date2}"
url = f"https://data-api.ecb.europa.eu/service/data/EXR/D.{currency.symbol()}.EUR.SP00.A?startPeriod={date1}&endPeriod={date2}"
file = StringIO(get_web_data(url, headers={'Accept': 'text/csv'}))
try:
data = pd.read_csv(file, dtype={'TIME_PERIOD': str, 'OBS_VALUE': str})
except ParserError:
return None
data.rename(columns={'TIME_PERIOD': 'Date', 'OBS_VALUE': 'Rate'}, inplace=True)
data = data[['Date', 'Rate']] # Keep only required columns
data.rename(columns={'TIME_PERIOD': 'Date', 'OBS_VALUE': 'Close'}, inplace=True)
data = data[['Date', 'Close']] # Keep only required columns
data['Date'] = pd.to_datetime(data['Date'], format="%Y-%m-%d", utc=True)
data['Rate'] = data['Rate'].apply(Decimal) # Convert from str to Decimal
data['Rate'] = Decimal('1') / data['Rate']
data['Rate'] = data['Rate'].apply(round, args=(10, ))
data['Close'] = data['Close'].apply(Decimal) # Convert from str to Decimal
data['Close'] = Decimal('1') / data['Close']
data['Close'] = data['Close'].apply(round, args=(10, ))
rates = data.set_index("Date")
return rates

Expand Down
6 changes: 3 additions & 3 deletions tests/test_downloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ def test_CBR_downloader(prepare_db):
downloader.PrepareRussianCBReader()
assert_frame_equal(codes, downloader.CBR_codes.head(2))

rates_usd = pd.DataFrame({'Rate': [Decimal('77.5104'), Decimal('77.2535'), Decimal('75.6826')],
rates_usd = pd.DataFrame({'Close': [Decimal('77.5104'), Decimal('77.2535'), Decimal('75.6826')],
'Date': [d2dt(210413), d2dt(210414), d2dt(210415)]})
rates_usd = rates_usd.set_index('Date')
rates_downloaded = downloader.CBR_DataReader(JalAsset(2), 1618272000, 1618358400)
assert_frame_equal(rates_usd, rates_downloaded)

rates_try = pd.DataFrame({'Rate': [Decimal('9.45087'), Decimal('9.49270'), Decimal('9.37234')],
rates_try = pd.DataFrame({'Close': [Decimal('9.45087'), Decimal('9.49270'), Decimal('9.37234')],
'Date': [d2dt(210413), d2dt(210414), d2dt(210415)]})
rates_try = rates_try.set_index('Date')
rates_downloaded = downloader.CBR_DataReader(JalAsset(4), 1618272000, 1618358400)
assert_frame_equal(rates_try, rates_downloaded)

def test_ECB_downloader(prepare_db):
rates_usd = pd.DataFrame({'Rate': [Decimal('0.8406186954'), Decimal('0.8358408559')],
rates_usd = pd.DataFrame({'Close': [Decimal('0.8406186954'), Decimal('0.8358408559')],
'Date': [d2dt(210413), d2dt(210414)]})
rates_usd = rates_usd.set_index('Date')
downloader = QuoteDownloader()
Expand Down

0 comments on commit fcf8259

Please sign in to comment.