beaapi¶
beaapi: A Python library library to make it easier to retrieve and work with BEA data. For the parallel R-package, see bea.R.
To install the package, download the .whl
file for the latest release, and install using pip
.
For help, see our documentation.
For development and building information, see CONTRIBUTING.md.
To Get Started¶
Once you have installed the package, you can load it in your Python script as follows:
[1]:
import beaapi
To use the package, you must first register for an API key from BEA by providing your name and email address. The key will be emailed to you.
Once you have received your BEA API key, you can save it to a variable to make it easier to use later:
[2]:
beakey = 'YOUR 36-DIGIT API KEY'
For scripts, an even better method is to create an unversioned text file called .env
with the contents beakey=Your36DigitAPiKey
and then use the package python-dotenv
to load it automatically.
[3]:
import os
from dotenv import load_dotenv
load_dotenv()
beakey = os.environ.get("beakey")
Get metadata¶
The BEA data is distributed across various datasets that each contain many tables. To get a list of the datasets available on the API, use get_data_set_list()
:
[4]:
list_of_sets = beaapi.get_data_set_list(beakey)
display(list_of_sets) # Note the last dataset is only for speeding up metadata queries
DatasetName | DatasetDescription | |
---|---|---|
0 | NIPA | Standard NIPA tables |
1 | NIUnderlyingDetail | Standard NI underlying detail tables |
2 | MNE | Multinational Enterprises |
3 | FixedAssets | Standard Fixed Assets tables |
4 | ITA | International Transactions Accounts |
5 | IIP | International Investment Position |
6 | InputOutput | Input-Output Data |
7 | IntlServTrade | International Services Trade |
8 | GDPbyIndustry | GDP by Industry |
9 | Regional | Regional data sets |
10 | UnderlyingGDPbyIndustry | Underlying GDP by Industry |
11 | APIDatasetMetaData | Metadata about other API datasets |
Queries to the different datasets take different parameters. To get a list of the paramaters for a given dataset, use get_parameter_list()
; for example, to get a list of the parameters for the NIPA dataset, use:
[5]:
list_of_params = beaapi.get_parameter_list(beakey, 'NIPA')
display(list_of_params)
ParameterName | ParameterDataType | ParameterDescription | ParameterIsRequiredFlag | ParameterDefaultValue | MultipleAcceptedFlag | AllValue | |
---|---|---|---|---|---|---|---|
0 | Frequency | string | A - Annual, Q-Quarterly, M-Monthly | 1 | 1 | ||
1 | ShowMillions | string | A flag indicating that million-dollar data sho... | 0 | N | 0 | |
2 | TableID | integer | The standard NIPA table identifier | 0 | <NA> | 0 | |
3 | TableName | string | The new NIPA table identifier | 0 | <NA> | 0 | |
4 | Year | integer | List of year(s) of data to retrieve (X for All) | 1 | 1 | X |
To get a list of the values for a given parameter, use get_parameter_values()
; for example, to get a list of the parameter values for the Frequency parameter of the NIPA dataset, use:
[6]:
list_of_param_vals = beaapi.get_parameter_values(beakey, 'NIPA', 'Frequency')
display(list_of_param_vals)
FrequencyID | Description | |
---|---|---|
0 | A | Annual |
1 | Q | Quarterly |
2 | M | Monthly |
The API documentation is a good place to also understand allowed values.
A few datasets (ITA, IIP, InputOutput, IntlServTrade, GDPbyIndustry, Regional, and UnderlyingGDPbyIndustry) allow you to filter the values based on what will be passed in for other parameters.
[7]:
tbl = beaapi.get_parameter_values_filtered(beakey, 'ITA', targetparameter='Indicator', AreaOrCountry="China",Frequency="A", Year="2011")
display(tbl.head())
Key | Desc | |
---|---|---|
0 | BalCapAcct | Balance on capital account |
1 | BalCurrAcct | Balance on current account |
2 | BalGds | Balance on goods |
3 | BalGdsServ | Balance on goods and services |
4 | BalPrimInc | Balance on primary income |
A few of the datasets publish metadata tables that can be queried for particular strings using search_metadata()
. This method allows you to search for BEA data by keyword. For example, to find all datasets in which the term “personal consumption” appears, use the following:
[8]:
search_data = beaapi.search_metadata('Gross domestic', beakey)
search_data.head(2)
[8]:
SeriesCode | RowNumber | LineDescription | LineNumber | ParentLineNumber | Tier | Path | TableId | Datasetname | TableName | ReleaseDate | NextReleaseDate | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | A191RL | 10 | Gross domestic product | 1 | 0 | 1 | T10101 | NIPA | Table 1.1.1. Percent Change From Preceding Per... | Feb 28 2019 8:30AM | Mar 28 2019 8:30AM | |
26 | A191RP | 280 | Gross domestic product, current dollars | 27 | 0 | 27 | T10101 | NIPA | Table 1.1.1. Percent Change From Preceding Per... | Feb 28 2019 8:30AM | Mar 28 2019 8:30AM |
Please note that that search_metadata currently searches only national data.
The contents of this function are automatically updated using a new metadata component of BEA’s API; as such, we recommend that you use it with your API key, and the first use of this function requires that you use your key or it will be unable to extract the metadata.
If you do not wish to automatically update the metadata (e.g., you have conducted a study using the search function), simply searching for the term without also passing your key to the function will do a search only using your locally stored version.
Get data¶
Once you have identified the TableId number and other information, you can use get_data()
to access the data. The following code, for example, returns the NIPA table with 2015 data for Table T20305.
[9]:
bea_tbl = beaapi.get_data(beakey, datasetname='NIPA', TableName='T20305', Frequency='Q', Year='2015')
display(bea_tbl.head(2))
TableName | SeriesCode | LineNumber | LineDescription | TimePeriod | METRIC_NAME | CL_UNIT | UNIT_MULT | DataValue | NoteRef | |
---|---|---|---|---|---|---|---|---|---|---|
0 | T20305 | DPCERC | 1 | Personal consumption expenditures (PCE) | 2015Q1 | Current Dollars | Level | 6 | 12083904 | T20305 |
1 | T20305 | DPCERC | 1 | Personal consumption expenditures (PCE) | 2015Q2 | Current Dollars | Level | 6 | 12224707 | T20305 |
We store in the meta-data the index columns so that you can create a unique index on the data-frame.
[10]:
display(bea_tbl.set_index(bea_tbl.attrs['index_cols']).head(2))
TableName | SeriesCode | LineDescription | METRIC_NAME | CL_UNIT | UNIT_MULT | DataValue | NoteRef | ||
---|---|---|---|---|---|---|---|---|---|
LineNumber | TimePeriod | ||||||||
1 | 2015Q1 | T20305 | DPCERC | Personal consumption expenditures (PCE) | Current Dollars | Level | 6 | 12083904 | T20305 |
2015Q2 | T20305 | DPCERC | Personal consumption expenditures (PCE) | Current Dollars | Level | 6 | 12224707 | T20305 |
Extra meta-data from the API is returned in a dictionary in the attributes called detail
and can vary based on the dataset.
[11]:
print('Extra detail keys:' + str(bea_tbl.attrs['detail'].keys()))
print("Let's look at some interesting ones.")
print('Statistic: ' + bea_tbl.attrs['detail']['Statistic'])
print("Notes corresponding to NoteRef:")
display(bea_tbl.attrs['detail']['Notes'].head())
Extra detail keys:dict_keys(['Statistic', 'UTCProductionTime', 'Dimensions', 'Notes'])
Let's look at some interesting ones.
Statistic: NIPA Table
Notes corresponding to NoteRef:
NoteRef | NoteText | |
---|---|---|
0 | T20305 | Table 2.3.5. Personal Consumption Expenditures... |
1 | T20305.1 | 1. Net expenses of NPISHs, defined as their gr... |
2 | T20305.2 | 2. Gross output is net of unrelated sales, sec... |
3 | T20305.3 | 3. Excludes unrelated sales, secondary sales, ... |
4 | T20305.4 | 4. Food consists of food and beverages purchas... |
To retrieve a limited selection of multiple years, list all the years you want to retrieve. For example, to retrieve data for 2011-2015, use Year="2011,2012,2013,2014,2015"
The API documentation includes information about the specific parameters required by get_data()
.
If you would like to format the data in a wide format so that different variables (in this case LineNumbers) are different rows, you can use to_wide_vars_to_cols()
.
[12]:
display(beaapi.to_wide_vars_in_cols(bea_tbl).head(3))
LineNumber | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TimePeriod | |||||||||||||||||||||
2015Q1 | 12083904 | 3867908 | 1283823 | 465426 | 285887 | 332965 | 199546 | 2584085 | 917371 | 366171 | ... | 814298 | 960760 | 992461 | 362114 | 1367850 | 1005736 | 10619844 | 546689 | 10570891 | 9107467 |
2015Q2 | 12224707 | 3927357 | 1309627 | 481820 | 292631 | 333600 | 201577 | 2617730 | 916930 | 368315 | ... | 828712 | 977198 | 1003757 | 363147 | 1374002 | 1010855 | 10759482 | 548295 | 10693235 | 9228648 |
2015Q3 | 12347752 | 3960436 | 1318493 | 481620 | 297306 | 337083 | 202483 | 2641944 | 924494 | 370385 | ... | 836130 | 978430 | 1012278 | 373825 | 1385653 | 1011828 | 10877903 | 545354 | 10802962 | 9333737 |
3 rows × 28 columns
Advanced topics¶
Throttling: The BEA api limits requests to a maximum of 100/minute and 100MB/minute (as well as 30 errors/minute). If the user exceeds this, they will be denied access for 1 hour. This package will automatically self-throttle, so in general, the user does not have to worry about that.
See the docs for additional information on: - How to construct API queries to pull data from existing iTables.
Quick Links¶
Disclaimer¶
The United States Department of Commerce (DOC) GitHub project code is provided on an ‘as is’ basis and the user assumes responsibility for its use. DOC has relinquished control of the information and no longer has responsibility to protect the integrity, confidentiality, or availability of the information. Any claims against the Department of Commerce stemming from the use of its GitHub project will be governed by all applicable Federal law. Any reference to specific commercial products, processes, or services by service mark, trademark, manufacturer, or otherwise, does not constitute or imply their endorsement, recommendation or favoring by the Department of Commerce. The Department of Commerce seal and logo, or the seal and logo of a DOC bureau, shall not be used in any manner to imply endorsement of any commercial product or activity by DOC or the United States Government.
Use of this library may result in data being stored on users’ local machines. Specifically, local copies of BEA API metadata may be stored and automatically updated in the beaapi_data
directory (unless otherwise changed) in order to improve performance of search_metadata()
.