Skip to main content

A python interface for the National Statistical API

Project description

natstat-py

A python interface for the National Statistical API

Installation

git clone https://github.com/jhelderman/natstat-py.git
cd natstat-py
pip install .

Configuration

Usage of this API requires an API key for National Statistical, which can be obtained via purchase on the National Statistical website. Configuration can be done via environmental variable or by setting the API key directly when initializing the API in python.

Environmental variable:

export NATSTAT_API_KEY=replace-me
import natstat
api = natstat.NatStatAPIv3()

Python:

import natstat
api = natstat.NatStatAPIv3(api_key="replace-me")

Additionally, the library has built-in rate limiting to comply with the API policies. The rate limiting can be configured on an hourly basis through the NATSTAT_REQ_PER_HOUR environmental variable. For example:

export NATSTAT_REQ_PER_HOUR=500

Usage

Basic request

The basic workflow for using the API is to initialize an API object, select the method corresponding to the API endpoint, form the corresponding request object, and invoke the method on the request. There is a method for each endpoint in the API. See the NatStat documentation for more information on the endpoints. The following example makes a request to the games endpoint for the mbb service for 2025-11-23.

api = natstat.NatStatAPIv3()
req = natstat.GamesReq(service="mbb", date=datetime.datetime(2025, 11, 23))
resp = api.games(req)
print(resp.to_dataframe())

Responses from the API can be converted to a pandas dataframe as is done in the last line of the example. Each record in the response will become a row in the dataframe.

Cached request

Caching is not supported directly in this library but can be implemented easily with the requests-cache library. To cache requests, make a cached session with the desired configuration and pass it when the API object is created.

# cache HTTP requests locally on the file system
# this library also has options to back the cache with Redis or a database
session = requests_cache.CachedSession("natstat", backend="filesystem")
api = natstat.NatStatAPIv3(session=session)
req = natstat.GamesReq(service="mbb", date=datetime.datetime(2025, 11, 23))
resp = api.games(req)
# request will used cached data instead of hitting the API
cached = api.games(req)
assert not isinstance(cached, natstat.NatStatError)
assert resp.data == cached.data
# user and meta objects have request-specific information
# if the response was successfully cached, these will be the same
assert resp.user() == cached.user()
assert resp.meta() == cached.meta()

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

natstat-0.1.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

natstat-0.1.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file natstat-0.1.0.tar.gz.

File metadata

  • Download URL: natstat-0.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for natstat-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2dfefe6cb43c04c3580be7bcd606553470cd16921ad823bd67de44f188fd6083
MD5 ad6e6a172f82ecd6fce6d290607ebc8d
BLAKE2b-256 abac70e1a4b068f629f6f55bfc4a8953f73e0233cfacaedc8fd3ebfe94ee8b21

See more details on using hashes here.

File details

Details for the file natstat-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: natstat-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for natstat-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2e16cc310c6c3638aaf912c5db794888b903afda36e11255a2a3e71290b89cb
MD5 592f78401fdbb590eed15a754a636895
BLAKE2b-256 7a2f345f19b06c5def7625882f06f703e1bf97e6a15cb3870a61a407646b5e60

See more details on using hashes here.

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