Skip to content

Utility API

Module Documentation

Overview

This program retrieves data on youth health from several sources and saves both unprocessed and processed versions of the data. Unprocessed versions of the data are saved in the original file format in which they were retrieved, such as .xlsx or JSON, while processed versions are saved as CSV and JSON. The sources include the U.S. Federal Reserve and the U.S. Census Bureau.

Requirements

A Federal Reserve Economic Data (FRED) API key is required to run this program and can be obtained from the FRED website. This key must be saved on a new line in the file secrets.txt with the following format:

FRED_API_KEY: <fred_api_key>

where <fred_api_key> is the FRED API key.

Installation

This file as well as related project files such as additional documentation are available in this project's repository on GitHub. To install the program from GitHub and change into the project directory:

git clone https://github.com/JimmyVanHout/youth_health_data_tool.git
cd youth_health_data_tool

Then, create a virtual environment and activate it:

python3 -m .venv venv
source .venv/bin/activate

Finally, install the required dependencies in the virtual environment:

pip install -r requirements.txt

Usage

Warning: This program may overwrite files or directories located in the current working directory. Therefore, it is strongly recommended to run this program in its own directory.

If no data files exist in the data/unprocessed subdirectory, the program will attempt to fetch the data from the online sources. Otherwise, the program will use the existing dataset in data/unprocessed. Therefore, in order to fetch updated data, the data directory should be removed.

To generate the processed CSV and JSON data files, as well as the unprocessed files if they do not already exist, change to the project directory and run the program:

python3 utility.py

Output

The program outputs a data directory containing the retrieved data as well as a checksums.txt file containing SHA256 checksums for each file in data.

The structure of the data directory after program completion is the following:

data
    processed
        csv
        json
    unprocessed
        census
        federal_reserve
        fred

data/processed contains the retrieved data in CSV and JSON file formats in the csv and json subdirectories, respectively.

data/unprocessed contains the subdirectories census, federal_reserve, and fred, indicating the source from which the data was retrieved. Within each of those subdirectories, the retrieved data is saved in its original file format.

Any ZIP files are saved in the proper subdirectory within data/unprocessed to indicate their source and are unzipped to create a new directory alongside the ZIP file. The data contained within the new directory consists of the unzipped files in their original format.

Updating

To obtain updates, change to the project directory and pull updates from GitHub:

git pull

Testing

To test, install pytest and then, from within the project directory, run:

pytest

To test with code coverage, additionally install the pytest extension pytest-cov and then, from within the project directory, run:

pytest --cov=.

Function Documentation

utility.adjust_for_inflation

utility.adjust_for_inflation(nominal, year=None, cpi=None)

Adjust the values in the nominal DataFrame for inflation.

This function expects the Pandas DataFrames nominal and cpi (if provided) to be sorted by year ascending. year can optionally be specified to indicate the year on which the index should be rebased. The default base year is the latest year in the intersection of nominal and cpi. If cpi is not provided, then the function will attempt to create a DataFrame by looking up the CPI values from data/processed/csv/cpi.csv.

Example:

nominal = pandas.DataFrame(
    {
        "year": [
            2010,
            2015,
            2018,
            2025,
        ],
        "value": [
            100,
            105,
            115,
            125,
        ]
    }
)
cpi = pandas.DataFrame(
    {
        "year": [
            2008,
            2010,
            2012,
            2015,
            2025,
        ],
        "cpi": [
            95,
            100,
            103,
            108,
            120,
        ]
    }
)
expected_output = pandas.DataFrame(
    {
        "year": [
            2010,
            2015,
            2025,
        ],
        "value": [
            120.0,
            116.66666666666666,
            125.0,
        ]
    }
)
assert expected_output.equals(utility.adjust_for_inflation(nominal, cpi)) # the assertion will be true

Arguments:

nominal: a Pandas DataFrame with one column year containing years and one column containing the nominal dollar values for each year

year: the base year on which the index will be rebased (optional)

cpi: a Pandas DataFrame with one column year containing years and one column containing the CPI for each year (optional)

Return:

a Pandas DataFrame with one column year containing years and one column containing the dollar values for each year, adjusted for inflation

utility.calc_case_shiller_home_price_index_inflation_adjusted

utility.calc_case_shiller_home_price_index_inflation_adjusted(case_shiller_home_price_index)

Calculate the inflation adjusted Case-Shiller home price index.

Example:

df = pandas.DataFrame(
    [
        {
            "year": 1987,
            "case_shiller_home_price_index": 66.2500833333
        },
        {
            "year": 1988,
            "case_shiller_home_price_index": 71.1335833333
        },
        {
            "year": 1989,
            "case_shiller_home_price_index": 75.5005833333
        },
        {
            "year": 1990,
            "case_shiller_home_price_index": 76.93575
        },
    ]
)
expected_output_df = pandas.DataFrame(
    {
        "year": [
            1987,
            1988,
            1989,
            1990
        ],
        "case_shiller_home_price_index_inflation_adjusted": [
            76.18710991512474,
            78.58123392396327,
            79.59212304732137,
            76.93575
        ]
    }
)
expected_output_data = {
    "case_shiller_home_price_index_inflation_adjusted": expected_output_df
}
assert expected_output_data["case_shiller_home_price_index_inflation_adjusted"].equals(utility.calc_case_shiller_home_price_index_inflation_adjusted(df)["case_shiller_home_price_index_inflation_adjusted"]) # the assertion will be true

Arguments:

case_shiller_home_price_index: a Pandas DataFrame containing the nominal Case-Shiller home price index time-series data

Return:

a dictionary containing a Pandas DataFrame of the inflation adjusted Case-Shiller home price index time-series data

utility.calc_cumulative_single_family_units_completed

utility.calc_cumulative_single_family_units_completed(single_family_units_completed)

Calculate cumulative single family housing units completed.

Example:

single_family_units_completed = pandas.DataFrame(
    {
        "year": [
            2023,
            2024,
            2025,
            2026
        ],
        "single_family_units_completed": [
            1000666.6666666666,
            1016000.0,
            1004000.0,
            970000.0
        ]
    }
)
expected_output_df = pandas.DataFrame(
    {
        "year": [
            2023,
            2024,
            2025,
            2026,
        ],
        "cumulative_single_family_units_completed": [
            1000666.6666666666,
            2016666.6666666665,
            3020666.6666666665,
            3990666.6666666665,
        ]
    }
)
expected_output = {
    "cumulative_single_family_units_completed": expected_output_df
}
received_output = utility.calc_cumulative_single_family_units_completed(single_family_units_completed)
assert expected_output["cumulative_single_family_units_completed"].equals(received_output["cumulative_single_family_units_completed"]) # the assertion will be true

Arguments:

single_family_units_completed: a Pandas DataFrame containing the time-series data for single family housing units completed

Return:

a dictionary containing a Pandas DataFrame of time-series data for cumulative single family housing units completed

utility.calc_cumulative_single_family_units_completed_per_person

utility.calc_cumulative_single_family_units_completed_per_person(single_family_units_completed, population)

Calculate cumulative single family housing units completed per person.

Example:

single_family_units_completed = pandas.DataFrame(
    {
        "year": [
            2023,
            2024,
            2025,
            2026
        ],
        "single_family_units_completed": [
            1000666.6666666666,
            1016000.0,
            1004000.0,
            970000.0
        ]
    }
)
population = pandas.DataFrame(
    {
        "year": [
            2023,
            2024
        ],
        "population": [
            336806231,
            340110988
        ]
    }
)
expected_output_df = pandas.DataFrame(
    {
        "year": [
            2023,
            2024,
        ],
        "cumulative_single_family_units_completed_per_person": [
            0.002971045588128287,
            0.005929436971517858,
        ]
    }
)
expected_output = {
    "cumulative_single_family_units_completed_per_person": expected_output_df
}
received_output = utility.calc_cumulative_single_family_units_completed_per_person(single_family_units_completed, population)
assert expected_output["cumulative_single_family_units_completed_per_person"].equals(received_output["cumulative_single_family_units_completed_per_person"]) # the assertion will be true

Arguments:

single_family_units_completed: a Pandas DataFrame containing the time-series data for single family housing units completed

population: a Pandas DataFrame containing population time-series data

Return:

a dictionary containing a Pandas DataFrame of time-series data for single family housing units completed per person

utility.calc_real_median_income_male_to_real_home_price_index_by_age

utility.calc_real_median_income_male_to_real_home_price_index_by_age(real_median_income_male_by_age, case_shiller_home_price_index_inflation_adjusted)

Calculate the real median income to real home price index by age for males.

Example:

real_median_income_male_by_age = pandas.DataFrame(
    {
        "year": [
            2021,
            2022,
            2023,
            2024
        ],
        "age_25_to_34": [
            55197.7709498564,
            54693.901684087,
            54420.48516096,
            55360.0
        ],
        "age_35_to_44": [
            69587.6260863227,
            65885.6761564874,
            66311.4538255285,
            70630.0
        ],
        "age_45_to_54": [
            70861.0645939746,
            69927.1502715208,
            72385.6282948751,
            74030.0
        ],
        "age_55_to_64": [
            60766.1702424069,
            60825.7934448274,
            63078.7575825202,
            64770.0
        ]
    }
)
case_shiller_home_price_index_inflation_adjusted = pandas.DataFrame(
    {
        "year": [
            2022,
            2023,
            2024,
            2025
        ],
        "case_shiller_home_price_index_inflation_adjusted": [
            328.2183830851,
            323.105723778,
            329.8674868998,
            328.6574166667
        ]
    }
)
expected_output_df = pandas.DataFrame(
    {
        "year": [
            2022,
            2023,
            2024
        ],
        "age_25_to_34": [
            166.63875182733454,
            168.4293441930831,
            167.82496668674736
        ],
        "age_35_to_44": [
            200.737312569737,
            205.23144266887044,
            214.1162824617949
        ],
        "age_45_to_54": [
            213.05068172671542,
            224.0307830158092,
            224.42345165859658
        ],
        "age_55_to_64": [
            185.32110503102618,
            195.22637000965184,
            196.351573199072
        ]
    }
)
expected_output = {
    "real_median_income_male_to_real_home_price_index_by_age": expected_output_df
}
received_output = utility.calc_real_median_income_male_to_real_home_price_index_by_age(real_median_income_male_by_age, case_shiller_home_price_index_inflation_adjusted)
assert expected_output["real_median_income_male_to_real_home_price_index_by_age"].equals(received_output["real_median_income_male_to_real_home_price_index_by_age"]) # the assertion will be true

Arguments:

real_median_income_male_by_age: a Pandas DataFrame containing the male real median income by age time-series data

case_shiller_home_price_index_inflation_adjusted: a Pandas DataFrame containing the Case-Shiller inflation adjusted home price index time-series data

Return:

a dictionary containing a Pandas DataFrame of the male real median income to real home price index by age time-series data

utility.calc_real_median_income_male_to_unit_output_by_age

utility.calc_real_median_income_male_to_unit_output_by_age(real_median_income_male_by_age, mean_annual_hours_worked, total_factor_productivity)

Calculate the real median income per unit output for males by age.

Example:

real_median_income_male_by_age = pandas.DataFrame(
    {
        "year": [
            2021,
            2022,
            2023,
            2024
        ],
        "age_25_to_34": [
            55197.7709498564,
            54693.901684087,
            54420.48516096,
            55360.0
        ],
        "age_35_to_44": [
            69587.6260863227,
            65885.6761564874,
            66311.4538255285,
            70630.0
        ],
        "age_45_to_54": [
            70861.0645939746,
            69927.1502715208,
            72385.6282948751,
            74030.0
        ],
        "age_55_to_64": [
            60766.1702424069,
            60825.7934448274,
            63078.7575825202,
            64770.0
        ]
    }
)

mean_annual_hours_worked = pandas.DataFrame(
    {
        "year": [
            2020,
            2021,
            2022,
            2023
        ],
        "mean_annual_hours_worked": [
            1777.88,
            1788.7,
            1789.59,
            1788.88
        ]
    }
)

total_factor_productivity = pandas.DataFrame(
    {
        "year": [
            2020,
            2021,
            2022,
            2023
        ],
        "total_factor_productivity": [
            0.9730775356,
            1.0,
            0.9840381742000001,
            0.9929984808000001
        ]
    }
)

expected_output_df = pandas.DataFrame(
    {
        "year": [
            2021,
            2022,
            2023
        ],
        "age_25_to_34": [
            30.859155224384413,
            31.05799493482761,
            30.63603920001687
        ],
        "age_35_to_44": [
            38.90402308174803,
            37.41325693246776,
            37.3300659264678
        ],
        "age_45_to_54": [
            39.61595829036429,
            39.70821265383777,
            40.74952546638642
        ],
        "age_55_to_64": [
            33.97225372751546,
            34.53999671897525,
            35.510217967934786
        ]
    }
)

expected_output = {
    "real_median_income_male_to_unit_output_by_age": expected_output_df
}
assert expected_output["real_median_income_male_to_unit_output_by_age"].equals(utility.calc_real_median_income_male_to_unit_output_by_age(real_median_income_male_by_age, mean_annual_hours_worked, total_factor_productivity)["real_median_income_male_to_unit_output_by_age"]) # the assertion will be true

Arguments:

real_median_income_male_by_age: a Pandas DataFrame containing male real median income by age time-series data

mean_annual_hours_worked: a Pandas DataFrame containing mean annual hours worked time-series data encompassing both males and females

total_factor_productivity: a Pandas DataFrame containing total factor productivity time-series data

Return:

a dictionary containing a Pandas DataFrame of male real median income per unit output by age time-series data

utility.calc_single_family_units_completed_per_person

utility.calc_single_family_units_completed_per_person(single_family_units_completed, population)

Calculate single family housing units completed per person.

Example:

single_family_units_completed = pandas.DataFrame(
    {
        "year": [
            2023,
            2024,
            2025,
            2026
        ],
        "single_family_units_completed": [
            1000666.6666666666,
            1016000.0,
            1004000.0,
            970000.0
        ]
    }
)
population = pandas.DataFrame(
    {
        "year": [
            2023,
            2024
        ],
        "population": [
            336806231,
            340110988
        ]
    }
)
expected_output_df = pandas.DataFrame(
    {
        "year": [
            2023,
            2024,
        ],
        "single_family_units_completed_per_person": [
            0.002971045588128287,
            0.002987260146972964,
        ]
    }
)
expected_output = {
    "single_family_units_completed_per_person": expected_output_df
}
received_output = utility.calc_single_family_units_completed_per_person(single_family_units_completed, population)
assert expected_output["single_family_units_completed_per_person"].equals(received_output["single_family_units_completed_per_person"]) # the assertion will be true

Arguments:

single_family_units_completed: a Pandas DataFrame containing the time-series data for single family housing units completed

population: a Pandas DataFrame containing population time-series data

Return:

a dictionary containing a Pandas DataFrame of time-series data for single family housing units completed per person

utility.calculate_checksums

utility.calculate_checksums()

Calculate the SHA256 checksums of the files in the data directory.

Arguments:

none

Return:

a list of tuples containing a file path and the SHA256 checksum of the file, sorted first by level and second alphabetically by file name

utility.calculate_checksums_aux

utility.calculate_checksums_aux(checksums, root, ignore=None)

Auxiliary function for utility.calculate_checksums that calculates the SHA256 checksum of each file in the specified root directory and each subdirectory recursively.

This function performs a breadth-first search to recursively discover each file rooted in the directory root. The order of discovery is the order in which files are added to the list of checksums checksums, with files on the same level sorted alphabetically by file name. Files with names beginning with two underscores (__) are ignored, as are any files with names specified in ignore.

Arguments:

checksums: a list of tuples containing a file path and the SHA256 checksum of the file, sorted first by level and second alphabetically by file name. When calling the function, an empty list may be provided.

root: the path to the root directory of the files

ignore: a list of file names (not full paths) to ignore

Return:

nothing. Note that on return the provided list checksums will be populated with tuples containing a file path and the SHA256 checksum of the file, sorted first by level and second alphabetically by file name.

utility.fetch_census_data

utility.fetch_census_data()

Fetch Census data from either disk or, if it does not exist on disk already, the URL of the file on the U.S. Census website.

Arguments:

none

Return:

a Pandas DataFrame containing the retrieved data

utility.fetch_federal_reserve_data

utility.fetch_federal_reserve_data()

Retrieve Federal Reserve data from disk or, if it does not exist on disk, from the Federal Reserve URL.

Arguments:

none

Return:

a Pandas DataFrame containing the retrieved data

utility.fetch_fred_data

utility.fetch_fred_data(statistic, series_id, start_time=None)

Retrieve FRED data for the specified statistic from disk or, if it does not exist on disk, from the FRED API endpoint.

Arguments:

statistic: the statistic for which to retrieve data

series_id: the FRED series ID for the statistic

start_time: the time at which the response from the previous FRED API call was received

Return:

a dictionary containing the time-series FRED data

utility.get_census_data

utility.get_census_data()

Get male median income by age data from the U.S. Census Bureau website.

The data is retrieved from the U.S. Census Bureau website and saved to data/unprocessed/census/median_income_male_by_age.xlsx unless it already exists in that location, in which case it is retrieved from disk without sending a request to the website. The returned data is a dictionary, with the keys being the statistics nominal_median_income_male_by_age and real_median_income_male_by_age and the values being Pandas DataFrames associated with those statistics. The columns of each DataFrame include the year and the median income of the different age groups. The data is also written to a JSON file and a CSV file for each statistic in data/processed/json and data/processed/csv, respectively.

Arguments:

none

Return:

the retrieved male median income by age data

utility.get_census_median_income_multiple_groups

utility.get_census_median_income_multiple_groups(df, groups)

Get nominal median income data for each specified age group.

Example:

groups = [
    "55 to 64 Years",
    "65 to 74 Years",
]
rows = [
    ['1949 (6)', 8342, 2751, 29620, 3624, 1165, 12540],
    [1948, 8185, 2828, 30070, 3376, 1310, 13930],
    ['1947 (5)', 8130, 2681, 30960, 3268, 1293, 14930],
    ['55 to 64 Years', numpy.nan, numpy.nan, numpy.nan, numpy.nan, numpy.nan, numpy.nan],
    ['Age and year', 'Male', numpy.nan, numpy.nan, 'Female', numpy.nan, numpy.nan],
    [numpy.nan, 'Number with income (thousands)', 'Median income', numpy.nan, 'Number with income (thousands)', 'Median income', numpy.nan],
    [numpy.nan, numpy.nan, 'Current dollars', '2024 dollars', numpy.nan, 'Current dollars', '2024 dollars'],
    [1989, 9818, 24430, 55980, 10320, 9163, 21000],
    [1988, 9955, 22650, 54180, 10400, 8377, 20040],
    ['1987 (21)', 10020, 21880, 54290, 10500, 7541, 18710],
    ['..65 to 74 Years', numpy.nan, numpy.nan, numpy.nan, numpy.nan, numpy.nan, numpy.nan],
    ['Age and year', 'Male', numpy.nan, numpy.nan, 'Female', numpy.nan, numpy.nan],
    [numpy.nan, 'Number with income (thousands)', 'Median income', numpy.nan, 'Number with income (thousands)', 'Median income', numpy.nan],
    [numpy.nan, numpy.nan, 'Current dollars', '2024 dollars', numpy.nan, 'Current dollars', '2024 dollars'],
    [1989, 7966, 14470, 33150, 9850, 7948, 18210],
    [1988, 7837, 13940, 33350, 9744, 7256, 17360],
    ['1987 (21)', 7690, 13410, 33270, 9629, 6986, 17330],
    ['..75 Years and Over', numpy.nan, numpy.nan, numpy.nan, numpy.nan, numpy.nan, numpy.nan],
    ['Age and year', 'Male', numpy.nan, numpy.nan, 'Female', numpy.nan, numpy.nan],
    [numpy.nan, 'Number with income (thousands)', 'Median income', numpy.nan, 'Number with income (thousands)', 'Median income', numpy.nan],
    [numpy.nan, numpy.nan, 'Current dollars', '2024 dollars', numpy.nan, 'Current dollars', '2024 dollars'],
]
columns = [
    "Table",
    "Unnamed: 1",
    "Unnamed: 2",
    "Unnamed: 3",
    "Unnamed: 4",
    "Unnamed: 5",
    "Unnamed: 6",
]
df = pandas.DataFrame.from_records(rows,columns=columns)
expected_output = pandas.DataFrame(
    {
        "year": [
            1987,
            1988,
            1989,
        ],
        "age_55_to_64": [
            21880,
            22650,
            24430,
        ],
        "age_65_to_74": [
            13410,
            13940,
            14470,
        ],
    }
)
received_output = utility.get_census_median_income_multiple_groups(df, groups)
assert expected_output.equals(received_output) # the assertion will be true

Arguments:

df: a Pandas DataFrame containing the unprocessed data read from the source .xlsx file

groups: a list of Census median income groups for which to get data

Return:

a dictionary containing Pandas DataFrames of median income data for each age group

utility.get_census_median_income_single_group

utility.get_census_median_income_single_group(rows, group, previous_end_index=None)

Get nominal median income for a particular age group.

Example:

expected_df = pandas.DataFrame(
    {
        "year": [
            2022,
            2023,
            2024,
        ],
        "age_35_to_44": [
            61460,
            64410,
            70630,
        ]
    }
)
expected_end_index = 9
received_df, received_end_index = utility.get_census_median_income_single_group(rows, group, 2)
assert expected_df.equals(received_df) # the assertion will be true
assert expected_end_index == received_end_index # the assertion will be true

Arguments:

rows: a list of rows from the male median income file

group: the age group for which to read data from the rows

previous_end_index: the index of the last row of data for the preceding age group (optional)

Return:

a Pandas DataFrame containing the median income data for the specified age group and the index of the last row of data in the .xlsx file for that age group

utility.get_data

utility.get_data()

Get data from all sources, create data directory structure, create additional datasets using data from multiple sources, and create a data/units/units.json file containing a mapping of file names to units.

Arguments:

none

Return:

a dictionary, the keys of which are the statistics from all of the sources and the values of which are the corresponding pandas DataFrames of each statistic

utility.get_federal_reserve_data

utility.get_federal_reserve_data()

Get data on real net worth per household by age from the U.S. Federal Reserve website.

The data is saved in both zipped and unzipped formats to data/unprocessed/federal_reserve/ unless it already exists in that location, in which case it is retrieved from disk without sending a request to the website. The returned data is a dictionary, with one key--the statistic real_net_worth_per_household_by_age--and one value--a Pandas DataFrame containing the retrieved data. The columns of the DataFrame include the year and the corresponding values for each age group. The data is also written to a JSON file and a CSV file in data/processed/json and data/processed/csv.

Arguments:

none

Return:

a Pandas DataFrame containing the data on real net worth per household for each age group

utility.get_fred_data

utility.get_fred_data()

Get data from the U.S. Federal Reserve's FRED API.

The data is saved to data/unprocessed/fred/ unless it already exists in that location, in which case it is retrieved from disk without sending a request to the FRED API. The returned data is a dictionary, with the keys being the statistics in the keys of the global dictionary FRED_SERIES_IDS and the values being Pandas DataFrames associated with those statistics. The columns of each DataFrame include the year and the corresponding values for the given statistic. Note that the DataFrames associated with the homeownership rate by age statistic contains more than two columns, with each additional column containing values for a specific age group. The data is also written to a JSON file and a CSV file for each statistic in data/processed/json and data/processed/csv, respectively.

Arguments:

none

Return:

a Pandas DataFrame containing the data for each statistic

utility.process_federal_reserve_data

utility.process_federal_reserve_data(df)

Extract age group data from overall Federal Reserve net worth and household count data.

Example:

input_df = pandas.DataFrame(
    {
        "Date": [
            "1989:Q3",
            "1989:Q3",
            "1989:Q4",
            "1989:Q4",
            "1990:Q1",
            "1990:Q1",
        ],
        "Category": [
            "age40to54",
            "ageunder40",
            "age40to54",
            "ageunder40",
            "age40to54",
            "ageunder40",
        ],
        "Assets": [
            7781784,
            3830838,
            7886534,
            3888881,
            7949269,
            3891248,
        ],
        "Net worth": [
            6539549,
            2452973,
            6617660,
            2484176,
            6656226,
            2467675,
        ],
        "Household count": [
            24579466,
            35622043,
            24596086,
            35691463,
            24635411,
            35754314,
        ],
    }
)
expected_output = pandas.DataFrame(
    {
        "year": [
            1989,
            1990,
        ],
        "age_less_than_40": [
            69231.61231197916,
            69017.54568693445,
        ],
        "age_40_to_54": [
            267555.8985082669,
            270189.3627835152,
        ],
        "age_55_to_69": [
            numpy.nan,
            numpy.nan,
        ],
        "age_greater_than_or_equal_to_70": [
            numpy.nan,
            numpy.nan,
        ],
    }
)
received_output = utility.process_federal_reserve_data(input_df)
assert expected_output.equals(received_output) # the assertion will be true

Arguments:

df: a Pandas DataFrame containing net worth and household count data retrieved from the Federal Reserve website

Return:

a Pandas DataFrame of nominal net worth per household data for each age group

utility.process_fred_data

utility.process_fred_data(statistic, d, data)

Convert FRED data for a statistic to a Pandas DataFrame and add to data dictionary.

Example:

statistic = "cpi"
data = {}
input_statistic_data = [
    {
        "realtime_start": "2026-03-11",
        "realtime_end": "2026-03-11",
        "date": "2026-01-01",
        "value": "326.588"
    },
    {
        "realtime_start": "2026-03-11",
        "realtime_end": "2026-03-11",
        "date": "2026-02-01",
        "value": "327.460"
    }
]
expected_output = pandas.DataFrame(
    {
        "year": [
            2026
        ],
        "cpi": [
            327.024
        ],
    }
)
utility.process_fred_data(statistic, input_statistic_data, data)
assert expected_output.equals(data[statistic]) # the assertion will be true

Arguments:

statistic: the statistic

d: a dictionary containing FRED data for a statistic

data: a dictionary containing processed data for each FRED statistic

Return:

nothing

utility.read_processed_data

utility.read_processed_data(statistic)

Read processed data for the specified statistic from the corresponding JSON file.

Arguments:

statistic: the statistic for which to read data

Return:

a Pandas DataFrame containing the data for the specified statistic

utility.write_processed_data

utility.write_processed_data(data)

Write data for each statistic to a JSON file and a CSV file in the processed data directory.

Arguments:

data: a dictionary containing key-value mappings of statistics to the corresponding Pandas DataFrame

Return:

nothing