Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import unicode_literals | ||
|
||
import pandas.compat as compat | ||
|
||
from pandas_datareader.base import _BaseReader | ||
|
||
|
||
class BankOfCanadaReader(_BaseReader): | ||
"""Get data for the given name from Bank of Canada.""" | ||
|
||
_URL = 'http://www.bankofcanada.ca/valet/observations' | ||
|
||
@property | ||
def url(self): | ||
if not isinstance(self.symbols, compat.string_types): | ||
raise ValueError('data name must be string') | ||
|
||
return '{0}/{1}/csv'.format(self._URL, self.symbols) | ||
|
||
@property | ||
def params(self): | ||
return {'start_date': self.start.strftime('%Y-%m-%d'), | ||
'end_date': self.end.strftime('%Y-%m-%d')} | ||
|
||
@staticmethod | ||
def _sanitize_response(response): | ||
""" | ||
Clean up the response string | ||
""" | ||
data = response.text.split('OBSERVATIONS')[1] | ||
return data.split('ERRORS')[0].strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from datetime import date, timedelta | ||
|
||
import pytest | ||
|
||
import pandas_datareader.data as web | ||
from pandas_datareader._utils import RemoteDataError | ||
|
||
|
||
class TestBankOfCanada(object): | ||
|
||
@staticmethod | ||
def get_symbol(currency_code, inverted=False): | ||
if inverted: | ||
return 'FXCAD{}'.format(currency_code) | ||
else: | ||
return 'FX{}CAD'.format(currency_code) | ||
|
||
def check_bankofcanada_count(self, code): | ||
df = web.DataReader(self.get_symbol(code), 'bankofcanada', | ||
date.today() - timedelta(days=30), date.today()) | ||
assert df.size > 15 | ||
|
||
def check_bankofcanada_valid(self, code): | ||
symbol = self.get_symbol(code) | ||
df = web.DataReader(symbol, 'bankofcanada', | ||
date.today() - timedelta(days=30), date.today()) | ||
assert symbol in df.columns | ||
|
||
def check_bankofcanada_inverted(self, code): | ||
symbol = self.get_symbol(code) | ||
symbol_inverted = self.get_symbol(code, inverted=True) | ||
|
||
df = web.DataReader(symbol, 'bankofcanada', | ||
date.today() - timedelta(days=30), date.today()) | ||
df_i = web.DataReader(symbol_inverted, 'bankofcanada', | ||
date.today() - timedelta(days=30), date.today()) | ||
|
||
pairs = zip((1 / df)[symbol].tolist(), df_i[symbol_inverted].tolist()) | ||
assert all(a - b < 0.01 for a, b in pairs) | ||
|
||
def test_bankofcanada_usd_count(self): | ||
self.check_bankofcanada_count('USD') | ||
|
||
def test_bankofcanada_eur_count(self): | ||
self.check_bankofcanada_count('EUR') | ||
|
||
def test_bankofcanada_usd_valid(self): | ||
self.check_bankofcanada_valid('USD') | ||
|
||
def test_bankofcanada_eur_valid(self): | ||
self.check_bankofcanada_valid('EUR') | ||
|
||
def test_bankofcanada_usd_inverted(self): | ||
self.check_bankofcanada_inverted('USD') | ||
|
||
def test_bankofcanada_eur_inverted(self): | ||
self.check_bankofcanada_inverted('EUR') | ||
|
||
def test_bankofcanada_bad_range(self): | ||
with pytest.raises(RemoteDataError): | ||
web.DataReader('FXCADUSD', 'bankofcanada', | ||
date.today(), date.today() - timedelta(days=30)) | ||
|
||
def test_bankofcanada_bad_url(self): | ||
with pytest.raises(RemoteDataError): | ||
web.DataReader('abcdefgh', 'bankofcanada', | ||
date.today() - timedelta(days=30), date.today()) |