Skip to main content

image Downloads image

Datapungibea

Datapungibea is a Python package that provides a simplified way to extract data from the U.S. Bureau of Economic Analysis (BEA). Its main purpose is to connect to BEA's Application Program Interface (API).

Features:

  • Quick access to 13 BEA datasets with automatic data cleaning
  • Code snippet generation for reproducible queries (standalone requests code)
  • Enriched NIPA data with table indentations (graph structure)
  • NIPA Summary tables (6 economic accounts)
  • Three flexible methods for API key management
  • Comprehensive test suite with HTML reporting
  • Python 3.14+ compatible

Installation

pip install datapungibea

Quick Start

1. Get a BEA API Key

Register for a free API key at: https://apps.bea.gov/API/signup/index.cfm

2. Set Up Your API Key

Option 1: Environment Variable (Recommended)

export API_KEY_BEA=your_api_key_here

Option 2: Pass Directly

import datapungibea as dpb
data = dpb.data("your_api_key_here")

Option 3: Key File

import datapungibea as dpb
data = dpb.data(keyfile="/path/to/keys.yaml")

3. Query Data

import datapungibea as dpb

data = dpb.data()

# Get the list of available data sets:
data.datasetlist()  # eg, 'NIPA'

# For a given data set, get the parameters specifying a data table
data.getParameterList('NIPA') # List NIPA parameters, eg TableID

# For a given parameter, get all values of that parameter
data.getParameterValues('NIPA','TableID')

# Get the data table 
table = data.NIPA('T10101')

Available Datasets

Datapungibea provides access to 12 BEA datasets:

Dataset Description Example
NIPA National Income and Product Accounts data.NIPA('T10101')
NIUnderlyingDetail NI underlying detail tables data.NIUnderlyingDetail('U20304')
FixedAssets Fixed Assets tables data.fixedAssets('FAAt101', '2010')
ITA International Transactions Accounts data.ITA('BalCurrAcct', 'Brazil', 'A', '2010')
IIP International Investment Position data.IIP('DebtSecAssets', 'All', 'All', '2010')
GDPbyIndustry GDP by Industry data.GDPbyIndustry('211', '1', 'A', '2018')
UnderlyingGDPbyIndustry Underlying GDP by Industry data.UnderlyingGDPbyIndustry('ALL', 'ALL', 'A', '2014')
InputOutput Input-Output tables data.InputOutput('56', '2010')
IntlServTrade International Services Trade data.IntlServTrade('ALL', 'ALL', 'ALL', 'AllCountries', 'All')
IntlServSTA International Services Supplied Through Affiliates data.IntlServSTA()
Regional Regional economic data data.Regional('00000', '1', 'CAINC1', '2010')
MNE Multinational Enterprises data.MNE(...)

NIPA Features

Table Indentations

NIPA queries automatically include table indentations (graph structure) showing hierarchical relationships between line items:

# With indentations (default)
data.NIPA('T10101')

# Without indentations
data.NIPA('T10101', includeIndentations=False)
API data Enriched data

NIPA Summary Tables

Get an overview of NIPA data across 6 economic accounts:

# Get summary for 2020, annual frequency
data.NIPASummary('2020', 'A')

# Get summary for 2020 Q1, quarterly frequency
data.NIPASummary('2020', 'Q')

The summary covers:

  1. Domestic Income and Product Account
  2. Private Enterprise Income Account
  3. Personal Income and Outlays Account
  4. Government Receipts and Expenditure Account
  5. Foreign Transactions Current Account
  6. Domestic Capital Account

Verbose Mode and Code Generation

Use verbose=True to get the full request details and a reproducible code snippet:

result = data.NIPA('T10101', verbose=True)

print(result.keys())
# ['dataFrame', 'request', 'code']

# Access the cleaned DataFrame
df = result['dataFrame']

# Access the raw request
response = result['request']
print(response.status_code)  # 200

# Get reproducible code snippet
print(result['code'])

The generated code snippet is standalone and can be used in scripts without datapungibea:

import requests
import os
import pandas as pd

url = "https://apps.bea.gov/api/data/"
key = os.getenv("API_KEY_BEA")

query = {
    "url": url,
    "params": {
        "UserID": key,
        "ResultFormat": "JSON",
        "method": "GETDATA",
        "datasetname": "NIPA",
        "TableName": "T10101",
        "Frequency": "Q",
        "Year": "X"
    }
}

retrivedData = requests.get(**query)
df_output = pd.DataFrame(retrivedData.json()['BEAAPI']['Results']['Data'])

API Key Management

# Linux/macOS
export API_KEY_BEA=your_api_key_here

# Windows (Command Prompt)
set API_KEY_BEA=your_api_key_here

# Windows (PowerShell)
$env:API_KEY_BEA="your_api_key_here"

Then use:

data = dpb.data()  # Automatically reads API_KEY_BEA

Direct Key Passing

data = dpb.data("your_api_key_here")

Key File

Create a YAML or JSON file with your API key:

keys.yaml:

API_KEY_BEA:
    key: your_api_key_here
    url: https://apps.bea.gov/api/data/

keys.json:

{
    "API_KEY_BEA": {
        "key": "your_api_key_here",
        "url": "https://apps.bea.gov/api/data/"
    }
}

Then use:

data = dpb.data(keyfile="/path/to/keys.yaml")

Testing

Run the test suite to verify your setup:

# Run all tests
python -m pytest datapungibea/tests/test_drivers.py -v

# Generate HTML report
python -m pytest datapungibea/tests/test_drivers.py --html=report.html --self-contained-html

# Run from Python
import datapungibea as dpb
dpb.tests.runTests(outputPath='./')

The tests verify:

  1. API connection and authentication
  2. Data cleaning and transformation
  3. Code snippet generation
  4. Code snippet execution and reproducibility

Migration from v1.x

Breaking Changes

  • NIPAVintage removed: The web scraping endpoint for vintage data is no longer available. Vintage data support is planned for a future release.
  • Environment variable renamed: Changed from BEA to API_KEY_BEA
  • Metadata access: Changed from df.meta to df.attrs['meta']
  • Configuration simplified: Removed setUserSettings(), setKeyName(), setTestFolder() functions

What's New

  • 2 additional datasets: NIUnderlyingDetail, IntlServSTA
  • Improved error handling and logging
  • Cleaner API with 3 key management methods
  • Better code snippet generation

Requirements

  • Python 3.14+
  • pandas
  • requests
  • pyyaml
  • beautifulsoup4
  • lxml
  • openpyxl
  • pyperclip

References

License

MIT License - see LICENSE file for details.

Contributing

Contributions welcome! Please open an issue or pull request on GitHub.

Data Disclaimer

Data used is sourced from the Bureau of Economic Analysis. As stated on the BEA website:

  • Unless stated otherwise, information published on this site is in the public domain and may be used or reproduced without specific permission.
  • As a U.S. government agency, BEA does not endorse or recommend any commercial products or services.
  • Any reference or link to the BEA Web site must not contain information that suggests an endorsement or recommendation by BEA.

For more information, see: https://www.bea.gov/help/guidelines-for-citing-bea

Metadata

Release files for datapungibea 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for datapungibea 2.0.0
File Size Uploaded Provenance Actions
datapungibea-2.0.0.tar.gz 65.4 kB Without provenance Details for datapungibea-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for datapungibea 2.0.0
File Interpreter ABI Platform Actions
datapungibea-2.0.0-py3-none-any.whl Python 3 none any Details for datapungibea-2.0.0-py3-none-any.whl

Total release size: 132.6 kB

Release files / datapungibea-2.0.0.tar.gz

Download URL datapungibea-2.0.0.tar.gz
Size 65.4 kB
Tags Source
SHA-256 checksum
How to use checksums
01359a86e69361b52a3ab1d2c824c96e7d4cf8fcb316a8ce7922659e2f142b0f
BLAKE2b-256 checksum
How to use checksums
6689015fde24b5dffae03894de489a65de8faac88c5e72d5926a99e8fd2218ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Release files / datapungibea-2.0.0-py3-none-any.whl

Download URL datapungibea-2.0.0-py3-none-any.whl
Size 67.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ded8b70c1ab2c5d9a241b282a4d0e792d49552423ba63169aa3e7d21b5ed4e80
BLAKE2b-256 checksum
How to use checksums
1af08930f69215cfe4e2df18f84503e87b06bdec32fd99faa0685d608dcba8d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Security for datapungibea 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page