Skip to main content

XQDB — Python Bindings

XQDB is independent and not affiliated with or endorsed by KX. kdb+ is a trademark of KX.

A Python interface to kdb+/q powered by Narwhals, with support for multiple dataframe backends (PyArrow, pandas, Polars).

Installation

Requirements: Python ≥ 3.10, Narwhals ≥ 2.10, PyArrow ≥ 20.0.0

Optional backend packages: pandas, polars

Install the published package:

python -m pip install xqdb

To build from source for development with a Rust toolchain:

python -m pip install -e .

Quick Start

import xqdb
import narwhals as nw

# Basic connection (PyArrow backend by default)
conn = xqdb.Q('localhost', 1800)

# Select an installed Narwhals output backend
conn = xqdb.Q('localhost', 1800, backend='polars')

# Authentication credentials require TLS unless the connection is already protected
conn = xqdb.Q(
    'localhost', 1800, user='user', passwd='password', enable_tls=True
)

# With TLS and retry
conn = xqdb.Q('localhost', 1800, enable_tls=True, retries=3, timeout=30)

Connection Parameters

Parameter Type Default Description
host str Hostname of the q process
port int Port of the q process
backend str "pyarrow" Installed Narwhals output backend; tested with "pyarrow", "pandas", and "polars"
user str "" q username; empty unless explicitly supplied
passwd str "" Password
enable_tls bool False Enable TLS with platform certificate verification
retries int 0 Number of retries with exponential backoff
timeout int 0 Connection timeout in seconds (0 = no timeout)

q IPC authentication sends credentials in cleartext when TLS is disabled. Enable TLS for credentialed connections unless another trusted transport already protects the socket.

Narwhals DataFrames and Backend Selection

Results from conn.sync() and conn.receive() return Narwhals DataFrames or Series backed by the selected backend. The default is PyArrow; pandas and Polars are supported when their optional packages are installed. An unavailable or unknown backend raises an error—XQDB does not silently substitute another backend.

To extract the underlying native DataFrame:

result = conn.sync("select from trade")  # Narwhals DataFrame
native_df = nw.to_native(result)  # PyArrow, pandas, or Polars Table/DataFrame

Input Constraints

  • Native or Narwhals eager DataFrames and Series are accepted.
  • Lazy frames are rejected rather than collected implicitly.
  • The Python/native boundary uses the Arrow C Stream interface; it does not serialize frames to Arrow IPC bytes.

Temporal range and precision

Python datetime, time, and timedelta values have microsecond precision. q timestamp or timespan atoms with non-zero sub-microsecond nanoseconds raise ValueError instead of being truncated. q date or datetime atoms outside Python's representable range raise OverflowError instead of being clamped.

Connect / Disconnect

# explicitly connect (auto-connects on first query)
conn.connect()

# disconnect (auto-disconnects on IO error)
conn.disconnect()

String Query

conn.sync("select from trade where date=last date")

Functional Query

Supports Python basic data types, Narwhals Series/DataFrame, and dict (with string keys).

from datetime import date, time

import pyarrow as pa

symbols = pa.chunked_array(
    [pa.array(["sym0", "sym1"]).dictionary_encode()]
)
conn.sync(
    ".gw.query",
    "table",
    {
        "date": date(2023, 11, 21),
        "syms": symbols,
        "startTime": time(9),
        "endTime": time(11, 30),
    },
)

Operators and Lambdas

Pass q primitives and arbitrary lambdas as first-class arguments:

from xqdb import XqdbQLambda, XqdbQOperator

conn.sync("{[op;a;b] .[op;(a;b)]}", XqdbQOperator.PLUS, 1, 2)
conn.sync("{[op;a;b] .[op;(a;b)]}", XqdbQLambda("{x+y}"), 1, 2)

# A non-root q context can be supplied explicitly.
scoped = XqdbQLambda("{x+y}", "analytics")

XqdbQOperator(name) accepts supported q primitive names such as "+"; it does not expose wire opcodes. XqdbQLambda(source, context="") preserves its source text, requires a brace-delimited UTF-8 body (optionally prefixed with k)), rejects NUL bytes in both fields, and rejects context values beginning with ".". The context "analytics" represents q namespace .analytics because the wire context omits the leading dot. Lambda source is executable q code: construct it only from trusted input.

Send DataFrame

import pyarrow as pa

frame = pa.table({"sym": ["a", "b"], "price": [10.5, 11.0]})
conn.sync("upsert", "table", frame)

Async Query

conn.asyn("upsert", "table", frame)

Subscribe

import pyarrow as pa

tables = pa.chunked_array(
    [pa.array(["table1", "table2"]).dictionary_encode()]
)
symbols = pa.chunked_array(
    [pa.array(["sym1", "sym2"]).dictionary_encode()]
)
conn.sync(".u.sub", tables, "")
conn.sync(".u.sub", tables, symbols)

while True:
    # returns ("upd", "table", Narwhals DataFrame)
    upd = conn.receive()
    print(upd)

Generate IPC Bytes

Serialize data as kdb+ IPC bytes without a connection.

import pyarrow as pa

from xqdb import serialize_as_ipc_bytes6

frame = pa.table({"sym": ["a", "b"], "price": [10.5, 11.0]})

# without compression
buffer = serialize_as_ipc_bytes6("sync", False, ["upd", "table", frame])

# with compression
buffer = serialize_as_ipc_bytes6("sync", True, ["upd", "table", frame])

msg_type: "async" | "sync" | "response"

Read Binary Table

Read a regular q binary table file directly into a Narwhals DataFrame. Select the native output backend independently of the file format.

from xqdb import read_binary6

df = read_binary6("/path/to/table.bin", backend="pandas")

Error Handling

from xqdb import XqdbError, XqdbIOError, XqdbAuthError

try:
    conn.sync("select from trade")
except XqdbAuthError:
    print("Authentication failed")
except XqdbIOError:
    print("Connection error")
except XqdbError:
    print("General xqdb error")

Data Type Mapping

Deserialization (q → Python)

q scalars map to Python scalars; q vectors/tables are returned as Narwhals DataFrames/Series backed by the selected backend.

Atom (scalar to Python)

q type n size Python type Note
boolean 1 1 bool
guid 2 16 str
byte 4 1 int
short 5 2 int
int 6 4 int
long 7 8 int
real 8 4 float
float 9 8 float
char 10 1 str
string 10 1 str
symbol 11 * str
timestamp 12 8 datetime
month 13 4 -
date 14 4 date 0001.01.01 - 9999.12.31
datetime 15 8 datetime
timespan 16 8 timedelta
minute 17 4 time 00:00 - 23:59
second 18 4 time 00:00:00 - 23:59:59
time 19 4 time 00:00:00.000 - 23:59:59.999
primitive 101-103 1 XqdbQOperator supported unary/binary/ternary primitive
lambda 100 * XqdbQLambda source and q context

Vector and Table (Arrow-backed Narwhals)

q type PyArrow representation Notes
boolean list bool Native Arrow boolean
byte list uint8 Native Arrow unsigned 8-bit
short list int16 Native Arrow signed 16-bit
int list int32 Native Arrow signed 32-bit
long list int64 Native Arrow signed 64-bit
real list float32 Native Arrow single precision
float list float64 Native Arrow double precision
char/strings string_view Arrow UTF-8 string view
symbol list dictionary-encoded string Preserves q symbol semantics
guid list binary_view Every non-null value is 16 bytes
nested list large_list Child type follows the q list type
timestamp list timestamp[ns] Nanosecond timestamp
date list date32 Days since the Unix epoch
datetime list timestamp[ms] Millisecond timestamp
timespan list duration[ns] Nanosecond duration
minute list time64[ns] Nanosecond time-of-day
second list time64[ns] Nanosecond time-of-day
time list time64[ns] Nanosecond time-of-day
table pyarrow.Table Returned through a Narwhals frame
keyed table pyarrow.Table Key and value columns are combined

Other selected backends receive the equivalent representation that Narwhals can construct from this Arrow stream. Backend-specific dtypes may differ while values and q semantics remain the same.

real/float 0n is mapped to null, not NaN.

short/int/long null and infinity values (0Nh/i/j, 0Wh/i/j, -0Wh/i/j) are mapped to null.

Serialization (Python → q)

Basic Data Type

Python type q type Note
bool boolean
int long
float float
str symbol
bytes string
datetime timestamp
date date 0001.01.01 - 9999.12.31
timedelta timespan
time time 00:00:00.000 - 23:59:59.999
XqdbQOperator primitive supported primitive name
XqdbQLambda lambda source and q context

Series, DataFrame, and Dictionary

Arrow C Stream dtype q type
boolean boolean list
uint8 byte list
int16 short list
int32 int list
int64 long list
float32 real list
float64 float list
string/string view general list of char vectors
dictionary-encoded string symbol list
16-byte binary values guid list
timestamp timestamp list
date32 date list
duration timespan list
time64 time list
nested numeric list general list of typed q vectors
eager DataFrame table

Dictionary serialization requires str keys.

Resources

License

XQDB is licensed under the BSD-3-Clause permissive open-source license, which permits use in proprietary and commercial applications.

Download files

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

Source Distribution

xqdb-0.1.3.tar.gz (265.8 kB view details)

Uploaded Source

Built Distributions

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

xqdb-0.1.3-cp310-abi3-win_amd64.whl (6.1 MB view details)

Uploaded CPython 3.10+Windows x86-64

xqdb-0.1.3-cp310-abi3-manylinux_2_28_x86_64.whl (5.8 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ x86-64

xqdb-0.1.3-cp310-abi3-macosx_11_0_arm64.whl (4.9 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file xqdb-0.1.3.tar.gz.

File metadata

  • Download URL: xqdb-0.1.3.tar.gz
  • Upload date:
  • Size: 265.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for xqdb-0.1.3.tar.gz
Algorithm Hash digest
SHA256 871bcc4f899e5db501426bebfe0923388c7f26c58b7826f9568a29782c3b85e5
MD5 6ed906a6ce1a2833e10ccc49b8810162
BLAKE2b-256 9312928ff07e653e538343c8e081f22a71481560dd2d6b8cf886ac19adcea9e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for xqdb-0.1.3.tar.gz:

Publisher: CI.yml on xbbg-org/xqdb

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

File details

Details for the file xqdb-0.1.3-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: xqdb-0.1.3-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 6.1 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for xqdb-0.1.3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 91054cf419c4dcebf16eda47d4ef47b04f09ed04700f07bc504974b6b6edde3c
MD5 40ea9effc8c1258ad116510f8e5ef5d4
BLAKE2b-256 a2248ec2ad6c7362b581ae914a0bf5e032f94e056def9470310f8598d10db617

See more details on using hashes here.

Provenance

The following attestation bundles were made for xqdb-0.1.3-cp310-abi3-win_amd64.whl:

Publisher: CI.yml on xbbg-org/xqdb

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

File details

Details for the file xqdb-0.1.3-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for xqdb-0.1.3-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a029d603ed551b7b2dfa29784106bfbf008baa0aa3d58ded34f20ef0b300b9f2
MD5 1b6b9ef5ba299a364b843f1c7dc6ba16
BLAKE2b-256 42e628d1bacd3f120d78f89fef0fbdd9e8cb35a9f354c87fd082ce218eacfc6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for xqdb-0.1.3-cp310-abi3-manylinux_2_28_x86_64.whl:

Publisher: CI.yml on xbbg-org/xqdb

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

File details

Details for the file xqdb-0.1.3-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: xqdb-0.1.3-cp310-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 4.9 MB
  • Tags: CPython 3.10+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for xqdb-0.1.3-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9e4d14e5814d617e05954bea6086089909bd0d0797bd010fa396da8d006f8608
MD5 a7cc6f5dd3bd4ee04e37f22b5b97e575
BLAKE2b-256 304c51864717b9f30c309ba2d906e4d5d03f4510acc28519004ca57e003cc1af

See more details on using hashes here.

Provenance

The following attestation bundles were made for xqdb-0.1.3-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: CI.yml on xbbg-org/xqdb

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

Release history Release notifications | RSS feed

This release

0.1.3 This release

4 files

0.1.2

4 files

0.1.1

4 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page