Skip to main content

a Python Polars interface to q

Project description

kola

a Python Polars Interface to q

Basic Data Type Map

q

Deserialization

Atom
k 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
Composite Data Type
k type n size python type
boolean list 1 1 pl.Boolean
guid list 2 16 pl.List(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
month list 13 4 -
date list 14 4 pl.Date
datetime list 15 8 pl.Datetime
timespan list 16 8 pl.Duration
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

performance is impacted by converting guid to string, deserialize the uuid to 16 fixed binary list, use .hex() to convert binary to string if required

real/float 0n is mapped to Polars null not NaN

short/int/long 0Nh/i/j, 0Wh/i/j and -0Wh/i/j are mapped to null

df.with_columns([
    (pl.col("uuid").apply(lambda u: u.hex()))
    ])

Serialization

Basic Data Type
python type k type note
bool boolean
int long
float float
str symbol
bytes string
datetime timestamp
date date 0001.01.01 - 9999.12.31
datetime datetime
timedelta timespan
time time 00:00:00.000 - 23:59:59.999
Dictionary, Series and DataFrame
python type k type
dict dict
pl.Boolean boolean
pl.List(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.Datetime datetime
pl.Duration timespan
pl.Time time
pl.DataFrame table

Limited Support for dictionary as arguments, requires string as keys.

Quick Start

Create a Connection

import polars as pl
import kola
# Connect to q
conn = kola.Q('localhost', 1800)

Connect(Optional)

Automatically connect when querying q process

conn.connect()

Disconnect

Automatically disconnect if any IO error

conn.disconnect()

String Query

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

Functional Query

For functional query, kola supports Python Basic Data Type, pl.Series, pl.DataFrame and Python Dictionary with string keys and Python Basic Data Type and pl.Series values.

from datetime import date, time

conn.sync(
    ".gw.query",
    "table",
    {
        "date": date(2023, 11, 21),
        "syms": pl.Series("", ["sym0", "sym1"], pl.Categorical),
        # 09:00
        "startTime": time(9),
        # 11:30
        "endTime": time(11, 30),
    },
)

Send DataFrame

# pl_df is a Polars DataFrame
conn.sync("upsert", "table", pl_df)
# pd_df is a Pandas DataFrame, use pl.DateFrame to cast Pandas DataFrame
conn.sync("upsert", "table", pl.DataFrame(pd_df))

Async Query

# pl_df is a Polars DataFrame
conn.asyn("upsert", "table", pl_df)

Subscribe

from kola import QType

conn.sync(".u.sub", pl.Series("", ["table1", "table2"], QType.Symbol), "")

# specify symbol filter
conn.sync(
    ".u.sub",
    pl.Series("", ["table1", "table2"], QType.Symbol),
    pl.Series("", ["sym1", "sym2"], QType.Symbol),
)

while true:
    # ("upd", "table", pl.Dataframe)
    upd = conn.receive()
    print(upd)

Generate IPC for q

import polars as pl
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])

Polars Documentations

Refer to

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

kola-2.3.1.tar.gz (60.1 kB view details)

Uploaded Source

Built Distributions

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

kola-2.3.1-cp310-abi3-win_amd64.whl (2.5 MB view details)

Uploaded CPython 3.10+Windows x86-64

kola-2.3.1-cp310-abi3-manylinux_2_28_x86_64.whl (3.0 MB view details)

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

kola-2.3.1-cp310-abi3-macosx_11_0_arm64.whl (2.3 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

kola-2.3.1-cp310-abi3-macosx_10_12_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file kola-2.3.1.tar.gz.

File metadata

  • Download URL: kola-2.3.1.tar.gz
  • Upload date:
  • Size: 60.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","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

Hashes for kola-2.3.1.tar.gz
Algorithm Hash digest
SHA256 ecab816cc4a6d90de2bdc1538dc961fbbea9ee2ff07612006cad852f15dd9004
MD5 0bb5d0ef9d4f5470dfdd8f3851e96f7b
BLAKE2b-256 29bff797b5783625423e86ddb1bdcd1c48ec3437c6f4eb7a189251e96e228ba7

See more details on using hashes here.

File details

Details for the file kola-2.3.1-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: kola-2.3.1-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.8 {"installer":{"name":"uv","version":"0.11.8","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

Hashes for kola-2.3.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4b1afa1c4314a5cef1fb57f3dd5366eccf21f31ab11322825164bec65c1d7493
MD5 4cc1b52746d7790787c94ce818377c8d
BLAKE2b-256 a7fa25410dd3879a722b5958c338963399f0bf5fb3b66028915a0f0ca351bdb0

See more details on using hashes here.

File details

Details for the file kola-2.3.1-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: kola-2.3.1-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.8 {"installer":{"name":"uv","version":"0.11.8","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

Hashes for kola-2.3.1-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b5b76972a4c71cd3ea17c7c1f9427b97c4910089eef44a1d92b12a9800b32741
MD5 a7736347d91c7824cebaa2645946bd18
BLAKE2b-256 31ac1e7331e53c1e703d59e8533fbbcdb8c94e97fe0d9e3dd249ea3808af1680

See more details on using hashes here.

File details

Details for the file kola-2.3.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: kola-2.3.1-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.8 {"installer":{"name":"uv","version":"0.11.8","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

Hashes for kola-2.3.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 701eaa1c048ade91fc53e5ce419c1ea14cc6b4b2c3863260283beb5dd1843957
MD5 b84ec4642c9748e740c3ba1c492addd3
BLAKE2b-256 b62c52c34da5ff7ab049a6587549ed8f2dcfe3921d7bbe867b391117df3ebffa

See more details on using hashes here.

File details

Details for the file kola-2.3.1-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: kola-2.3.1-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.8 {"installer":{"name":"uv","version":"0.11.8","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

Hashes for kola-2.3.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6625dcc8ef4d323e270ba93e157b4fa614bb56831c3f3c9a0452fe42188e5f0a
MD5 ebf0d2d73782db02d5ca841d9f0cfdb2
BLAKE2b-256 5ef6238ef0df8e4e75a8d588664f287571806ce8f634e320d3757a84aafef4e6

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