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.0.tar.gz (59.9 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.0-cp310-abi3-win_amd64.whl (2.5 MB view details)

Uploaded CPython 3.10+Windows x86-64

kola-2.3.0-cp310-abi3-manylinux_2_28_x86_64.whl (4.7 MB view details)

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

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

Uploaded CPython 3.10+macOS 11.0+ ARM64

kola-2.3.0-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.0.tar.gz.

File metadata

  • Download URL: kola-2.3.0.tar.gz
  • Upload date:
  • Size: 59.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for kola-2.3.0.tar.gz
Algorithm Hash digest
SHA256 4c93630235eeb620123571ea2ba0fb71ebfb4ceeff189b152ec41c089c0aa19e
MD5 22ee05409981699be3d24def0cf7fd6d
BLAKE2b-256 90159afd20ec9b89caf5dd786d338acdee44fc6aea8b5cd062946ff7dd9e5783

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kola-2.3.0-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: maturin/1.13.1

File hashes

Hashes for kola-2.3.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e276a1a88b1c4a2f99e00582753fc913c3646e0b92f8ddf6da7fd62607a48fcb
MD5 4ae4aef7fce57e1bf6a4d31a20de2f8e
BLAKE2b-256 009588b0fe0d87f4b8de4a561bc7d70ef807043131f178ae344452b6381cdafa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kola-2.3.0-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ba8e428415cebfe279610dc031f27f6bf61910d45735d2e4df1288066cc1d70b
MD5 216f1bfcbfacd702b6e3c5a765aef85d
BLAKE2b-256 e5f71c7edcac33ea0985764b4ad02734e759a9569b1429ecf2783ee6acdd492b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kola-2.3.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a83d265249dc5e0dbc8749da28784a62b43fdaa7cc2caebdfb8c4acd0c5a3fa1
MD5 e2bbe0266424c66d20e383d85d681fc2
BLAKE2b-256 8da4739ecce1854d367d56e0924b2c0cfe9831690abd92450675c736435c4354

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kola-2.3.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 feba81b73d4e8bcf160d05ee0ed325c58a55315541405e7abfcfffcc9dbc37b0
MD5 4834913322b11a8192449a1bc00ba35b
BLAKE2b-256 b7b14e7edf6f8f2551d48397c445ea09578dfd86ff3f6f6312ae0ae9021ecdce

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