Registry / data / fredapi

fredapi

JSON →
library0.5.2pypypi✓ verified 24d ago

fredapi is a Python API for Federal Reserve Economic Data (FRED) from the St. Louis Fed, and also provides methods for analyzing point-in-time data from ALFRED. It leverages pandas for data output, returning data as Series or DataFrames. The current version is 0.5.2, with a release cadence of a few updates per year, primarily addressing Python and dependency compatibility.

pip install fredapi
INSTALL
IMPORT
SIG · FREDAPI
F
fredapi
datapythonv0.5.2
Install
7.7s avg
Import
1010ms
Disk
164MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 1.050s · 164.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 7.7s · import 0.970s · 157MB
164MB installed
● package 164MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Fred
from fredapi import Fred

Initialize the Fred client with your API key, then fetch or search for economic data series. Data is returned as a pandas Series or DataFrame.

import os from fredapi import Fred # Get a FRED API key from https://fred.stlouisfed.org/docs/api/api_key.html # Set it as an environment variable or pass directly. api_key = os.environ.get('FRED_API_KEY', 'YOUR_FRED_API_KEY') if not api_key or api_key == 'YOUR_FRED_API_KEY': print("Warning: FRED_API_KEY environment variable not set or placeholder used. Get a key from fred.stlouisfed.org.") # Using a dummy key to allow the example to run without immediate error, # but API calls will fail without a valid key. api_key = 'abcdefghijklmnopqrstuvwxyz123456' fred = Fred(api_key=api_key) # Fetch a specific economic data series (e.g., S&P 500) data = fred.get_series('SP500') print("Fetched S&P 500 data (last 5 entries):") print(data.tail()) # Search for data series search_results = fred.search('unemployment rate') print("\nSearch results for 'unemployment rate' (first 3):") print(search_results[['id', 'title']].head(3))
Debug
Known issues
gotchaAn API key is mandatory for all requests. You can obtain one for free from the FRED website. Ensure it's set as the 'FRED_API_KEY' environment variable or passed directly to the `Fred` constructor.
fix
Apply for a FRED API key at https://fred.stlouisfed.org/docs/api/api_key.html and configure it as an environment variable (`export FRED_API_KEY='your_key'`) or pass it directly: `fred = Fred(api_key='your_key')`.
affects: All versions
breakingOlder versions of fredapi (<0.5.1) used `DataFrame.append`, which is deprecated in newer pandas versions. Using such old fredapi versions with modern pandas will lead to `FutureWarning`s or errors.
fix
Upgrade fredapi to version 0.5.1 or newer: `pip install --upgrade fredapi`. This version replaced `DataFrame.append` with `pd.concat`.
affects: <0.5.1
breakingVersions of fredapi prior to 0.3.7 used HTTP for API calls. FRED's API primarily uses HTTPS. Using very old versions might result in connection errors if FRED no longer supports HTTP for certain endpoints.
fix
Upgrade fredapi to version 0.3.7 or newer: `pip install --upgrade fredapi`. This version switched to using HTTPS for all FRED API calls.
affects: <0.3.7
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'fredapi'
The 'fredapi' library has not been installed in the Python environment you are currently using.
fix
Install the 'fredapi' package using pip: `pip install fredapi` or using conda if in an Anaconda environment: `conda install -c conda-forge fredapi`.
ValueError: You need to set a valid API key.
The fredapi client was initialized without a valid FRED API key, which is required to authenticate requests to the FRED API.
fix
Obtain a free API key from the FRED website (fred.stlouisfed.org) and provide it during Fred object initialization (e.g., `Fred(api_key='YOUR_API_KEY')`), set it as an environment variable `FRED_API_KEY`, or save it to a file specified by `api_key_file`.
AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getchildren'
This error occurs in Python 3.9 and later because the `getchildren()` method of `xml.etree.ElementTree.Element` was deprecated in Python 3.2 and removed in Python 3.9, and the installed version of `fredapi` relies on this deprecated method for XML parsing.
fix
Upgrade 'fredapi' to a version compatible with Python 3.9+ (e.g., `pip install --upgrade fredapi`) or ensure you are using a Python version older than 3.9.
ImportError: cannot import name 'Fred' from 'fredapi'
This typically occurs if you have a local Python file named 'fredapi.py' in the same directory as your script, which shadows the installed 'fredapi' package, or if there's a circular import issue.
fix
Rename any local file named 'fredapi.py' to something else (e.g., 'my_fred_script.py') to avoid module shadowing, or ensure your import statement is `from fredapi import Fred` and not attempting to import a non-existent name.
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: ...
The FRED API key is missing, invalid, or incorrectly configured, preventing successful authentication with the FRED API.
fix
Obtain a valid FRED API key from the FRED website (fred.stlouisfed.org/docs/api/api_key.html) and provide it when initializing Fred or set it as an environment variable:
```python
# Option 1: Pass key directly
from fredapi import Fred
fred = Fred(api_key='YOUR_API_KEY')

# Option 2: Set as environment variable (e.g., export FRED_API_KEY='YOUR_API_KEY' in your shell)
# Then, fredapi will automatically pick it up:
# from fredapi import Fred
# fred = Fred()
```
Upgrade
Version history
0.5.2latest on PyPI · released May 5, 2024
Audit
Dependencies
pandasrequiredCore data structures (Series, DataFrame) for API responses.
Agent activity
46 hits · last 30 days
node
44
Amazon
1
Resources
fredapi — pip install fredapi · libregistry