Metadata-Version: 2.5
Name: datapungi_fed
Version: 0.6.0
Summary: Python interface to the FRED and GeoFRED APIs
Author-email: James Otterson <jjotterson@gmail.com>
Requires-Python: >=3.13
Description-Content-Type: text/markdown
Classifier: License :: OSI Approved :: MIT License
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.13
License-File: LICENSE
Requires-Dist: requests
Requires-Dist: pandas
Requires-Dist: pytest
Requires-Dist: pytest-html
Requires-Dist: pyyaml
Project-URL: Homepage, https://codeberg.org/jotterson/datapungi_fed
Import-Name: datapungi_fed

<!--
TODO: add explanation of the request part of the vintage.
-->

[![image](https://img.shields.io/pypi/v/datapungi_fed.svg)](https://pypi.org/project/datapungi_fed/)
[![Downloads](https://pepy.tech/badge/datapungi-fed)](https://pepy.tech/project/datapungi-fed)
[![image](https://img.shields.io/pypi/pyversions/datapungi_fed.svg)](https://pypi.org/project/datapungi_fed/)

```bash
pip install datapungi_fed
```

# datapungi_fed

`datapungi_fed` is a Python interface to the
[FRED](https://fred.stlouisfed.org/) and
[GeoFRED](https://geofred.stlouisfed.org/) APIs. It fetches Federal Reserve
economic data and returns clean pandas DataFrames, together with the raw
request and a reproducible code snippet upon demand.

**Key features**
- One-line access to any FRED time series: `dpf.ts('GDP')`
- Full access to all FRED and GeoFRED endpoints through quick entry points tracking FRED's structure (eg. "dpf.series.tags('GDP')") 
- Optional verbose output: DataFrame + raw `requests.Response` + reproducible code
- API keys handled through environment variables, or passed strings. 

## Quick start

The FRED API is built around a dataset called "series" 
of hundreds of thousands of time-series. This package provides a quick access to it:

```python
import datapungi_fed as dpf

# Can request one or more variables at once
series = dpf.ts('GDP')
print(series.tail())

series = dpf.ts(['GDP','GNP'])
print(series.tail())

# some metadata is already provided 
print(series.attrs['meta'])  # response metadata
```

Output:

```text
                GDP
2024-01-01  27939.0
2024-04-01  28202.8
2024-07-01  28488.5
2024-10-01  28949.2
```

NOTE: dpf.ts will parallelize data fetching from FRED, whereas dpf.series will 
not.  

## Setting up the API key

You need a free FRED API key from https://research.stlouisfed.org/docs/api/api_key.html.

**Recommended: environment variable**

```bash
export API_KEY_FED=your_key_here
```

**Pass directly**

```python
import datapungi_fed as dpf

client = dpf.Client(api_key='your_key_here')
```

**Use an environment variable**

```python
import datapungi_fed as dpf

client = dpf.Client()  # reads API_KEY_FED from the environment
```

**Store in a file**

```python
import json

import datapungi_fed as dpf

with open('/path/to/key.json') as f:
    client = dpf.Client(api_key=json.load(f)['API_KEY_FED'])
```

Module-level shortcuts like `dpf.ts('GDP')` still work as long as
``API_KEY_FED`` is set in the environment.

## Working with data


Available database groups:

FRED database group   | description
----------- | -----------
[categories](https://fred.stlouisfed.org/categories/)   | Categories of datasets - 8 top categories (eg, National Accounts, Prices) that break down into subgroups
[releases](https://fred.stlouisfed.org/releases/)       | Release groups of data - about 300 (eg, Western Hemisphere Regional Economic Outlook, Penn World Table)
[series](https://fred.stlouisfed.org/)                  | About 600,000 time series provided by various sources
[sources](https://fred.stlouisfed.org/sources/)         | List of data sources - about 90 data providers (eg, IMF and Bank of Mexico)
[tags](https://fred.stlouisfed.org/tags/)               | Tags applied to time series (eg, location, data source, frequency) - about 5,000 tags
[geo](https://research.stlouisfed.org/docs/api/geofred/)  | Harvest data and shape files found in GeoFRED
datasetlist                                             | list of all (FRED and GeoFRED) databases


The above "dpf.ts('GDP')" calls are quick taps to the "dpf.series" data.


## series
The series datasets provides data and metadata information on a specific time-series,
besides searching tools.

Available databases in the series group:

| database     | Description                                  |
| --------     | -----------                                  |
| series       | metadata of a series                         |
| search       | search for a series                          |
| observations | actual series data                           |
| categories   | FED categories associated to the series      |
| release      | The release table of the series              |
| tags         | Tags associated to the series                |
| updates      | List of series updated (at given parameters) |
| vintagedates | Dates when a series were updated                  |

For example:
```python
# the following are the same, they all call dpf.series.observations:
dpf.ts('gdp')
dpf.series('gdp')
dpf.series.observations('gdp')

# metadata
dpf.series.series('gdp')

# search
dpf.series.search('GDP deflator for the US')
dpf.series.search(search_text='monetary+service+index')
dpf.series.search.tags(series_search_text='monetary+service+index')
dpf.series.search.related_tags(series_search_text='mortgage+rate', tag_names='30-year;frb')
```


### See what is available

```python
import datapungi_fed as dpf

dpf.datasetlist()
```

### Explore categories, releases, sources, and tags

There are hundreds of thousands of time-series available at the FRED API,
navigating them is non-trivial.
Navigate via internal FED classification scheme
```python
import datapungi_fed as dpf

# This is a graph, with starting point "0" which you can navigate with:
#  - use the children function to find the category of interest
#  - once there, use the series function to get the list of series of that category
dpf.category.children(0) #get the children categories of the starting point, 
dpf.category.series(32442)
dpf.category(32442) # get info on the 32442 category
```

Navigate via information on the release information
```python
import datapungi_fed as dpf

dpf.release() # all 337 or so release sources (eg, id=1132, name = Dallas Fed Energy Survey)

```

Explore also via tags and sources with "dpf.tag" and "dpf.source"

```python
dpf.tags.tags(tag_names='monetary+aggregates;weekly')
dpf.tags.related_tags(tag_names='monetary+aggregates;weekly')
dpf.tags.tags.series(tag_names='slovenia;food;oecd')
```

#### Releases
Another way to navigate the data is through releases - a bundled release of 
series around a theme.  For these use the 
```python

# There is about 300 release groups of data
dpf.release()

# get a release of interest (say Z.1 - Fininancial Accounts data, release_id = 52), 
# there are the series included on that release:
dpf.release.series(52) #52 is the id of Z.1 release table, read from the above.

# get the dates the data set was released:
# Note: set "include_release_dates_with_no_data" to true if you want future 
#       release dates.
dpf.release.dates(52,realtime_start='2026-01-01',include_release_dates_with_no_data='true')

# Calendar of US economic data releases for 2027 year:
#  NOTE: use the offset option to get the entire dataset, with increments of 
#        multiples of 1000
dpf.release.releases.dates(realtime_start="2027-01-01",realtime_end="2027-12-31",include_release_dates_with_no_data='true')
```

### Get GeoFRED data

```python
import datapungi_fed as dpf

dpf.geo(series_id='WIPCPI')
dpf.geo.shapes('bea')
dpf.geo.meta(series_id='SMU56000000500000001')
dpf.geo(series_id='WIPCPI', start_date='2012-01-01')
dpf.geo.data(series_group='882', date='2013-01-01', region_type='state', units='Dollars', frequency='a', season='NSA')
```

## Code snippets

Use `verbose=True` to get the raw request and a standalone code snippet:

```python
import datapungi_fed as dpf

result = dpf.series('GNP', verbose=True)

result['dataFrame']  # pandas DataFrame
result['request']    # requests.Response
result['code']       # string of code that reproduces the query
```
## Running tests

The tests are recorded with `pytest-recording`/`vcrpy`. Cassettes are
committed, so the suite runs without a real API key by default.

```bash
# Run the full test suite (no API key needed)
pytest datapungi_fed/tests

# CI-friendly subset
pytest -q datapungi_fed/tests/test_driversCI.py

# Full suite
pytest datapungi_fed/tests/test_drivers.py

# Re-record cassettes (requires a real key)
API_KEY_FED=your_key pytest datapungi_fed/tests
```

Or from Python:

```python
import datapungi_fed as dpf

dpf.tests.runTests()  # opens HTML report in a browser
```

## Requirements

- Python >= 3.13
- requests, pandas, pyyaml




## License

MIT

