Skip to main content

Interact with the KOF Time Series Database API

Project description

Interact with the KOF Time Series Database API

The tsdbapi package is a Python wrapper for the KOF Time Series DB API. The functionality of the package includes reading time series data, metadata and release information from the KOF time series database. At the moment, the functionality of the package is quite limited (in contrast to the R package). If you miss a function for a particular API endpoint, check for updates regularly. The package functions convert the JSON data returned by the KOF Time Series DB API to either a Python dict or a polars.DataFrame.

Installation

Install the package with

pip install tsdbapi

Basic Usage

Use the function read_ts to read time series from the KOF time series database. The code below reads the time series with the key ch.kof.barometer and returns it as a polars.DataFrame.

from tsdbapi import read_ts

read_ts(ts_keys="ch.kof.barometer")

Read multiple time series at the same time by providing a list of keys:

read_ts(ts_keys=["ch.kof.globalbaro.leading","ch.kof.globalbaro.leading"])

Authorization

When running the code above, you will be redirected to KOF's identity provider (Keycloak), where you have to log in with your KOF credentials (unless you are already logged in). If you do not have a KOF account, you can still access the public time series in the KOF time series database by setting the access_type to public:

from tsdbapi import read_ts, set_config

set_config(access_type = "public")
read_ts("ch.kof.barometer")

The time series ch.kof.barometer is a public time series.

For programmatic access without user log in, for example in a non-interactive session, you must use an API key. Create an API key for your user with:

from tsdbapi import create_user_api_key

api_key = create_user_api_key()
print(api_key)

Any previously created API key will be overwritten and therefore invalidated. Store the newly created API key securely because it cannot be retrieved later.

Use the API key by setting the environment variable TSDBAPI_API_KEY before running a Python script. Alternatively, you can set the corresponding package configuration option:

from tsdbapi import read_ts, set_config

set_config(api_key="my_api_key")
read_ts(ts_keys="ch.kof.barometer")

Vintages

Every time series can have multiple vintages (or versions). A time series vintage is based on the data available at its vintage date.

By default, read_ts returns the most recent vintage (or version) of the time series. To specify a different vintage, use the valid_on parameter. The code below reads the KOF barometer vintage based on the data available at January 15, 2026.

from tsdbapi import read_collection_ts
from datetime import date

read_ts("ch.kof.barometer", valid_on = date(2026, 1, 15))

For users with role extern (everyone not employed at KOF), a time series vintage is only visible once it has been officially released, hence its data, including its vintage date, can only be read after release. A time series vintage is usually released several days after its vintage date.

Release information

The release information, including the release time, of the most recent time series vintage can be read with

from tsdbapi import read_ts_release

read_ts_release(ts_keys="ch.kof.barometer")

Use the valid_on parameter to specify a different vintage. For users with the role extern, only vintages that have been released are visible. However, the release information of future, yet to be released time series vintages can be read with

from tsdbapi import read_ts_release_future

read_ts_release_future(ts_keys="ch.kof.barometer")

Note that he release times of future vintages are not guaranteed and are subject to change (although changes are rare).

Download Quota

If you are a KOF data service subscriber, the number of time series downloads (reads) per year is limited by a quota. You can check your annual download quota and the number of time series downloads remaining in the current subscription year with

from tsdbapi import read_user_quota

read_user_quota()

Collections

To read an entire collection of time series use

from tsdbapi import read_collection_ts

read_collection_ts(collection="bs_indicator", owner="public")

Every time series collection has an owner. By default, the owner is assumed to be yourself (owner="self").

You can list all collections visible to you with

from tsdbapi import read_collection_ts

list_collections()

For users with the role extern, the includes all collections owned by the user himself and by the user public.

Metadata

The metadata of one or multiple time series can be read with

from tsdbapi import read_metadata_ts

read_ts_metadata(ts_keys="ch.kof.barometer")

To read the metadata of an entire collection of time series:

from tsdbapi import read_collection_ts_metadata

read_collection_ts_metadata(collection="bs_indicator", owner="public")

Advanced usage

Consult the package help for a detailed documentation of all available functions.

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

tsdbapi-0.3.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

tsdbapi-0.3.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file tsdbapi-0.3.1.tar.gz.

File metadata

  • Download URL: tsdbapi-0.3.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tsdbapi-0.3.1.tar.gz
Algorithm Hash digest
SHA256 4adba85c39213e82d4790785fc7a8b787353cb2789c5fcc8ffedad1bf04c9a4d
MD5 9508e4ed187ab3544aa861e6b3c1db69
BLAKE2b-256 66a0bc9a49ea8caecc13e7bb63a3393b8a8f162c41fdbd096504d3c3e3c53644

See more details on using hashes here.

File details

Details for the file tsdbapi-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: tsdbapi-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tsdbapi-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7030a400e329532f012fed21bfce2885b1996d30faed54da22e2ff65d550164f
MD5 f658287c29b0c75f3a7469a8cb03bb51
BLAKE2b-256 94ea037fd84211ed6ea35122baf7c7939a0b1f7857856efee6d0dd4e0d58484f

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