a Python Polars interface to q
Project description
kola
A Python Polars interface to kdb+/q, powered by Rust.
Installation
pip install kola
Requirements: Python ≥ 3.10, Polars ≥ 1.31.0, PyArrow ≥ 20.0.0
Quick Start
Create a Connection
import polars as pl
import kola
# basic connection
conn = kola.Q('localhost', 1800)
# with authentication
conn = kola.Q('localhost', 1800, user='user', passwd='password')
# with TLS and retry
conn = kola.Q('localhost', 1800, enable_tls=True, retries=3, timeout=30)
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
host |
str |
Hostname of the q process | |
port |
int |
Port of the q process | |
user |
str |
"" |
Username (defaults to OS login user) |
passwd |
str |
"" |
Password |
enable_tls |
bool |
False |
Enable TLS encryption |
retries |
int |
0 |
Number of retries with exponential backoff |
timeout |
int |
0 |
Connection timeout in seconds (0 = no timeout) |
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, pl.Series, pl.DataFrame, and dict (with string keys).
from datetime import date, time
conn.sync(
".gw.query",
"table",
{
"date": date(2023, 11, 21),
"syms": pl.Series("", ["sym0", "sym1"], kola.QType.Symbol),
"startTime": time(9),
"endTime": time(11, 30),
},
)
Send DataFrame
# Polars DataFrame
conn.sync("upsert", "table", pl_df)
# Pandas DataFrame (cast to Polars first)
conn.sync("upsert", "table", pl.DataFrame(pd_df))
Async Query
conn.asyn("upsert", "table", pl_df)
Subscribe
from kola import QType
conn.sync(".u.sub", pl.Series("", ["table1", "table2"], QType.Symbol), "")
# with symbol filter
conn.sync(
".u.sub",
pl.Series("", ["table1", "table2"], QType.Symbol),
pl.Series("", ["sym1", "sym2"], QType.Symbol),
)
while True:
# returns ("upd", "table", pl.DataFrame)
upd = conn.receive()
print(upd)
Generate IPC Bytes
Serialize data as kdb+ IPC bytes without a connection.
from kola import serialize_as_ipc_bytes6
df = pl.DataFrame(
{
"sym": pl.Series("sym", ["a", "b", "c"], pl.Categorical),
"price": [1, 2, 3],
}
)
# without compression
buffer = serialize_as_ipc_bytes6("sync", False, ["upd", "table", df])
# with compression
buffer = serialize_as_ipc_bytes6("sync", True, ["upd", "table", df])
msg_type: "async" | "sync" | "response"
Read Binary Table
Read a kdb+ binary table (splayed/flat file) directly into a Polars DataFrame.
from kola import read_binary6
df = read_binary6("/path/to/binary/table")
Error Handling
from kola import KolaError, KolaIOError, KolaAuthError
try:
conn.sync("select from trade")
except KolaAuthError:
print("Authentication failed")
except KolaIOError:
print("Connection error")
except KolaError:
print("General kola error")
QType
kola.QType provides Polars dtype aliases for q types, useful when constructing pl.Series for functional queries.
| QType | Polars dtype |
|---|---|
Boolean |
pl.Boolean |
Guid |
pl.Array(pl.Binary, 16) |
Byte |
pl.UInt8 |
Short |
pl.Int16 |
Int |
pl.Int32 |
Long |
pl.Int64 |
Real |
pl.Float32 |
Float |
pl.Float64 |
Char |
pl.UInt8 |
String |
pl.Utf8 |
Symbol |
pl.Categorical |
Timestamp |
pl.Datetime("ns") |
Date |
pl.Date |
Datetime |
pl.Datetime("ms") |
Timespan |
pl.Duration("ns") |
Time |
pl.Time |
Data Type Mapping
Deserialization (q → Python)
Atom
| 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 |
List / Table
| q type | n | size | Polars dtype |
|---|---|---|---|
boolean list |
1 | 1 | pl.Boolean |
guid list |
2 | 16 | pl.Array(pl.Binary, 16) |
byte list |
4 | 1 | pl.UInt8 |
short list |
5 | 2 | pl.Int16 |
int list |
6 | 4 | pl.Int32 |
long list |
7 | 8 | pl.Int64 |
real list |
8 | 4 | pl.Float32 |
float list |
9 | 8 | pl.Float64 |
char list |
10 | 1 | pl.Utf8 |
string list |
10 | 1 | pl.Utf8 |
symbol list |
11 | * | pl.Categorical |
timestamp list |
12 | 8 | pl.Datetime("ns") |
month list |
13 | 4 | - |
date list |
14 | 4 | pl.Date |
datetime list |
15 | 8 | pl.Datetime("ms") |
timespan list |
16 | 8 | pl.Duration("ns") |
minute list |
17 | 4 | pl.Time |
second list |
18 | 4 | pl.Time |
time list |
19 | 4 | pl.Time |
table |
98 | * | pl.DataFrame |
dictionary |
99 | * | - |
keyed table |
99 | * | pl.DataFrame |
Guid is deserialized as a 16-byte fixed binary array. Use
.hex()to convert to string if needed:df.with_columns(pl.col("uuid").apply(lambda u: u.hex()))
real/float0nis mapped to Polarsnull, notNaN.
short/int/longnull and infinity values (0Nh/i/j,0Wh/i/j,-0Wh/i/j) are mapped tonull.
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 |
Series, DataFrame, and Dictionary
| Polars dtype | q type |
|---|---|
dict |
dict |
pl.Boolean |
boolean |
pl.Array(pl.Binary, 16) |
guid |
pl.UInt8 |
byte |
pl.Int16 |
short |
pl.Int32 |
int |
pl.Int64 |
long |
pl.Float32 |
real |
pl.Float64 |
float |
pl.Utf8 |
char |
pl.Categorical |
symbol |
pl.Datetime |
timestamp |
pl.Date |
date |
pl.Duration |
timespan |
pl.Time |
time |
pl.DataFrame |
table |
Dictionary serialization requires
strkeys.
Polars Documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kola-2.3.2.tar.gz.
File metadata
- Download URL: kola-2.3.2.tar.gz
- Upload date:
- Size: 62.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29d312b4b30bed8b63640c2cfb774431497dd880b8cf13a27691d0f53dc690cb
|
|
| MD5 |
9ee9b36c38cc2d2c04f3084ce09b1f13
|
|
| BLAKE2b-256 |
ee324e8903aa89ac33605e922e70e40c4713f5ad10a797499520265b0d6116e2
|
File details
Details for the file kola-2.3.2-cp310-abi3-win_amd64.whl.
File metadata
- Download URL: kola-2.3.2-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 2.5 MB
- Tags: CPython 3.10+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a93c405184209689b8670044291f63c8d371e212200f02bba3f94c5066d469dc
|
|
| MD5 |
a7a0111d88d1712a2dbdb1c843a0925e
|
|
| BLAKE2b-256 |
fef4ef22fb68d688a3118429e5d22233574b23822eca31ac46a1ddb12cf5bc35
|
File details
Details for the file kola-2.3.2-cp310-abi3-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: kola-2.3.2-cp310-abi3-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.10+, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3452181e1afdeae9601dc6ab4f2b22bfa03f4068679029bc3da7755ea0687eef
|
|
| MD5 |
76c63f5e1af7b51580f485c32d4fc825
|
|
| BLAKE2b-256 |
21da83a6f7842a7958fdbf98f9fe458a4a3b82268a8171f19b39d2e1ee916c21
|
File details
Details for the file kola-2.3.2-cp310-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: kola-2.3.2-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 2.3 MB
- Tags: CPython 3.10+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce49475ed20f583fb50b9a7b83b780bd9633e97539924d84246a5e652159785d
|
|
| MD5 |
f7e13e09f9f689b030cbec8d7194bd13
|
|
| BLAKE2b-256 |
44dbe2326f7f33b687cf74873f0ad1c97c65c3ce7a6c564d9a67e6a592c74d0b
|
File details
Details for the file kola-2.3.2-cp310-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: kola-2.3.2-cp310-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 2.6 MB
- Tags: CPython 3.10+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f00ffda0f660628076ea858b10718d04e6eb858830ed815984a68db3f66e1841
|
|
| MD5 |
673396e37b2d6387f50c5838417afc88
|
|
| BLAKE2b-256 |
2eb81810da210bb2721c02d87926c48f54e31a657c70e72ef1a683cb67e7d8de
|