Skip to content

ENH: accept mutliple series for FRED DataReader #5492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ Improvements to existing features
option it is no longer possible to round trip Excel files with merged
MultiIndex and Hierarchical Rows. Set the ``merge_cells`` to ``False`` to
restore the previous behaviour. (:issue:`5254`)
- The FRED DataReader now accepts multiple series (:issue`3413`)

API Changes
~~~~~~~~~~~
Expand Down
4 changes: 3 additions & 1 deletion doc/source/remote_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ FRED
gdp=web.DataReader("GDP", "fred", start, end)
gdp.ix['2013-01-01']


# Multiple series:
inflation = web.DataReader(["CPIAUCSL", "CPILFESL"], "fred", start, end)
inflation.head()
.. _remote_data.ff:

Fama/French
Expand Down
44 changes: 29 additions & 15 deletions pandas/io/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
import pandas.compat as compat
from pandas import Panel, DataFrame, Series, read_csv, concat
from pandas.core.common import PandasError
from pandas.core.common import is_list_like, PandasError
from pandas.io.parsers import TextParser
from pandas.io.common import urlopen, ZipFile, urlencode
from pandas.util.testing import _network_error_classes
Expand All @@ -41,8 +41,9 @@ def DataReader(name, data_source=None, start=None, end=None,

Parameters
----------
name : str
the name of the dataset
name : str or list of strs
the name of the dataset. Some data sources (yahoo, google, fred) will
accept a list of names.
data_source: str
the data source ("yahoo", "google", "fred", or "ff")
start : {datetime, None}
Expand Down Expand Up @@ -436,24 +437,37 @@ def get_data_fred(name, start=dt.datetime(2010, 1, 1),
Date format is datetime

Returns a DataFrame.

If multiple names are passed for "series" then the index of the
DataFrame is the outer join of the indicies of each series.
"""
start, end = _sanitize_dates(start, end)

fred_URL = "http://research.stlouisfed.org/fred2/series/"

url = fred_URL + '%s' % name + '/downloaddata/%s' % name + '.csv'
with urlopen(url) as resp:
data = read_csv(resp, index_col=0, parse_dates=True,
header=None, skiprows=1, names=["DATE", name],
na_values='.')
try:
return data.truncate(start, end)
except KeyError:
if data.ix[3].name[7:12] == 'Error':
raise IOError("Failed to get the data. Check that {0!r} is "
"a valid FRED series.".format(name))
raise
if not is_list_like(name):
names = [name]
else:
names = name

urls = [fred_URL + '%s' % n + '/downloaddata/%s' % n + '.csv' for
n in names]

def fetch_data(url, name):
with urlopen(url) as resp:
data = read_csv(resp, index_col=0, parse_dates=True,
header=None, skiprows=1, names=["DATE", name],
na_values='.')
try:
return data.truncate(start, end)
except KeyError:
if data.ix[3].name[7:12] == 'Error':
raise IOError("Failed to get the data. Check that {0!r} is "
"a valid FRED series.".format(name))
raise
df = concat([fetch_data(url, n) for url, n in zip(urls, names)],
axis=1, join='outer')
return df

def get_data_famafrench(name):
# path of zip files
Expand Down
22 changes: 22 additions & 0 deletions pandas/io/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
import pandas.util.testing as tm
from numpy.testing import assert_array_equal

if compat.PY3:
from urllib.error import HTTPError
else:
from urllib2 import HTTPError

def _skip_if_no_lxml():
try:
Expand Down Expand Up @@ -422,6 +426,24 @@ def test_invalid_series(self):
name = "NOT A REAL SERIES"
self.assertRaises(Exception, web.get_data_fred, name)

@network
def test_fred_multi(self):
names = ['CPIAUCSL', 'CPALTT01USQ661S', 'CPILFESL']
start = datetime(2010, 1, 1)
end = datetime(2013, 1, 27)

received = web.DataReader(names, "fred", start, end).head(1)
expected = DataFrame([[217.478, 0.99701529, 220.544]], columns=names,
index=[pd.tslib.Timestamp('2010-01-01 00:00:00')])
expected.index.rename('DATE', inplace=True)
assert_frame_equal(received, expected, check_less_precise=True)

@network
def test_fred_multi_bad_series(self):

names = ['NOTAREALSERIES', 'CPIAUCSL', "ALSO FAKE"]
with tm.assertRaises(HTTPError):
DataReader(names, data_source="fred")

if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down