Skip to main content

A yfinance-style Python client for policyuncertainty.com data

Project description

economicpolicyuncertainty

A yfinance-style Python client for policyuncertainty.com. Fetch any EPU index as a pandas DataFrame, cached locally in SQLite so repeat calls don't re-hit the source.

Installation

pip install economicpolicyuncertainty

With the optional REST server:

pip install economicpolicyuncertainty[server]

Quick start

import economicpolicyuncertainty as pu

pu.available()                                        # DataFrame of every series
pu.download("UK_DAILY", refresh=True)                 # fetch + return DataFrame indexed by date
pu.download("ALL_COUNTRIES_MONTHLY", column="UK")     # one country from the panel file

s = pu.Series("UK_DAILY")
s.info                                                # registry metadata dict
s.history(start="2020-01-01", end="2020-12-31")       # filtered DataFrame
s.columns()                                           # for wide series, list available columns
s.latest()                                            # most recent record as a dict
s.refresh()                                           # force re-fetch from source

pu.refresh_all()                                      # refresh every series; failures are isolated

Indicators

Key Description Frequency Coverage
US_MONTHLY Baker, Bloom & Davis US EPU index Monthly 1985 – present
US_DAILY Multi-variant daily US EPU index Daily 1985 – present
UK_DAILY UK daily EPU index Daily 2001 – present
ALL_COUNTRIES_MONTHLY ~24-country EPU panel + global GEPU Monthly 1985 – present
US_CATEGORICAL US EPU broken out by policy category Monthly 1985 – present
TRADE_POLICY_UNCERTAINTY Daily US trade policy uncertainty (TPU) Daily 1985 – present
MONETARY_POLICY_UNCERTAINTY Baker-Bloom-Davis US monetary policy uncertainty Monthly 1985 – present
CLIMATE_POLICY_UNCERTAINTY Gavriilidis et al. climate policy uncertainty Monthly 1985 – present
US_STATE_EPU State-level EPU, one column per US state Monthly 1985 – present
US_CHINA_TENSION Rogers, Sun & Sun US-China tension index Monthly 1993 – present

Multi-column series

ALL_COUNTRIES_MONTHLY, US_CATEGORICAL, and US_STATE_EPU each contain many columns in a single file. Use column= to filter to one, or .columns() to see what's available:

s = pu.Series("ALL_COUNTRIES_MONTHLY")
s.columns()
# ['australia', 'brazil', 'canada', 'chile', 'china', 'france',
#  'gepu_current', 'gepu_ppp', 'germany', 'greece', 'india', 'ireland',
#  'italy', 'japan', 'korea', 'mainland_china', 'mexico', 'pakistan',
#  'russia', 'scmp_china', 'singapore', 'spain', 'uk', 'us']

pu.download("ALL_COUNTRIES_MONTHLY", column="uk")

s = pu.Series("US_CATEGORICAL")
s.columns()
# ['1_economic_policy_uncertainty', '2_monetary_policy', '3_taxes',
#  '4_government_spending', '5_health_care', '6_national_security',
#  '7_entitlement_programs', '8_regulation', '9_trade_policy',
#  '10_sovereign_debt_currency_crises', 'financial_regulation',
#  'fiscal_policy_taxes_or_spending']

Column names are lowercased and have spaces replaced with underscores. Pass either the raw name ("United Kingdom") or the sanitized form ("uk") — both work.

API reference

pu.available() → DataFrame

Returns a DataFrame listing every series and its metadata (key, frequency, shape, url, notes).

pu.download(key, start=None, end=None, column=None, refresh=False) → DataFrame

Returns the series as a DataFrame indexed by date. Reads from the local cache by default; pass refresh=True to fetch fresh data first.

pu.Series(key)

Object-oriented interface to a single series.

Method Returns
.history(start, end, column) DataFrame
.columns() list of column names (wide series only)
.latest(column) most recent record as a dict
.refresh() re-fetches from source, returns stats dict
.info registry metadata dict

pu.refresh_all() → list[dict]

Refreshes every series. Each failure is caught individually so one bad URL doesn't block the rest.

Local REST API (optional)

Useful for accessing data from non-Python environments.

python server.py          # runs at http://127.0.0.1:5000

Refreshes all series on startup, then daily at 06:00 local time (REFRESH_HOUR / REFRESH_MINUTE env vars to change it).

Endpoint Description
GET /api/series all series + metadata
GET /api/series/<key> data as JSON (?start=&end=&column=)
GET /api/series/<key>/columns available columns for wide series
GET /api/series/<key>/latest most recent record
POST /api/series/<key>/refresh force re-fetch of one series
POST /api/refresh-all force re-fetch of everything

Handling the source's bot check

The site occasionally serves a JavaScript challenge page to automated requests. Every fetch tries a browser-header requests session first, then falls back to cloudscraper. If both fail, download the file manually and feed it in:

from economicpolicyuncertainty import fetch, db, registry

entry = registry.get("UK_DAILY")
with open("UK_Daily_Policy_Data.csv", "rb") as f:
    rows = fetch.parse(f.read(), entry)
db.upsert_rows(entry["key"], rows, shape=entry["shape"])

Verifying sources

If a series stops returning data, run:

python check_sources.py

It reports OK <rows> or FAILED <error> for every registry entry. A broken URL is a one-line fix in policyuncertainty/registry.py.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

economicpolicyuncertainty-0.1.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

economicpolicyuncertainty-0.1.1-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file economicpolicyuncertainty-0.1.1.tar.gz.

File metadata

File hashes

Hashes for economicpolicyuncertainty-0.1.1.tar.gz
Algorithm Hash digest
SHA256 72631ead7a869084f38fba621c8b64c39892a6011daed6755dac0a3a144e9ca1
MD5 2784a81fa99333bf43ae519ad1797302
BLAKE2b-256 587fbb5184860f423b847302e01e8bbbee43953005268409341592de922290c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for economicpolicyuncertainty-0.1.1.tar.gz:

Publisher: publish.yml on conorzen/economicpolicyuncertainty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file economicpolicyuncertainty-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for economicpolicyuncertainty-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3768eecf8b9796177b69e57ab9b0e9e190b2c2215a43efb06e7015b179700b96
MD5 b11a351a128dabc44eb1068a2b87eeed
BLAKE2b-256 5dbc5c96a2fe8cca2527d86473334e429faf6de3f496f619392f92c31c7134ad

See more details on using hashes here.

Provenance

The following attestation bundles were made for economicpolicyuncertainty-0.1.1-py3-none-any.whl:

Publisher: publish.yml on conorzen/economicpolicyuncertainty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page