Pandas data reader update, getting FRED currency exchange rates

2.4k Views Asked by At

So, I was attempting to grab USD/CNY rates from FRED using Pandas with the following code I found on another stack exchange post:

import pandas.io.data as web
cny = wb.get_data_fred('DEXCHUS')

However I got an error-

ImportError: The pandas.io.data module is moved to a separate package (pandas-datareader). After installing the pandas-datareader package (https://github.com/pydata/pandas-datareader), you can change the import ``from pandas.io import data, wb`` to ``from pandas_datareader import data, wb``.

I followed the GitHub instructions to install pandas-datareader and then changed to from pandas.io import data, wb then when I try to run my code I get an error that the module has no get_data_fred.

How can I use the updated package to get the FRED data?

1

There are 1 best solutions below

0
On

There are a few different ways to get the FRED data. For the generic DataReader call, try (using version 0.5.0):

import pandas_datareader as pdr
cny = pdr.DataReader("DEXCHUS", "fred")

Or, more similar to the syntax you had previously:

from pandas_datareader import data
cny = data.get_data_fred("DEXCHUS")

The data.get_data_fred method just returns the following, which provides a third way to get your data:

# Connect to FRED report and read from connection
cny = data.FredReader("DEXCHUS").read()

All results should be identical, however I prefer the more general first call myself.