Skip to content

Quickstart

Create a small dataset, analyze it with q from Python, and convert the result to a pandas DataFrame.

Estimated time: 10 minutes

Prerequisites

Before you start:

1. Import KDB-X Python

Import the pykx package using the conventional kx alias:

>>> import pykx as kx

The kx alias

Documentation examples use this optional alias to keep KDB-X Python code concise.

2. Create a table

Create a table of sample trades from native Python lists:

>>> trades = kx.Table(
...     data={
...         'symbol': ['AAPL', 'MSFT', 'AAPL', 'MSFT'],
...         'price': [189.0, 374.0, 191.0, 376.0],
...         'size': [100, 200, 150, 100],
...     }
... )
>>>
>>> print(trades)

KDB-X Python creates a typed pykx.Table and stores it in q memory:

symbol price size
-----------------
AAPL   189   100
MSFT   374   200
AAPL   191   150
MSFT   376   100

KDB-X Python converts the Python strings, floats, and integers into their corresponding q types. Keeping the data in q memory lets q functions operate on it without another conversion.

3. Inspect with Python indexing

KDB-X Python tables support familiar Python indexing. Use a slice to select the first two rows:

>>> print(trades[:2])
symbol price size
-----------------
AAPL   189   100
MSFT   374   200

Pass a list of column names to select specific columns:

>>> print(trades[['symbol', 'price']])
symbol price
------------
AAPL   189
MSFT   374
AAPL   191
MSFT   376

4. Analyze with q

Use kx.q to calculate the average price and total size for each symbol:

>>> summary = kx.q(
...     '{select average_price:avg price, total_size:sum size by symbol from x}',
...     trades,
... )
>>>
>>> print(summary)

The q function receives trades as its x argument and returns a keyed table:

symbol| average_price total_size
------| ------------------------
AAPL  | 190           250
MSFT  | 375           300

The embedded q runtime executes this query in the same Python process. To query a separate q process, use the IPC interface.

5. Convert to a pandas DataFrame

KDB-X Python results remain typed q objects until you choose to convert them. Call .pd() when a downstream Python workflow needs a pandas DataFrame:

>>> summary_df = summary.pd()
>>> print(summary_df)

The resulting DataFrame contains one row per symbol:

        average_price  total_size
symbol
AAPL            190.0         250
MSFT            375.0         300

Next steps

Continue with KDB-X Python Fundamentals for a guided tour of q types, conversions, table methods, q functions, and IPC.

Or go directly to a focused guide: