Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Using PyFred API

This notebook gives additional use cases of the public library of PyFredAPI. Tulip has some custom functions that make things a bit easier, I explain how to use those in another notebook.

The author of the library has a tutorials here

To make FRED work you need to set up the key. You can use one I created for Kate (but with my email)

from dotenv import load_dotenv

load_dotenv()  # Should load a KEY named FRED_API_KEY from a .env file. Other
def show_info_on_id(id):
    category = pf.get_series_categories(series_id=id)
    info = pf.get_series_info(series_id=id)
    pprint(info)
    pprint(pf.get_series_categories(series_id=id))


def get_category_id_from_id(id):
    category = pf.get_series_categories(series_id=id)
    cat = category["categories"][0]["id"]
    return cat


def get_parent_id_from_id(id):
    category = pf.get_series_categories(series_id=id)
    parent_id = category["categories"][0]["parent_id"]
    return parent_id


def show_related_series(
    id,
    seasonal_adjustment="Seasonally Adjusted Annual Rate",
    popularity_min=1,
    all_details=False,
):
    # Note: all_related_series morphs here from a pyfredapi object to a list

    category_id = get_category_id_from_id(id)

    all_related_series = pf.get_category_series(category_id=category_id)
    # print(all_related_series)
    all_related_series = [
        series_info
        for series_info in all_related_series.values()
        if (series_info.seasonal_adjustment == seasonal_adjustment)
        and series_info.popularity >= popularity_min
    ]
    # print(all_related_series)
    all_related_series = sorted(
        all_related_series,
        key=operator.attrgetter("popularity"),
        reverse=True,
    )
    # print(all_related_series)
    if all_details:
        pprint(all_related_series)
    else:
        for series_info in all_related_series:
            print(f"{series_info.id}: {series_info.title}")

Income

Flow of funds

Notice that the structure of the code in fred is “BOGZ1” + Z1 CODE + “Q”/“A” etc.

Lets check that the trick works: