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 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.0.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.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tsdbapi-0.3.0.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.0.tar.gz
Algorithm Hash digest
SHA256 775d16885c3aa33b3224f98378da95b9e14f820f6e3702f76f9298800ff50b21
MD5 45305f61a019cba41b35b03a28080bdf
BLAKE2b-256 71357407cc71c98898ffef205fd9b3f4fd57b28999e5402663e634d2eb3af192

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tsdbapi-0.3.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54e90bcf1641f15c574a4ee88bb13284ed7560d73851b7ed5bd75ab81b7140a9
MD5 90d043195d9e28a54a2a528fbff53ce9
BLAKE2b-256 2d5d63c68e4fab6184b8fc1e70040564b3b8c6fc0084a007660fac16ec3f1125

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