mortada / fredapi Goto Github PK
View Code? Open in Web Editor NEWPython API for FRED (Federal Reserve Economic Data) and ALFRED (Archival FRED)
License: Apache License 2.0
Python API for FRED (Federal Reserve Economic Data) and ALFRED (Archival FRED)
License: Apache License 2.0
Hi,
it would have been really nice if you could add to Fred()
call also proxy settings.
Thanks
Is there anything to pull tags for a series data
Getting below error message when calling Libor rates series ids from fredapi.
It was working just fine last week. Start occurring error after 09/13/2020.
Traceback (most recent call last):
File "C:\Users\amylin\Desktop\DevOp\FR\venv\lib\site-packages\fredapi\fred.py", line 64, in __fetch_data
response = urlopen(url)
File "C:\Users\amylin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\amylin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\amylin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\amylin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\amylin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\amylin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\amylin\Desktop\DevOp\ATG.FinanceRates\venv\lib\site-packages\fredapi\fred.py", line 131, in get_series
root = self.__fetch_data(url)
File "C:\Users\amylin\Desktop\DevOp\ATG.FinanceRates\venv\lib\site-packages\fredapi\fred.py", line 68, in __fetch_data
raise ValueError(root.get('message'))
ValueError: Not Found. The series does not exist.
fred_endpoint.get_series_all_releases(series_id)
Call fred_endpoint.get_series_all_releases(series_id)
with the following series_id:
Return historical rates from Fred
Windows-10-10.0.17134 Build 17134
Python 3.7.3
fredapi 0.4.2
I have forked the master to dig into where the issue is coming from.
I directly did a request toward Fred API endpoint and it is still returning bad request.
From my observation/investigation, this is caused by Fred API instead of the package.
I've notified Federal Reserve Bank of St Louis about the issue.
the dataset I cannot load is called:
100*(Real Gross Domestic Product-Real Potential Gross Domestic Product)/Real Potential Gross Domestic Product
Dear fellow users,
I am getting the following error when using fredapi:
Traceback (most recent call last):
File "/xyz/xyz/.local/lib/python3.8/site-packages/fredapi/fred.py", line 64, in __fetch_data
response = urlopen(url)
File "/usr/lib/python3.8/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.8/urllib/request.py", line 531, in open
response = meth(req, response)
File "/usr/lib/python3.8/urllib/request.py", line 640, in http_response
response = self.parent.error(
File "/usr/lib/python3.8/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/usr/lib/python3.8/urllib/request.py", line 502, in _call_chain
result = func(*args)
File "/usr/lib/python3.8/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/xyz/xyz/xyz.py", line 18, in
DataSetBuilder_1()
File "/xyz/xyz/xyz/xyz.py", line 150, in DataSetBuilder_1
x=fred.get_series(x, columns=[y])
File "/xyz/xyz/.local/lib/python3.8/site-packages/fredapi/fred.py", line 131, in get_series
root = self.__fetch_data(url)
File "/xyz/xyz/.local/lib/python3.8/site-packages/fredapi/fred.py", line 68, in __fetch_data
raise ValueError(root.get('message'))
ValueError: Not Found. The series does not exist.
Does anybody have a clue of what this is about? Thanks in advance!
Hi All,
I am attempting to retrieve data in the following manner:
from fredapi import Fred
fred = Fred(api_key = 'MyAPIKey')
data = fred.get_series('SP500')
But doing so results in the error:
'xml.etree.ElementTree.Element' object has no attribute 'getchildren'
Is this due to a missing dependency and is there a workaround available?
Hi
I read the blog you write, it is really helpful.
I am thinking about get all the data observations by iterating through all the series_ids, like this:
-------https://api.stlouisfed.org/fred/series/observations?series_id=[for i in all series_ids]&api_key=************---------
Do you think its possible to do so? If it is , is there any good methods you recommend?
Thanks
Yiwei
Basically, I am having trouble writing columns to an Excel file from a series in Fred. My issue is in the last line of the dataget function, where I have tried many things, and each give an error.
from fredapi import Fred
import pandas as pd
import time
#key = blah
API_KEY = "blah"
fred = Fred(api_key = API_KEY)
#Format = (id, description)
data_list = [('SP500', 'Stock Exchange Index'), ('UNRATE', 'Civilian Unemployment Rate')]
def dataget (id,description):
#get a series
values = fred.get_series(id, observation_start='1/01/2012')
#make an Excelwriter in a workbook called "FRED_DATA"
writer = pd.ExcelWriter('FRED_Data', engine='xlsxwriter')
#Create a custom sheet with (`description` + data) as the worksheet name.
values.to_excel(writer, str(description + " Data"))
#BUT I WANT THAT LINE^ TO INCLUDE COLUMNS, so something like this...
#values.to_excel(writer, str(description + "data"),columns = (date, description))
#Break each (series_id, name) tupple into a an (id,description) pair
for tupple in data_list:
id = tupple[0]
description = tupple[1]
#call dataget on this pair
dataget(id,description)
`
I put 600+ series_id in a text file and let Python loop through it.
There occurred error 'HTTPError: HTTP Error 400: Bad Request' & 'HTTPError: HTTP Error 429: Too Many Requests'
It won't fix even I use sleep.
Then I tested with 100 series. It ran fine without any error.
I also tested Excel Add-in provided by Fred. The maximum # of download is up to column GR, that's 174.
However API can't download 174 series without error message.
Can I ask what's is the maximum of series API can retrieve per connection request?
Is there a way to download 1000+ series without error?
Thanks,
Mindy
Hello! I love fredapi and depend on it to pull the 3000+ series related to equifax subprime credit scores. With the new edition of pandas, .append is officially deprecated in favor of .concat. I believe there are three instances of .append in the fredapi code, and would deeply appreciate if they could be edited to .concat so fredapi could continue to operate in the future.
for child in root:
dates.append(self._parse(child.text))
for child in root:
num_results_returned += 1
series_id = child.get('id')
series_ids.append(series_id)
if max_results_needed > self.max_results_per_request:
for i in range(1, max_results_needed // self.max_results_per_request + 1):
offset = i * self.max_results_per_request
next_data, _ = self.__do_series_search(url + '&offset=' + str(offset))
data = data.append(next_data)
I am still new to the backend of python packages and this is my first issue post, so apologies if this is either too complicated or something I should be able to do myself. Thank you again and please let me know if you have any questions or if there's anything I can do!
So not sure if I made a mistake last week but I was getting the following error when retrieving info about the SP500 with Python 2.7.10 (Python 3.4 was fine):
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position 239
: ordinal not in range(128)
I can't be sure because I could not reproduce the problem today outside of the fake tests, but I figured the problem lied with the conjunction of (i) somehow getting an ascii response though it is advertised as being UTF-8 encoded, and (ii) having the copyright sign in the response which is the only non-ascii character I've seen from FRED.
Just putting it out there in case somebody noticed the issue too. I have a patch ready if the issue is verified (https://github.com/elmotec/fredapi/tree/fix_non_utf_chars_in_response).
Hello @mortada; thanks for a really great library! I noticed an interesting issue -- I think FRED has a limitation on vintage dates for a query (I believe it's 2000). I ran into this when trying to call get_series_all_releases
with series_id='DFF'
(Effective Fed Funds Rate, published daily). As is, there wasn't a good way to get around it since the realtime_start
and realtime_end
parameters are class variables instead of being passed in. I believe the code just errors out (i.e. it doesn't even provide a partial data set) .
Here is my proposal for a solution; see if it makes sense:
realtime_start
and realtime_end
to be arguments (defaulted to None so it doesn't break anyone's code)self.realtime_start
and self.realtime_end
to new valuesget_series_vintage_dates
on the series_id
and see if there are more than 2,000 values within the self.realtime_start
and self.realtime_end
valuesIf you agree with the idea, I'm happy to code it up and submit a pull request! Thank you.
Using Python 3.9, fredapi does not work any longer because it uses the function getchildren() for xml.etree.Elements. This function is deprecated and has been removed with Python 3.9.
I created a pull request to fix it:
Solution: Just remove all getchildren() calls and change to iteration, e.g in get_series():
for child in root.getchildren(): ...
change to:
for child in root: ...
See https://docs.python.org/3.8/library/xml.etree.elementtree.html and https://docs.python.org/3.9/library/xml.etree.elementtree.html, search for getchildren()
Received this error when using the get_series method. Worked fine previously but get this error code now: URLError: <urlopen error [Errno 11001] getaddrinfo failed>
If you want to get multiple series in dataframe, you have to do something like this:
df = pd.DataFrame()
for series in ['VIXCLS', 'SP500']:
s = fred.get_series(series)
s.name = series
df = df.join(s, how='outer')
I wonder if we can add a method to allow you to get multiple series at once.
Noticed there's a PR (#15), but seemed like it contains more than one changes.
Dear developer,
I want to request 5 datasets, in total there are probably 2000 data points. Sometimes I can get them all without any issue. However, most of times after getting 2 or 3 datasets, the API threw an error "The value for variable api_key is not registered.". I googled online and it seems most people can get way more data points than me without a problem.
Here is how I query all my five datasets.
fred = Fred(api_key='xxxxxxxxxxxxxxxxx')
fetch_start_date='2000-01-01'
UMich_CCI_df = fred.get_series('UMCSENT', observation_start=fetch_start_date)
time.sleep(5)
Gas_Price_df = fred.get_series('GASREGCOVM', observation_start=fetch_start_date)
time.sleep(5)
GDP_Annual_Rate = fred.get_series('CPGDPAI', observation_start=fetch_start_date)
time.sleep(5)
Housing_Starts_df = fred.get_series('HOUST', observation_start=fetch_start_date)
time.sleep(5)
Unemployment_Rate_df = fred.get_series('UNRATE', observation_start=fetch_start_date)
I registered my API yesterday and pass it directly as the 'api_key' parameter. I am wondering if I am doing anything wrong here so I cannot always get all datasets.
I use Python 3.6 in conda environment. the Pandas version is 1.1.5.
I am sure this is not a bug in your repo but I just couldn't find a solution and don't understand why. Really appreciate if you can provide any help.
Best regards,
Api worked always fine but now gives error
Urlopen error SSL: CERTIFICATE_VERIFY_FAILED certificate verify failed: certificate has expired (_ssl.c:1129)
I could not find a way to correct this error
Just a standard example gives an error:
from fredapi import Fred
fred = Fred(api_key='..')
data = fred.get_series('GDP')
tried restarting and playing around with various date import commands, but the simple code
Traceback (most recent call last):
File "", line 254, in run_nodebug
File "..FredDL.py", line 32, in
data = fred.get_series('GDP')
File "C:\Python27\lib\site-packages\fredapi\fred.py", line 132, in get_series
data[self._parse(child.get('date'))] = val
File "C:\Python27\lib\site-packages\fredapi\fred.py", line 65, in _parse
return to_datetime(date_str, format=format).to_datetime()
AttributeError: 'datetime.datetime' object has no attribute 'to_datetime'
Hi mortada,
I just found your conda install fredapi for new version 5.0 but it doesn't work.
https://anaconda.org/piiq/fredapi
It announces PackagesNotFoundError: The following packages are not available from current channels.
Could you check it?
Appreciate your supporting.
Using clean Anaconda install with JupyterLab notebook in a dedicated virtual environment:
from fredapi import Fred
fred = Fred(api_key='#####')
data = fred.get_series('SP500')
AttributeError Traceback (most recent call last)
Cell In[7], line 1
----> 1 data = fred.get_series('SP500')
File ~\anaconda3\envs\venv_1\Lib\site-packages\fredapi\fred.py:135, in Fred.get_series(self, series_id, observation_start, observation_end, **kwargs)
133 raise ValueError('No data exists for series id: ' + series_id)
134 data = {}
--> 135 for child in root.getchildren():
136 val = child.get('value')
137 if val == self.nan_char:
AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getchildren'
I am still quite new to python, and suppose it is something obvious, but I have no idea why this isn't working since it looks very straight forward in all the docs.
Any help would be appreciated!
@mortada, one more thing I'd like to fix. When one calls get_series with realtime_start or realtime_end, the argument is passed verbatim and if the format is not a valid YYYY-MM-DD date, the url is invalid and Fred rejects the request.
I'd like to handle realtime_start and realtime_end the same way as observation_start and observation_end. Then if any of the 2 realtime argument is specified, return a pandas.DataFrame instead of a pandas.Series with a pandas.MultiIndex composed of the observation dates as well as the realtime start and realtime end.
I recognize that it's a little bit redundant with the other functions of the Fred class (e.g. get_series_all_releases) but it provides more flexibility like asking for revisions during a specific time span (say 3M at the begining of the year). I also think that having both realtime_start and realtime_end in the index makes it easier to manipulate the data to find what was the value of the series at a particular point in time.
I've uploaded my branch to https://github.com/elmotec/fredapi/tree/get_series_with_realtime (see TestFred.test_get_series_with_realtime for an example). Let me know what you think.
I also think it would make sense to even be able to request multiple series and leverage pandas to align the dates. That would be my next enhancement.
How do I get the api to give me data that has been adjusted to "percentage change from a year ago"? for example: https://fred.stlouisfed.org/series/GDP ? thank you
Hi, I noticed setup.py fails on the following in fred.py:
line 56 except HTTPError, exc:
Should this be
except HTTPError as exc:
>>> from fredapi import Fred
>>> fred = Fred(api_key_file='api_key_file.txt')
>>> fred.search('Real GDP')
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1270-01-01 00:00:00
Having dates outside of the limitations of pd.TimeStamp causes the problem. In the above search, we get back a time series (HPGDPUKA) that goes back to 1270, so fails to be searched for.
In fredapi\fred.py
there is a line rv = pd.to_datetime(date_str, format=format)
, where the bug happens. If this line is replaced with rv = pd.to_datetime(date_str, errors='ignore', format=format)
, this would solve the issue (at the cost of having a object index in such cases, instead of DateTimeIndex).
Hi,
Firstly, thank you to anyone that can help me solve this issue.
I am using Anaconda2.7 with Spyder. I have installed pandas and fredapi through the Anaconda Promp using 'Conda install pandsas' and 'Conda install fredapi' You can see this at the bottom of the attachment.
I can confirm that some things work because the "get_series_info('GDP')" does return the correct information.
However, when using the command "get_series('GDP')" I continually get the following error:
File "G:/Financial Modelling/Python Scripts/LIBOR3M.py", line 11, in
s = fred.get_series('SP500')
File "C:\Users\Chase.French\AppData\Local\Continuum\anaconda2\lib\site-packages\fredapi\fred.py", line 129, in get_series
data[self._parse(child.get('date'))] = val
File "C:\Users\Chase.French\AppData\Local\Continuum\anaconda2\lib\site-packages\fredapi\fred.py", line 64, in _parse
return pd.to_datetime(date_str, format=format).to_datetime()
AttributeError: 'Timestamp' object has no attribute 'to_datetime'
I would really appreciate any help that can be provided!
When I try to get "Dot Plot" related data from ALFRED with vintage_all = fred.get_series_all_releases('FEDTARMD')
the output seems off. For example for the median federal funds rate projection for the FOMC meeting 2017-03-15, we should get three datapoints: 2017: 1.4, 2018: 2.1, 2019: 3.0, which is what you get when you download all vintages for 'FEDTARMD' into a zipped excel from ALFRED. But for some reason the api call gives me a NaN for 2016 and 3 for 2019, both these values are fine, but 2017 and 2018 projections are missing. Either I'm doing something wrong or the api call does not handle this data output correctly.
When fetching for T5YIFR, for instance, there's an error.
OBS: using get_series_all_releases() method
Bad Request. There are 2004 vintage dates in the specified real-time period: 1776-07-04 to 9999-12-31. This exceeds the maximum number of vintage dates allowed (2000).
Maybe the solution would be to add a date range for this method? Or option to increase the number of vintages dates?
Thanks
Are the LIBOR rates exluded from the Fred API Terms and Agreements? Is there any way to access the indicator?
How to integrate FRED API with ZOHO Api
Hi - I am getting the error below when trying to run a simple data pull using fredapi
Anybody out there kind enough to help identify the issue?
Thanks!
CODE
import pandas as pd
from fredapi import Fred
api_key = 'my_key'
fred = Fred(api_key = api_key)
series_data = fred.get_series('SP500')
print(series_data)
ERROR
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 1348, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1282, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1328, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1277, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1037, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 975, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1454, in connect
self.sock = self._context.wrap_socket(self.sock,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 517, in wrap_socket
return self.sslsocket_class._create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 1075, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 1346, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:992)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/PycharmProjects/Email/venv/lib/python3.11/site-packages/fredapi/fred.py", line 64, in __fetch_data
response = urlopen(url)
^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 216, in urlopen
return opener.open(url, data, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 519, in open
response = self._open(req, data)
^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 536, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 496, in _call_chain
result = func(*args)
^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 1391, in https_open
return self.do_open(http.client.HTTPSConnection, req,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 1351, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:992)>
python-BaseException
Process finished with exit code 1
Similar to what is described here: https://fredhelp.stlouisfed.org/fred/graphs/customize-a-fred-graph/data-transformation-add-series-to-existing-line/
Hello, I am wondering if it is possible to query the fredapi for the suggested citation of a certain variable? For example, for the FRED identifier PSAVERT, the browser URL is https://fred.stlouisfed.org/series/PSAVERT, and the suggested citation is "U.S. Bureau of Economic Analysis, Personal Saving Rate [PSAVERT], retrieved from FRED, Federal Reserve Bank of St. Louis; https://fred.stlouisfed.org/series/PSAVERT, December 25, 2022," as shown in the notes section of the website. Is there a way to pass the PSAVERT identifier to a fredapi function, and get back this suggested citation? Thanks!
Putting both pandas and fredapi in a "requirements.txt" file and then installing in a fresh virtualenv leads to:
Collecting fredapi (from -r requirements.txt (line 15))
Using cached fredapi-0.4.0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-ej66i5se/fredapi/setup.py", line 3, in <module>
from fredapi.version import version as __version__
File "/tmp/pip-build-ej66i5se/fredapi/fredapi/__init__.py", line 3, in <module>
from fredapi.fred import Fred
File "/tmp/pip-build-ej66i5se/fredapi/fredapi/fred.py", line 14, in <module>
import pandas as pd
ImportError: No module named 'pandas'
Can the version.py file go outside the fredapi directory?
I wrote a client for FRED like fredpi
except I used c#/dotnet. In the course of writing my client I came across some ambiguities in the FRED documentation. I also found some cases where the data I received from the API was not consistent with the documented API.
I wrote the FRED team several times and their responses only raised more questions. I eventually wound up creating my own definitions and writing my code against my own spec. I have long wondered, however, if I am not understanding the documentation correctly or if perhaps I am not using the API in the way it is intended to be used. After running a few queries with fredapi
I see some cases where the same the same anomalies I found in the API are apparent in the the results returned by fredapi
.
1.) Why does get latest data known on a given date return multiple rows for a given observation date? I would expect that since we are querying for latest we would only see one row e.g. the latest one. For the example shown here I would expect to see only these rows:
2013-10-01 2014-03-27 17089.6
2014-01-01 2014-05-29 17101.3
2.) Not all vintage dates are returned for a realtime query that spans multiple vintage periods:
fredapi
documentation:
get_series_all_releases(self, series_id, realtime_start=None, realtime_end=None)
Get all data for a Fred series id including first releases and all revisions. This returns a DataFrame
with three columns: 'date', 'realtime_start', and 'value'. For instance, the US GDP for Q4 2013 was first released
to be 17102.5 on 2014-01-30, and then revised to 17080.7 on 2014-02-28, and then revised to 17089.6 on
2014-03-27. You will therefore get three rows with the same 'date' (observation date) of 2013-10-01 but three
different 'realtime_start' of 2014-01-30, 2014-02-28, and 2014-03-27 with corresponding 'value' of 17102.5, 17080.7
and 17089.6
This query:
fred.get_series('GDP', realtime_start='2014-01-28', realtime_end='2014-03-30', observation_start='2013-10-01', observation_end='2013-10-01')
returns only one result:
2013-10-01 17089.6
Based on the spreadsheet for GDP downloaded from Alfred, three vintages for observation period 2013-10-01 were released between 2014-01-28 and 2014-03-30:
2014-01-30 17102.5
2014-02-28 17080.7
2014-03-27 17089.6
Again note that the fredapi
documentation says "...including first releases and all revisions" therefore I would expect to see all three vintages. Also, note that the date returned by the query is not a vintage date but instead appears to be the observation period.
Is the result of this query correct? Based on FRED documentation, result from the query above demonstrates what I believe is a defect in the FRED API (not a defect in fredapi
). I believe FRED has co-mingled the concepts of realtime periods and vintage dates. As a result, queries such as the one above return meaningless or inconsistent results. I posted a fairly in-depth analysis of my findings I will provide a link if requested.
H,
I would like to know if anyone is able to retrieve this serie : 'EUR3MTD156N'
from https://fred.stlouisfed.org/series/EUR3MTD156N
I still get this error:
*** ValueError: Not Found. The series does not exist.
and i don't know why.
Thanks in advance,
I created a docker fredapi image for my own use, but if you'd like to incorporate the Dockerfile into this project please feel free to. Or to test out the image just try out the devshell: docker run -it erezbinyamin/fredapi python
FROM python:3.6
RUN apt-get update
RUN git clone https://github.com/mortada/fredapi.git \
&& cd fredapi \
&& pip install .
Hello,
I work on FRED at the St. Louis Fed. Beginning on August 18, 2015, the FRED API will require HTTPS requests. This change will help provide secure communication with the FRED API. An automatic redirect will forward HTTP requests to HTTPS. We recommend that you update the URLs in your code. The API currently supports HTTPS to allow you to test your applications with this secure protocol.
Please contact us at [email protected] or 314-444-FRED (3733) if you have questions or concerns. Thanks for using FRED and the FRED API.
Sincerely,
Keith Taylor
[email protected]
I have been using fredapi to download data from FRED API without problem. But I've been trying to run the same codes that I had before in these two days and I keep getting something called "gaierror".
Here is the codes that I ran (I just masked the API key):
from fredapi import Fred
fred = Fred(api_key='xxxxxxxxxxxxxxxxxxxxxxxxx')
import pandas as pd
data = fred.get_series('SP500')
and I got the following error. Can anyone help me to understand what error it is and how to solve it? Thanks a lot.
gaierror Traceback (most recent call last)
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1253 try:
-> 1254 h.request(req.get_method(), req.selector, req.data, headers)
1255 except OSError as err: # timeout error
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/http/client.py in request(self, method, url, body, headers)
1106 """Send a complete request to the server."""
-> 1107 self._send_request(method, url, body, headers)
1108
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/http/client.py in _send_request(self, method, url, body, headers)
1151 body = _encode(body, 'body')
-> 1152 self.endheaders(body)
1153
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/http/client.py in endheaders(self, message_body)
1102 raise CannotSendHeader()
-> 1103 self._send_output(message_body)
1104
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/http/client.py in _send_output(self, message_body)
933
--> 934 self.send(msg)
935 if message_body is not None:
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/http/client.py in send(self, data)
876 if self.auto_open:
--> 877 self.connect()
878 else:
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/http/client.py in connect(self)
1252
-> 1253 super().connect()
1254
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/http/client.py in connect(self)
848 self.sock = self._create_connection(
--> 849 (self.host,self.port), self.timeout, self.source_address)
850 self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/socket.py in create_connection(address, timeout, source_address)
693 err = None
--> 694 for res in getaddrinfo(host, port, 0, SOCK_STREAM):
695 af, socktype, proto, canonname, sa = res
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/socket.py in getaddrinfo(host, port, family, type, proto, flags)
732 addrlist = []
--> 733 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
734 af, socktype, proto, canonname, sa = res
gaierror: [Errno -2] Name or service not known
During handling of the above exception, another exception occurred:
URLError Traceback (most recent call last)
in ()
----> 1 data = fred.get_series('SP500')
/home/nbkz19v/.local/lib/python3.5/site-packages/fredapi/fred.py in get_series(self, series_id, observation_start, observation_end, **kwargs)
129 if kwargs.keys():
130 url += '&' + urlencode(kwargs)
--> 131 root = self.__fetch_data(url)
132 if root is None:
133 raise ValueError('No data exists for series id: ' + series_id)
/home/nbkz19v/.local/lib/python3.5/site-packages/fredapi/fred.py in __fetch_data(self, url)
62 url += '&api_key=' + self.api_key
63 try:
---> 64 response = urlopen(url)
65 root = ET.fromstring(response.read())
66 except HTTPError as exc:
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
161 else:
162 opener = _opener
--> 163 return opener.open(url, data, timeout)
164
165 def install_opener(opener):
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/urllib/request.py in open(self, fullurl, data, timeout)
464 req = meth(req)
465
--> 466 response = self._open(req, data)
467
468 # post-process response
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/urllib/request.py in _open(self, req, data)
482 protocol = req.type
483 result = self._call_chain(self.handle_open, protocol, protocol +
--> 484 '_open', req)
485 if result:
486 return result
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
442 for handler in handlers:
443 func = getattr(handler, meth_name)
--> 444 result = func(*args)
445 if result is not None:
446 return result
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/urllib/request.py in https_open(self, req)
1295 def https_open(self, req):
1296 return self.do_open(http.client.HTTPSConnection, req,
-> 1297 context=self._context, check_hostname=self.check_hostname)
1298
1299 https_request = AbstractHTTPHandler.do_request
/storage/EIX_LAB_NH_1/install/anaconda3/lib/python3.5/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1254 h.request(req.get_method(), req.selector, req.data, headers)
1255 except OSError as err: # timeout error
-> 1256 raise URLError(err)
1257 r = h.getresponse()
1258 except:
URLError: <urlopen error [Errno -2] Name or service not known>
This category for e.g.
https://fred.stlouisfed.org/categories/33003
fred.search_by_category(33003)
ValueError: No series exists for category id: 33003
Why aren't all categories accessible?
========================================================================================== test session starts ==========================================================================================
platform freebsd13 -- Python 3.9.16, pytest-7.2.0, pluggy-1.0.0 -- /usr/local/bin/python3.9
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/usr/ports/finance/py-fredapi/work-py39/fredapi-0.5.0/.hypothesis/examples')
Using --randomly-seed=3873099282
rootdir: /usr/ports/finance/py-fredapi/work-py39/fredapi-0.5.0
plugins: forked-1.4.0, hypothesis-6.56.2, cov-2.9.0, randomly-3.12.0, timeout-2.1.0, freezegun-0.4.2, rerunfailures-10.1, flaky-3.7.0, xdist-2.5.0, env-0.6.2, mock-1.10.4, anyio-3.6.1, subtests-0.8.0
collected 6 items
fredapi/tests/test_fred.py::TestFred::test_invalid_kwarg_in_get_series PASSED [ 16%]
fredapi/tests/test_fred.py::TestFred::test_get_series_info_payem PASSED [ 33%]
fredapi/tests/test_fred.py::TestFred::test_get_series PASSED [ 50%]
fredapi/tests/test_fred.py::TestFred::test_search SKIPPED (Not sure why this crashes in some environments, skipping) [ 66%]
fredapi/tests/test_fred.py::TestFred::test_invalid_id_in_get_series PASSED [ 83%]
fredapi/tests/test_fred.py::TestFred::test_invalid_id_in_get_series_info FAILED [100%]Error in sys.excepthook:
Original exception was:
*** Error code 1
There's no particular error message.
Version: 0.5.0
Python-3.9
FreeBSD 13.1
When downloading data with Python 3.12, the data fetching results in an error. It looks like that FredAPI is using getchildren() method, which is no longer supported by Python >3.9. This method was used in the xml.etree.ElementTree library but has been deprecated.
This has been raised previously by other users e.g. in https://community.anaconda.cloud/t/fredapi-error-using-get-series-method-from-fredapi-package/51231previously
I wanted to use fredapi in my Anaconda installation so I made a conda skeleton from the PyPI package and then built the skeleton. The conda build script was successful and the new fredapi package tested OK:
#truncated
===== testing package: fredapi-0.3.4-py27_0 =====
import: u'fredapi'
===== fredapi-0.3.4-py27_0 OK =====
TEST END: fredapi-0.3.4-py27_0
Then I tried installing and this, too, appeared successful.
$ conda install --use-local fredapi
Fetching package metadata: ........
Solving package specifications: .
Package plan for installation in environment /usr/local/share/anaconda:
The following NEW packages will be INSTALLED:
fredapi: 0.3.4-py27_0
Linking packages ...
[ COMPLETE ]|###################################################| 100%
$ conda list fredapi
# packages in environment at /usr/local/share/anaconda:
#
fredapi 0.3.4 py27_0
But the interpreter in this environment does't recognize the new conda package.
Python 2.7.9 |Anaconda 2.2.0 (x86_64)| (default, Dec 15 2014, 10:37:34)
IPython 3.1.0 -- An enhanced Interactive Python.
In [1]: import fredapi
Traceback (most recent call last):
File "<ipython-input-1-462d15ed0c18>", line 1, in <module>
import fredapi
ImportError: No module named fredapi
None of conda's troubleshooting suggestions apply to this case. Do you have any suggestions for properly building a conda package for fredapi?
After installing the latest version of fredapi and running the following code (from the library homepage):
from fredapi import Fred
fred = Fred(api_key=fred_key)
data = fred.get_series('SP500')
I received the following error message:
[AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getchildren']()
It seems as though the API is retrieving XML data. How can I resolve this?
Got the following error when searching for "Real GDP" with fred.search:
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1270-01-01 00:00:00
I identified the cause on the following stack overflow submission:
Basically, dates before 1677 are not allowed. My guess is that the above is a typo somewhere on FRED's side, and the timestamp should be 1970-01-01. Indeed, when I print the dates approaching the glitch they are as follows:
2003-01-01
2002-01-01
1930-01-01
1997-01-01
1996-01-01
1270-01-01
^error occurs here
I identified the problem in the following file on line 75:
/home/user/anaconda3/lib/python3.7/site-packages/fredapi/fred.py
and the fix I crafted is to add this function to the class:
def _check_date_format(self,date_str):
datetime = date_str.split('-')
year = datetime[0]
if (float(year[1]) < 7) and (float(year[1]) > 0):
year = list(year)
year[1] = '9'
year = ''.join(year)
datetime = [year,datetime[1],datetime[2]]
datetime = '-'.join(datetime)
return datetime
and change the _parse function as follows
def _parse(self, date_str, format='%Y-%m-%d'):
"""
helper function for parsing FRED date string into datetime
"""
try:
rv = pd.to_datetime(date_str, format=format)
except:
date_str = self._check_date_format(date_str)
rv = pd.to_datetime(date_str, format=format)
if hasattr(rv, 'to_pydatetime'):
rv = rv.to_pydatetime()
return rv
I hope that you can add this to the fredapi wrapper code master! This fix should work until 2100 lol.
Hello,
First of all I wanted to say this is a great package! However I seem to get an error when running fred.search_by_category(33003)
which corresponds to getting LIBOR rates from the FRED site. In particular I get ValueError: No series exists for category id: 33003
when the series do exist and can be found at: https://fred.stlouisfed.org/categories/33003
When i download the Fed Funds Rate, Range, Low (FEDTARRL) using fred.get_series_all_releases('FEDTARRL')
, few of the data points are missing as an example for 12/13/2023 and 3/20/2024 - 2026 rate is missing.
The following snippet doesn't work for me:
from fredapi import Fred
fred = Fred(api_key='insert api key here')
data = fred.get_series('SP500')
I get the following error:
from fredapi import Fred
Traceback (most recent call last):
File "", line 1, in
from fredapi import Fred
File "C:", line 8, in
from fredapi import Fred
ImportError: cannot import name 'Fred'
When using this command:
data = fred.get_series_first_release('GDP')
data.tail()
I get this error: C:\Users\Valued Customer\Anaconda3\lib\site-packages\fredapi\fred.py:242: FutureWarning: to_datetime is deprecated. Use self.to_pydatetime() realtime_start = self._parse(child.get('realtime_start'))
C:\Users\Valued Customer\Anaconda3\lib\site-packages\fredapi\fred.py:244: FutureWarning: to_datetime is deprecated. Use self.to_pydatetime() date = self._parse(child.get('date'))
Does your code need to be updated?
Hi,
I have been trying to fetch series using fred.get_series(code, observation_start=(from_date), observation_end=(to_today), units=units_used, frequency=frequency_used)
. The series are in a dictionary and I'm using the aforementioned bit in a loop, then writing the result in an excel file. Everything works great, however, the latest date I am receiving is "2022-12-01" instead of January and February 2023 data that DO exist for most of the series I am fetching (the monthly ones). My start date is 2020 but even If I put 2022, it would still end on December that year. Changing the "observation_end" from "to_today" to a static 2023-02-28 per example also did not work. Creating a new API key also yielded the same results.
Is there a reason why it is stopping at the end of 2022?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.