PyKX type wrappers
pykx.wrappers
Wrappers for q data structures, with conversion functions to Python/Numpy/Pandas/Arrow.
Under PyKX, q has its own memory space in which it stores q data structures in the same way it is stored within a regular q process. PyKX provides Pythonic wrappers around these objects in q memory.
In general, these wrappers consist of a pointer to a location in q memory, and a collection of methods to operate on/with that data.
Memory in q is managed by reference counting, and so wrapper objects (i.e. pykx.K
objects) hold a reference to the underlying object in q memory, and release it when the Python
wrapper is deallocated.
A benefit of this approach to interacting with q data is that all conversions are deferred until explicitly performed via one of the conversion methods:
.py
for Python.np
for Numpy.pd
for Pandas.pa
for PyArrow
Example:
>>> import pykx as kx
>>> t = kx.q('([] x: 1 2 3; y: `a`b`c)') # Create a table in q memory
>>> x = t['x'] # Index into it in q
>>> x
pykx.LongVector(pykx.q('1 2 3'))
>>> x.np() # Convert the vector to Numpy
array([1, 2, 3])
>>> t.pd() # Convert the table to Pandas
x y
0 1 a
1 2 b
2 3 c
>>> t.pa() # Convert the table to PyArrow
pyarrow.Table
x: int64
y: string
----
x: [[1,2,3]]
y: [["a","b","c"]]
>>> t.py() # Convert the table to Python
{'x': [1, 2, 3], 'y': ['a', 'b', 'c']}
Conversions from q to Python avoid copying data where possible, but that is not always possible. Furthermore, conversions can result in loss of data about type information - not a loss of the data itself, but of the type information that informs how it will be interpreted.
For example, if we had a q second atom pykx.SecondAtom('04:08:16')
, and we converted it to Python
using its .py
method, we would get datetime.timedelta(seconds=14896)
. If we convert that back
to q using the pykx.K
constructor, we get pykx.TimespanAtom(pykx.q('0D04:08:16.000000000'))
.
We started with a pykx.SecondAtom
, but after converting to Python and then back to q, we get a
pykx.TimestampAtom
. This is because that is what datetime.timedelta
converts to by default, but
the net effect is that we lost type information about the q data.
This round-trip-lossiness can be prevented in 2 ways:
- If possible, avoid converting the
pykx.K
object to a Python/Numpy/Pandas/PyArrow type in the first place. No information can be lost during a conversion if no conversion occurs. It is frequently the case that no conversion actually needs to occur. As an example, instead of converting an entirepykx.Table
into a Pandas dataframe, only to use a few columns from it, one can index into thepykx.Table
directly to get the desired columns, thereby avoiding the conversion of the entire table. - Conversions from Python to q can be controlled by specifying the desired type. Using
pykx.K
as a constructor forces it to chose what q type the data should be converted to (using the same mechanism aspykx.toq
), but by using the class of the desired q type directly, e.g. [pykx.SecondAtom
][], one can override the defaults.
So to avoid the loss of type information from the previous example, we could run
pykx.SecondAtom(datetime.timedelta(seconds=14896))
instead of
pykx.K(datetime.timedelta(seconds=14896))
.
Wrapper type hierarchy
The classes in the diagram are all attributes of pykx
. They should be accessed as pykx.K
,
pykx.LongVector
, pykx.Table
, etc.
graph LR
K --> Atom
K --> Collection
Atom --> GUIDAtom
Atom --> NumericAtom
NumericAtom --> IntegralNumericAtom
IntegralNumericAtom --> BooleanAtom
IntegralNumericAtom --> ByteAtom
IntegralNumericAtom --> ShortAtom
IntegralNumericAtom --> IntAtom
IntegralNumericAtom --> LongAtom
NumericAtom --> NonIntegralNumericAtom
NonIntegralNumericAtom --> RealAtom
NonIntegralNumericAtom --> FloatAtom
Atom --> CharAtom
Atom --> SymbolAtom
Atom --> TemporalAtom
TemporalAtom --> TemporalFixedAtom
TemporalFixedAtom --> TimestampAtom
TemporalFixedAtom --> MonthAtom
TemporalFixedAtom --> DateAtom
TemporalFixedAtom --> DatetimeAtom
TemporalAtom --> TemporalSpanAtom
TemporalSpanAtom --> TimespanAtom
TemporalSpanAtom --> MinuteAtom
TemporalSpanAtom --> SecondAtom
TemporalSpanAtom --> TimeAtom
Atom --> EnumAtom
Atom --> Function
Function --> Lambda
Function --> UnaryPrimitive
UnaryPrimitive --> Identity
Identity --> ProjectionNull
Function --> Operator
Function --> Iterator
Function --> Projection
Function --> Composition
Function --> AppliedIterator
AppliedIterator --> Each
AppliedIterator --> Over
AppliedIterator --> Scan
AppliedIterator --> EachPrior
AppliedIterator --> EachRight
AppliedIterator --> EachLeft
Function --> Foreign
Collection --> Vector
Vector --> List
Vector --> GUIDVector
Vector --> NumericVector
NumericVector --> IntegralNumericVector
IntegralNumericVector --> BooleanVector
IntegralNumericVector --> ByteVector
IntegralNumericVector --> ShortVector
IntegralNumericVector --> IntVector
IntegralNumericVector --> LongVector
NumericVector --> NonIntegralNumericVector
NonIntegralNumericVector --> RealVector
NonIntegralNumericVector --> FloatVector
Vector --> CharVector
Vector --> SymbolVector
Vector --> TemporalVector
TemporalVector --> TemporalFixedVector
TemporalFixedVector --> TimestampVector
TemporalFixedVector --> MonthVector
TemporalFixedVector --> DateVector
TemporalFixedVector --> DatetimeVector
TemporalVector --> TemporalSpanVector
TemporalSpanVector --> TimespanVector
TemporalSpanVector --> MinuteVector
TemporalSpanVector --> SecondVector
TemporalSpanVector --> TimeVector
Vector --> EnumVector
Collection --> Mapping
Mapping --> Dictionary
Dictionary --> KeyedTable
Mapping --> Table
Table --> SplayedTable
SplayedTable --> PartitionedTable
K
K(x, *args, cast=None, **kwargs)
Base type for all q objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Any
|
An object that will be converted into a |
required |
Atom
Bases: K
See Also
[pykx.Collection
][]
Base type for all q atoms, including singular basic values, and functions.
EnumAtom
Bases: Atom
Wrapper for q enum atoms.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable |
The name of a list in q memory. |
required | |
index |
An index used in Enumeration. |
required | |
value |
An item that is used in Enumerate and Enum Extend. |
required | |
extend |
A boolean set to True to use Enum Extend and False to use Enumerate. |
required |
value
value()
Returns the value of the enumeration
domain
domain()
Returns the name of the domain of the enum
index
index()
Returns the index of the enum in the q list
TemporalAtom
Bases: Atom
Base type for all q temporal atoms.
TemporalSpanAtom
TemporalFixedAtom
TimeAtom
SecondAtom
MinuteAtom
TimespanAtom
DatetimeAtom
DatetimeAtom(*args, **kwargs)
Bases: TemporalFixedAtom
The q datetime type is deprecated.
PyKX does not provide a rich interface for the q datetime type, as it is deprecated. Avoid using it whenever possible.
Wrapper for q datetime atoms.
DateAtom
MonthAtom
TimestampAtom
SymbolAtom
Bases: Atom
Unique symbols are never deallocated!
Reserve symbol data for values that are recurring. Avoid using symbols for data being generated over time (e.g. random symbols) as memory usage will continually increase.
See Also
[pykx.CharVector
][]
Wrapper for q symbol atoms.
CharAtom
NumericAtom
NonIntegralNumericAtom
FloatAtom
RealAtom
IntegralNumericAtom
LongAtom
IntAtom
ShortAtom
ByteAtom
GUIDAtom
BooleanAtom
Collection
Bases: K
See Also
[pykx.Collection
][]
Base type for all q collections (i.e. non-atoms), including vectors, and mappings.
Vector
Bases: Collection
, abc.Sequence
Base type for all q vectors, which are ordered collections of a particular type.
append
append(data)
Append object to the end of a vector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
PyKX Vector/List object |
required | |
data |
Data to be used when appending to a list/vector, when appending to a typed list this must be an object with a type which converts to an equivalent vector type. When appending to a List any type object can be appended. |
required |
Raises:
Type | Description |
---|---|
PyKXException
|
When dealing with typed vectors appending to this vector with data of a different type is unsupported and will raise an error |
Examples:
Append to a vector object with an atom
>>> import pykx as kx
>>> qvec = kx.q.til(3)
>>> qvec
pykx.LongVector(pykx.q('0 1 2'))
>>> qvec.append(3)
>>> qvec
pykx.LongVector(pykx.q('0 1 2 3'))
Attempt to append a vector object with an incorrect type:
>>> import pykx as kx
>>> qvec = kx.q.til(3)
>>> qvec.append([1, 2, 3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda3/lib/python3.8/site-packages/pykx/wrappers.py", line 1262, ...
raise QError(f'Appending data of type: {type(data)} '
pykx.exceptions.QError: Appending data of type: <class 'pykx.wrappers.LongVector'> ...
Append to a list object with an atom and list:
>>> import pykx as kx
>>> qlist = kx.toq([1, 2.0])
>>> qlist
pykx.List(pykx.q('
1
2f
'))
>>> qlist.append('a')
>>> qlist
pykx.List(pykx.q('
1
2f
`a
'))
>>> qlist.append([1, 2])
>>> qlist
pykx.List(pykx.q('
1
2f
`a
1 2
'))
extend
extend(data)
Extend a vector by appending supplied values to the vector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
PyKX Vector/List object |
required | |
data |
Data to be used when extending the a list/vector, this can be data of any type which can be converted to a PyKX object. |
required |
Raises:
Type | Description |
---|---|
PyKXException
|
When dealing with typed vectors extending this vector with data of a different type is unsupported and will raise an error |
Examples:
Extend a vector object with an atom and list
>>> import pykx as kx
>>> qvec = kx.q.til(3)
>>> qvec
pykx.LongVector(pykx.q('0 1 2'))
>>> qvec.extend(3)
>>> qvec
pykx.LongVector(pykx.q('0 1 2 3'))
>>> qvec.extend([4, 5, 6])
>>> qvec
pykx.LongVector(pykx.q('0 1 2 3 4 5 6'))
Attempt to extend a vector object with an incorrect type:
>>> import pykx as kx
>>> qvec = kx.q.til(3)
>>> qvec.extend('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda3/lib/python3.8/site-packages/pykx/wrappers.py", line 1271, ...
raise QError(f'Extending data of type: {type(data)} '
pykx.exceptions.QError: Extending data of type: <class 'pykx.wrappers.SymbolAtom'> ...
Extend a list object with an atom and list:
>>> import pykx as kx
>>> qlist = kx.toq([1, 2.0])
>>> qlist
pykx.List(pykx.q('
1
2f
'))
>>> qlist.extend('a')
>>> qlist
pykx.List(pykx.q('
1
2f
`a
'))
>>> qlist.extend([1, 2])
>>> qlist
pykx.List(pykx.q('
1
2f
`a
1
2
'))
List
Bases: Vector
The memory layout of a q list is special.
All other vector types (see: subclasses of [pykx.Vector
][]) are structured in-memory as a
K object which contains metadata, followed immediately by the data in the vector. By
contrast, q lists are a a vector of pointers to K objects, so they are structured in-memory
as a K object containing metadata, followed immediately by pointers. As a result, the base
data "contained" by the list is located elsewhere in memory. This has performance and
ownership implications in q, which carry over to PyKX.
Wrapper for q lists, which are vectors of K objects of any type.
np
np(*, raw=False, has_nulls=None)
Provides a Numpy representation of the list.
NumericVector
Bases: Vector
Base type for all q numeric vectors.
IntegralNumericVector
BooleanVector
GUIDVector
ByteVector
ShortVector
IntVector
LongVector
NonIntegralNumericVector
RealVector
FloatVector
CharVector
SymbolVector
Bases: Vector
Unique symbols are never deallocated!
Reserve symbol data for values that are recurring. Avoid using symbols for data being generated over time (e.g. random symbols) as memory usage will continually increase.
Wrapper for q symbol vectors.
TemporalVector
TemporalFixedVector
Bases: TemporalVector
Base type for all q temporal vectors which represent a fixed date/time.
TemporalSpanVector
Bases: TemporalVector
Base type for all q temporal vectors which represent a span of time.
TimestampVector
MonthVector
DateVector
DatetimeVector
DatetimeVector(*args, **kwargs)
Bases: TemporalFixedVector
The q datetime type is deprecated.
PyKX does not provide a rich interface for the q datetime type, as it is depreceated. Avoid using it whenever possible.
Wrapper for q datetime vectors.
TimespanVector
MinuteVector
SecondVector
TimeVector
EnumVector
Bases: Vector
Wrapper for q enum vectors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable |
The handle of a list in q memory. |
required | |
indices |
An list used in Enumeration. |
required | |
values |
A list of items that is used in Enumerate and Enum Extend. |
required | |
extend |
A boolean set to True to use Enum Extend and False to use Enumerate. |
required |
values
values()
Returns the resolved value of the enumeration
domain
domain()
Returns the name of the domain of the enum
indices
indices()
Returns the indices of the enum in the q list
Anymap
Mapping
Mapping(*args, **kwargs)
Table
Table(*args, **kwargs)
Bases: PandasAPI
, Mapping
See Also
- [
pykx.SplayedTable
][] - [
pykx.PartitionedTable
][]
Wrapper for q tables, including in-memory tables, splayed tables, and partitioned tables.
Note: Despite the name, keyed tables are actually dictionaries.
insert
insert(row, match_schema=False, test_insert=False, inplace=True)
Helper function around q
's insert
function which inserts a row or multiple rows into
a q Table object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row |
Union[list, List]
|
A list of objects to be inserted as a row. |
required |
match_schema |
bool
|
Whether the row/rows to be inserted must match the tables current schema. |
False
|
test_insert |
bool
|
Causes the function to modify a small local copy of the table and return the modified example, this can only be used with embedded q and will not modify the source tables contents. |
False
|
inplace |
bool
|
Causes the underlying Table python object to update itself with the resulting Table after the insert. |
True
|
Returns:
Type | Description |
---|---|
The resulting table after the given row has been inserted. |
Raises:
Type | Description |
---|---|
PyKXException
|
If the |
Examples:
Insert a single row onto a Table, ensuring the new row matches the current tables schema.
>>> tab.insert([1, 2.0, datetime.datetime(2020, 2, 24)], match_schema=True)
upsert
upsert(row, match_schema=False, test_insert=False, inplace=True)
Helper function around q
's upsert
function which inserts a row or multiple rows into
a q Table object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row |
Union[list, List]
|
A list of objects to be inserted as a row. |
required |
match_schema |
bool
|
Whether the row/rows to be inserted must match the tables current schema. |
False
|
test_insert |
bool
|
Causes the function to modify a small local copy of the table and return the modified example, this can only be used with embedded q and will not modify the source tables contents. |
False
|
inplace |
bool
|
Causes the underlying Table python object to update itself with the resulting Table after the upsert. |
True
|
Returns:
Type | Description |
---|---|
The resulting table after the given row has been upserted. |
Raises:
Type | Description |
---|---|
PyKXException
|
If the |
Examples:
Upsert a single row onto a Table, ensuring the new row matches the current tables schema.
>>> tab.upsert([1, 2.0, datetime.datetime(2020, 2, 24)], match_schema=True)
sql
sql(query, *args)
Execute an SQL query against the supplied PyKX tabular object.
This function expects the table object to be supplied as a parameter to the query, additional parameters can be supplied as positional arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
A str object indicating the query to be executed, this must contain a required argument $1 associated with the table being queried. |
required | |
*args |
Any additional positional arguments required for query execution. |
()
|
Returns:
Type | Description |
---|---|
The queried table associated with the SQL statement |
Examples:
Query a simple table supplying no additional arguments
>>> tab = kx.Table(data = {'x': [1, 2, 3], 'x1': ['a', 'b', 'a']})
>>> tab.sql("select * from $1 where x1='a'")
pykx.Table(pykx.q('
x x1
----
1 a
3 a
'))
Query a simple table supplying multiple arguments
>>> tab = kx.Table(data = {'x': [1, 2, 3], 'x1': ['a', 'b', 'a']})
>>> tab.sql("select * from $1 where x1=$2 and x=$3", 'a', 1)
pykx.Table(pykx.q('
x x1
----
1 a
'))
select
select(columns=None, where=None, by=None, inplace=False)
Apply a q style select statement on the supplied table defined within the process.
This implementation follows the q functional select syntax with limitations on structures supported for the various clauses a result of this.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
A dictionary mapping the name to be given to a column and the logic to be applied in aggregation to that column both as strings. |
None
|
|
where |
Conditional filtering used to select subsets of the data on which by-clauses and appropriate aggregations are to be applied. |
None
|
|
by |
A dictionary mapping the names to be assigned to the produced columns and the columns whose results are used to construct the groups of the by clause. |
None
|
|
inplace |
Whether the result of an update is to be persisted. This operates for tables referenced by name in q memory or general table objects https://code.kx.com/q/basics/qsql/#result-and-side-effects. |
False
|
Examples:
Define a q table in python, and give it a name in q memory
>>> import pykx as kx
>>> qtab = kx.Table(data = {
... 'col1': kx.random.random(100, ['a', 'b', 'c']),
... 'col2': kx.random.random(100, 1.0),
... 'col3': kx.random.random(100, False),
... 'col4': kx.random.random(100, 10.0)})
Select all items in the table
>>> qtab.select()
Filter table based on various where conditions
>>> qtab.select(where='col2<0.5')
Retrieve statistics by grouping data on symbol columns
>>> qtab.select(columns={'maxCol2': 'max col2'}, by={'col1': 'col1'})
>>> qtab.select(columns={'avgCol2': 'avg col2', 'minCol4': 'min col4'}, by={'col1': 'col1'})
Retrieve grouped statistics with restrictive where condition
>>> qtab.select(columns={'avgCol2': 'avg col2', 'minCol4': 'min col4'}, by={'col1': 'col1'}, where='col3=0b')
exec
exec(columns=None, where=None, by=None)
Apply a q style exec statement on the supplied PyKX Table.
This implementation follows the q functional exec syntax with limitations on structures supported for the various clauses a result of this.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
A dictionary mapping the name to be given to a column and the logic to be applied in aggregation to that column both as strings. A string defining a single column to be retrieved from the table as a list. |
None
|
|
where |
Conditional filtering used to select subsets of the data on which by clauses and appropriate aggregations are to be applied. |
None
|
|
by |
A dictionary mapping the names to be assigned to the produced columns and the the columns whose results are used to construct the groups of the by clause. |
None
|
Examples:
Define a PyKX Table
>>> qtab = kx.Table(data = {
... 'col1': kx.random.random(100, ['a', 'b', 'c']),
... 'col2': kx.random.random(100, 1.0),
... 'col3': kx.random.random(100, False),
... 'col4': kx.random.random(100, 10.0)}
... )
Select last item of the table
qtab.exec()
Retrieve a column from the table as a list
qtab.exec('col3')
Retrieve a set of columns from a table as a dictionary
qtab.exec({'symcol': 'col1'})
qtab.exec({'symcol': 'col1', 'boolcol': 'col3'})
Filter columns from a table based on various where conditions
qtab.exec('col3', where='col1=`a')
qtab.exec({'symcol': 'col1', 'maxcol4': 'max col4'}, where=['col1=`a', 'col2<0.3'])
Retrieve data grouping by data on symbol columns
qtab.exec('col2', by={'col1': 'col1'})
qtab.exec(columns={'maxCol2': 'max col2'}, by={'col1': 'col1'})
qtab.exec(columns={'avgCol2': 'avg col2', 'minCol4': 'min col4'}, by={'col1': 'col1'})
Retrieve grouped statistics with restrictive where condition
qtab.exec(columns={'avgCol2': 'avg col2', 'minCol4': 'min col4'}, by={'col1': 'col1'}, where='col3=0b')
update
update(columns=None, where=None, by=None, inplace=False)
Apply a q style update statement on tables defined within the process.
This implementation follows the q functional update syntax with limitations on structures supported for the various clauses a result of this.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
A dictionary mapping the name of a column present in the table or one to be added to the contents which are to be added to the column, this content can be a string denoting q data or the equivalent Python data. |
None
|
|
where |
Conditional filtering used to select subsets of the data on which by-clauses and appropriate aggregations are to be applied. |
None
|
|
by |
A dictionary mapping the names to be assigned to the produced columns and the columns whose results are used to construct the groups of the by clause. |
None
|
|
inplace |
Whether the result of an update is to be persisted. This operates for tables referenced by name in q memory or general table objects https://code.kx.com/q/basics/qsql/#result-and-side-effects. |
False
|
Examples:
Define a q table in python and named in q memory
>>> qtab = kx.Table(data={
... 'name': ['tom', 'dick', 'harry'],
... 'age': [28, 29, 35],
... 'hair': ['fair', 'dark', 'fair'],
... 'eye': ['green', 'brown', 'gray']}
... )
Update all the contents of a column
qtab.update({'eye': '`blue`brown`green'})
Update the content of a column restricting scope using a where clause
qtab.update({'eye': ['blue']}, where='hair=`fair')
Define a q table suitable for by clause example
>>> bytab = kx.Table(data={
... 'name': kx.random.random(100, ['nut', 'bolt', 'screw']),
... 'color': kx.random.random(100, ['red', 'green', 'blue']),
... 'weight': 0.5 * kx.random.random(100, 20),
... 'city': kx.random.random(100, ['london', 'paris', 'rome'])})
Apply an update grouping based on a by phrase
bytab.update({'weight': 'avg weight'}, by={'city': 'city'})
Apply an update grouping based on a by phrase and persist the result using the inplace keyword
bytab.update(columns={'weight': 'avg weight'}, by={'city': 'city'}, inplace=True)
delete
delete(columns=None, where=None, inplace=False)
Apply a q style delete statement a PyKX table defined.
This implementation follows the q functional delete syntax with limitations on structures supported for the various clauses a result of this.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
Denotes the columns to be deleted from a table. |
None
|
|
where |
Conditional filtering used to select subsets of the data which are to be deleted from the table. |
None
|
|
inplace |
Whether the result of an update is to be persisted. This operates for tables referenced by name in q memory or general table objects https://code.kx.com/q/basics/qsql/#result-and-side-effects. |
False
|
Examples:
Define a PyKX Table against which to run the examples
>>> qtab = kx.Table(data = {
... 'name': ['tom', 'dick', 'harry'],
... 'age': [28, 29, 35],
... 'hair': ['fair', 'dark', 'fair'],
... 'eye': ['green', 'brown', 'gray']}
... )
Delete all the contents of the table
>>> qtab.delete()
Delete single and multiple columns from the table
>>> qtab.delete('age')
>>> qtab.delete(['age', 'eye'])
Delete rows of the dataset based on where condition
>>> qtab.delete(where='hair=`fair')
>>> qtab.delete(where=['hair=`fair', 'age=28'])
Delete a column from the dataset named in q memory and persist the result using the inplace keyword
>>> qtab.delete('age', inplace=True)
reorder_columns
reorder_columns(cols, inplace=False)
Reorder the columns of a supplied table, using a supplied list of columns. This list order the columns in the supplied order, if less than the total number of columns in the original table are supplied then the supplied columns will be first columns in the new table
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cols |
Union[List, str]
|
The column(s) which will be used to reorder the columns of the table |
required |
inplace |
bool
|
Whether the result of an update is to be persisted. This operates for tables referenced by name in q memory or general table objects https://code.kx.com/q/basics/qsql/#result-and-side-effects. |
False
|
Returns:
Type | Description |
---|---|
The resulting table after the columns have been rearranged. |
Examples:
Order a single column to be the first column in a table
>>> tab = kx.Table(data={
... 'a': [1, 2, 3],
... 'b': ['a', 'b', 'c'],
... 'c': [1.0, 2.0, 3.0]
... })
>>> tab.reorder_columns('c')
pykx.Table(pykx.q('
c a b
-----
1 1 a
2 2 b
3 3 c
'))
Reorder all columns within a table
>>> tab = kx.Table(data={
... 'a': [1, 2, 3],
... 'b': ['a', 'b', 'c'],
... 'c': [1.0, 2.0, 3.0]
... })
>>> tab.reorder_columns(['b', 'c', 'a'])
pykx.Table(pykx.q('
b c a
-----
a 1 1
b 2 2
c 3 3
'))
xbar
xbar(values)
Apply xbar
round down operations on the column(s) of a table to a specified
value
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values |
Provide a dictionary mapping the column to apply rounding to with
the rounding value as follows |
required |
Returns:
Type | Description |
---|---|
A table with rounding applied to the specified columns. |
>>> import pykx as kx
>>> N = 5
>>> kx.random.seed(42)
>>> tab = kx.Table(data = {
... 'x': kx.random.random(N, 100.0),
... 'y': kx.random.random(N, 10.0)})
>>> tab
pykx.Table(pykx.q('
x y
-----------------
77.42128 8.200469
70.49724 9.857311
52.12126 4.629496
99.96985 8.518719
1.196618 9.572477
'))
>>> tab.xbar({'x': 10})
pykx.Table(pykx.q('
x y
-----------
70 8.200469
70 9.857311
50 4.629496
90 8.518719
0 9.572477
'))
>>> tab.xbar({'x': 10, 'y': 2})
pykx.Table(pykx.q('
x y
----
70 8
70 8
50 4
90 8
0 8
'))
window_join
window_join(table, windows, cols, aggs)
Window joins provide the ability to analyse the behaviour of data in one table in the neighborhood of another.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table |
A |
required | |
windows |
A pair of lists containing times/timestamps denoting the beginning and end of the windows |
required | |
cols |
The names of the common columns |
required | |
aggs |
A dictionary mapping the name of a new derived column to a list
specifying the function to be applied as the first element and the columns
which should be passed from the |
required |
Returns:
Type | Description |
---|---|
For each record of the original table, a record with additional columns
denoted by the |
>>> trades = kx.Table(data={
... 'sym': ['ibm', 'ibm', 'ibm'],
... 'time': kx.q('10:01:01 10:01:04 10:01:08'),
... 'price': [100, 101, 105]})
>>> quotes = kx.Table(data={
... 'sym': 'ibm',
... 'time': kx.q('10:01:01+til 9'),
... 'ask': [101, 103, 103, 104, 104, 107, 108, 107, 108],
... 'bid': [98, 99, 102, 103, 103, 104, 106, 106, 107]})
>>> windows = kx.q('{-2 1+\:x}', trades['time'])
>>> trades.window_join(quotes,
... windows,
... ['sym', 'time'],
... {'ask_max': [lambda x: max(x), 'ask'],
... 'ask_minus_bid': [lambda x, y: x - y, 'ask', 'bid']})
pykx.Table(pykx.q('
sym time price ask_minus_bid ask_max
----------------------------------------
ibm 10:01:01 100 3 4 103
ibm 10:01:04 101 4 1 1 1 104
ibm 10:01:08 105 3 2 1 1 108
'))
PartitionedTable
PartitionedTable(*args, **kwargs)
Dictionary
Dictionary(*args, **kwargs)
KeyedTable
KeyedTable(*args, **kwargs)
Bases: Dictionary
, PandasAPI
Wrapper for q keyed tables, which are a kind of table-like dictionary.
Refer to chapter 8.4 of Q for Mortals for more information about q keyed tables.
insert
insert(row, match_schema=False, test_insert=False, inplace=True)
Helper function around q
's insert
function which inserts a row or multiple rows into
a q Table object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row |
Union[list, List]
|
A list of objects to be inserted as a row. |
required |
match_schema |
bool
|
Whether the row/rows to be inserted must match the tables current schema. |
False
|
test_insert |
bool
|
Causes the function to modify a small local copy of the table and return the modified example, this can only be used with embedded q and will not modify the source tables contents. |
False
|
inplace |
bool
|
Causes the underlying Table python object to update itself with the resulting Table after the insert. |
True
|
Returns:
Type | Description |
---|---|
The resulting table after the given row has been inserted. |
Raises:
Type | Description |
---|---|
PyKXException
|
If the |
Examples:
Insert a single row onto a Table, ensuring the new row matches the current tables schema.
>>> tab.insert([1, 2.0, datetime.datetime(2020, 2, 24)], match_schema=True)
upsert
upsert(row, match_schema=False, test_insert=False, inplace=True)
Helper function around q
's upsert
function which inserts a row or multiple rows into
a q Table object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row |
Union[list, List]
|
A list of objects to be inserted as a row, if the table is within embedded q you may also pass in a table object to be upserted. |
required |
match_schema |
bool
|
Whether the row/rows to be inserted must match the tables current schema. |
False
|
test_insert |
bool
|
Causes the function to modify a small local copy of the table and return the modified example, this can only be used with embedded q and will not modify the source tables contents. |
False
|
inplace |
bool
|
Causes the underlying Table python object to update itself with the resulting Table after the insert. |
True
|
Returns:
Type | Description |
---|---|
The resulting table after the given row has been upserted. |
Raises:
Type | Description |
---|---|
PyKXException
|
If the |
Examples:
Upsert a single row onto a Table, ensuring the new row matches the current tables schema.
>>> tab.upsert([1, 2.0, datetime.datetime(2020, 2, 24)], match_schema=True)
sql
sql(query, *args)
Execute an SQL query against the supplied PyKX KeyedTable object.
This function expects the keyed table object to be supplied as a parameter to the query, additional parameters can be supplied as positional arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
A str object indicating the query to be executed, this must contain a required argument $1 associated with the table being queried. |
required | |
*args |
Any additional positional arguments required for query execution. |
()
|
Returns:
Type | Description |
---|---|
The queried table associated with the SQL statement |
Examples:
Query a keyed table supplying no additional arguments
>>> tab = kx.Table(
... data = {'x': [1, 2, 3], 'x1': ['a', 'b', 'a']}
... ).set_index('x')
>>> tab.sql("select * from $1 where x1='a'")
pykx.Table(pykx.q('
x x1
----
1 a
3 a
'))
Query a keyed table supplying multiple arguments
>>> tab = kx.Table(
... data = {'x': [1, 2, 3], 'x1': ['a', 'b', 'a']}
... ).set_index('x')
>>> tab.sql("select * from $1 where x1=$2 and x=$3", 'a', 1)
pykx.Table(pykx.q('
x x1
----
1 a
'))
select
select(columns=None, where=None, by=None, inplace=False)
Apply a q style select statement on the supplied keyed table defined within the process.
This implementation follows the q functional select syntax with limitations on structures supported for the various clauses a result of this.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
A dictionary mapping the name to be given to a column and the logic to be applied in aggregation to that column both as strings. |
None
|
|
where |
Conditional filtering used to select subsets of the data on which by-clauses and appropriate aggregations are to be applied. |
None
|
|
by |
A dictionary mapping the names to be assigned to the produced columns and the columns whose results are used to construct the groups of the by clause. |
None
|
|
inplace |
Whether the result of an update is to be persisted. This operates for tables referenced by name in q memory or general table objects https://code.kx.com/q/basics/qsql/#result-and-side-effects. |
False
|
Examples:
Define a q table in python, and give it a name in q memory
>>> import pykx as kx
>>> qtab = kx.Table(data = {
... 'col1': kx.random.random(100, ['a', 'b', 'c']),
... 'col2': kx.random.random(100, 1.0),
... 'col3': kx.random.random(100, False),
... 'col4': kx.random.random(100, 10.0)}
... ).set_index('col1')
Select all items in the table
>>> qtab.select()
Filter table based on various where conditions
>>> qtab.select(where='col2<0.5')
Retrieve statistics by grouping data on symbol columns
>>> qtab.select(columns={'maxCol2': 'max col2'}, by={'col1': 'col1'})
>>> qtab.select(columns={'avgCol2': 'avg col2', 'minCol4': 'min col4'}, by={'col1': 'col1'})
Retrieve grouped statistics with restrictive where condition
>>> qtab.select(columns={'avgCol2': 'avg col2', 'minCol4': 'min col4'}, by={'col1': 'col1'}, where='col3=0b')
exec
exec(columns=None, where=None, by=None)
Apply a q style exec statement on the supplied PyKX KeyedTable.
This implementation follows the q functional exec syntax with limitations on structures supported for the various clauses a result of this.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
A dictionary mapping the name to be given to a column and the logic to be applied in aggregation to that column both as strings. A string defining a single column to be retrieved from the table as a list. |
None
|
|
where |
Conditional filtering used to select subsets of the data on which by clauses and appropriate aggregations are to be applied. |
None
|
|
by |
A dictionary mapping the names to be assigned to the produced columns and the the columns whose results are used to construct the groups of the by clause. |
None
|
Examples:
Define a PyKX KeyedTable
>>> qtab = kx.Table(data = {
... 'col1': kx.random.random(100, ['a', 'b', 'c']),
... 'col2': kx.random.random(100, 1.0),
... 'col3': kx.random.random(100, False),
... 'col4': kx.random.random(100, 10.0)}
... ).set_index('col1')
Select last item of the table
qtab.exec()
Retrieve a column from the table as a list
qtab.exec('col3')
Retrieve a set of columns from a table as a dictionary
qtab.exec({'symcol': 'col1'})
qtab.exec({'symcol': 'col1', 'boolcol': 'col3'})
Filter columns from a table based on various where conditions
qtab.exec('col3', where='col1=`a')
qtab.exec({'symcol': 'col1', 'maxcol4': 'max col4'}, where=['col1=`a', 'col2<0.3'])
Retrieve data grouping by data on symbol columns
qtab.exec('col2', by={'col1': 'col1'})
qtab.exec(columns={'maxCol2': 'max col2'}, by={'col1': 'col1'})
qtab.exec(columns={'avgCol2': 'avg col2', 'minCol4': 'min col4'}, by={'col1': 'col1'})
Retrieve grouped statistics with restrictive where condition
qtab.exec(columns={'avgCol2': 'avg col2', 'minCol4': 'min col4'}, by={'col1': 'col1'}, where='col3=0b')
update
update(columns=None, where=None, by=None, inplace=False)
Apply a q style update statement on a PyKX KeyedTable.
This implementation follows the q functional update syntax with limitations on structures supported for the various clauses a result of this.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
A dictionary mapping the name of a column present in the table or one to be added to the contents which are to be added to the column, this content can be a string denoting q data or the equivalent Python data. |
None
|
|
where |
Conditional filtering used to select subsets of the data on which by-clauses and appropriate aggregations are to be applied. |
None
|
|
by |
A dictionary mapping the names to be assigned to the produced columns and the columns whose results are used to construct the groups of the by clause. |
None
|
|
inplace |
Whether the result of an update is to be persisted. This operates for tables referenced by name in q memory or general table objects https://code.kx.com/q/basics/qsql/#result-and-side-effects. |
False
|
Examples:
Define a q table in python and named in q memory
>>> qtab = kx.Table(data={
... 'name': ['tom', 'dick', 'harry'],
... 'age': [28, 29, 35],
... 'hair': ['fair', 'dark', 'fair'],
... 'eye': ['green', 'brown', 'gray']}
... )
Update all the contents of a column
>>> qtab.update({'eye': '`blue`brown`green'})
Update the content of a column restricting scope using a where clause
>>> qtab.update({'eye': ['blue']}, where='hair=`fair')
Define a q table suitable for by clause example
>>> bytab = kx.Table(data={
... 'name': kx.random.random(100, ['nut', 'bolt', 'screw']),
... 'color': kx.random.random(100, ['red', 'green', 'blue']),
... 'weight': 0.5 * kx.random.random(100, 20),
... 'city': kx.random.random(100, ['london', 'paris', 'rome'])}
... ).set_index('city')
Apply an update grouping based on a by phrase
>>> bytab.update({'weight': 'avg weight'}, by={'city': 'city'})
Apply an update grouping based on a by phrase and persist the result using the inplace keyword
>>> bytab.update(columns={'weight': 'avg weight'}, by={'city': 'city'}, inplace=True)
delete
delete(columns=None, where=None, inplace=False)
Apply a q style delete statement a PyKX keyed table defined.
This implementation follows the q functional delete syntax with limitations on structures supported for the various clauses a result of this.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
Denotes the columns to be deleted from a table. |
None
|
|
where |
Conditional filtering used to select subsets of the data which are to be deleted from the table. |
None
|
|
inplace |
Whether the result of an update is to be persisted. This operates for tables referenced by name in q memory or general table objects https://code.kx.com/q/basics/qsql/#result-and-side-effects. |
False
|
Examples:
Define a PyKX Table against which to run the examples
>>> qtab = kx.Table(data = {
... 'name': ['tom', 'dick', 'harry'],
... 'age': [28, 29, 35],
... 'hair': ['fair', 'dark', 'fair'],
... 'eye': ['green', 'brown', 'gray']}
... ).set_index('name')
Delete all the contents of the table
>>> qtab.delete()
Delete single and multiple columns from the table
>>> qtab.delete('age')
>>> qtab.delete(['age', 'eye'])
Delete rows of the dataset based on where condition
>>> qtab.delete(where='hair=`fair')
>>> qtab.delete(where=['hair=`fair', 'age=28'])
Delete a column from the dataset named in q memory and persist the result using the inplace keyword
>>> qtab.delete('age', inplace=True)
Function
Bases: Atom
Base type for all q functions.
Function
objects can be called as if they were Python functions. All provided arguments will
be converted to q using [pykx.toq
][], and the execution of the function will happen in q.
...
can be used to omit an argument, resulting in a function projection.
Refer to chapter 6 of Q for Mortals for more information about q functions.
Lambda
Bases: Function
Wrapper for q lambda functions.
Lambda's are the most basic kind of function in q. They can take between 0 and 8 parameters
(inclusive), which all must be q objects themselves. If the provided parameters are not
[pykx.K
][] objects, they will be converted into them using [pykx.toq
][].
Unlike other [pykx.Function
][] subclasses, Lambda
objects can be called with keyword
arguments, using the names of the parameters from q.
UnaryPrimitive
Bases: Function
See Also
[pykx.Identity
][]
Wrapper for q unary primitive functions, including ::
, and other built-ins.
Unary primitives are a class of built-in q functions which take exactly one parameter. New ones cannot be defined by a user through any normal means.
Identity
Bases: UnaryPrimitive
Wrapper for the q identity function, also known as generic null (::
).
Most types in q have their own null value, but ::
is used as a generic/untyped null in
contexts where a non-null q object would otherwise be, e.g. as a null value for a
generic list, or as a null value in a dictionary.
::
is also the identity function. It takes a single q object as a parameter, which it returns
unchanged.
ProjectionNull
Bases: Identity
Projection nulls are unwieldy.
There are very few scenarios in which a typical user needs to work with a projection null directly, and doing so can be very error-prone. Instead of using them directly, operate on / work with projections, which use them implicitly.
Wrapper for the q projection null.
Projection null is a special q object which may initially seem to be
generic null (::
), but is actually a magic value used to create projections.
When a projection null is provided as an argument to a q function, the result is a function projection.
Operator
Operator(op)
Bases: Function
Wrapper for q operator functions.
Operators include @
, *
, +
, !
, :
, ^
, and many more. They are documented on the q
reference page: https://code.kx.com/q/ref/#operators
Iterator
Bases: Function
Wrappers for q iterator functions.
Iterators include the mapping iterators ('
, ':
, /:
, and \:
), and the accumulating
iterators (/
, and \
). They are documented on the q reference page:
https://code.kx.com/q/ref/#iterators
Projection
Bases: Function
Wrapper for q function projections.
Similar to [functools.partial
][], q functions can have some of their parameters fixed in
advance, resulting in a new function, which is a projection. When this projection is called,
the fixed parameters are no longer required, and cannot be provided.
If the original function had n
parameters, and it had m
of them provided, the result would
be a function (projection) that has m
parameters.
In PyKX, the special Python singleton ...
is used to represent
[projection null][pykx.ProjectionNull
]
params
params()
The param names from the base function that have not been set.
args
args()
The supplied arguments to the function being projected.
The arguments in the tuple are ordered as they were applied to the function, with projection nulls to fill the empty spaces before and in-between the supplied arguments. The tuple may either end with the last supplied argument, or have some trailing projection nulls depending on how the projection was created.
Examples:
>>> f = kx.q('{x+y+z}')
>>> f.args
()
>>> f(..., 2)
pykx.Projection(pykx.q('{x+y+z}[;2]'))
>>> f(..., 2).args
(pykx.ProjectionNull(pykx.q('::')), pykx.LongAtom(pykx.q('2')))
>>> f(..., 2, ...)
pykx.Projection(pykx.q('{x+y+z}[;2;]'))
>>> f(..., 2, ...).args
(pykx.ProjectionNull(pykx.q('::')), pykx.LongAtom(pykx.q('2')), pykx.ProjectionNull(pykx.q('::')))
func
func()
The original function with no fixed parameters.
With the original function, a new projection can be created, or it can simply be called with every parameter set.
Composition
Bases: Function
Wrapper for q function compositions.
Functions in q can be directly composed, as opposed to creating a new lambda function that applies a chain of functions. Direct composition of functions lends itself to a style which is referred to as "point-free" or "tacit" programming.
AppliedIterator
Bases: Function
Base type for all q functions that have had an iterator applied to them.
Iterators, also known as adverbs, are like Python decorators, in that they are functions which
take a function as their argument, and return a modified version of it. The iterators
themselves are of the type [pykx.Iterator
][], but when applied to a function a new type
(which is a subclass of AppliedIterator
) is created depending on what iterator was used.
Each
Over
Scan
EachPrior
EachRight
EachLeft
Foreign
Bases: Atom
Wrapper for foreign objects, i.e. wrapped pointers to regions outside of q memory.
py
py(stdlib=None, raw=None)
The resulting object is a reference to the same memory location as the initial object.
This can result in unexpected behavior and it is recommended to only modify the
original python object passed into the Foreign
Turns the pointer stored within the Foreign back into a Python Object.
SymbolicFunction
SymbolicFunction(*args, **kwargs)
Bases: Function
, SymbolAtom
Special wrapper type representing a symbol atom that can be used as a function.
sym
sym()
The symbolic function as a plain symbol.
func
func()
The symbolic function as a regular function, obtained by dereferencing the symbol.
with_execution_ctx
with_execution_ctx(execution_ctx)
Get a new symbolic function that will be evaluated within the provided q instance.
ParseTree
ParseTree(tree)
Special wrapper for a list which will be treated as a ParseTree. For use with the Query API
table
table(contents)
Helper function to create a ParseTree for the creation of a Table.
If a dict is passed creates: (flip;(!;contents.keys();enlist,contents.values()))
Else creates: (flip;(!;contents;enlist,contents))
For use with the Query API, particauly for fby
queries.
list
list(values)
Helper function to create a ParseTree for the creation of a List.
Creates: (enlist;value0;value1...valueN)
value
value(contents, eval=False)
Helper function to create a ParseTree which calls value
on it's contents.
Creates: (`.q.value;contents)
fby
fby(by, aggregate, data, by_table=False, data_table=False)
Helper function to create a ParseTree of an fby
call
Creates: (fby;(enlist;aggregate;data);by)
data_table
and by_table
can be set to True to create Table ParseTree of their input
Variable
Variable(name)
Helper class for passing Variable names through the Query API
Column
Column(column=None, name=None, value=None, is_tree=False)
Helper class creating queries for the Query API
abs
abs(iterator=None)
Return the absolute value of items in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the absolute value for all elements in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], 2, -1]
... })
>>> tab.select(kx.Column('a').abs())
pykx.Table(pykx.q('
a
-
1
1
0
'))
acos
acos(iterator=None)
Calculate arccos for a column or items in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the arccos value for all elements in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').acos())
pykx.Table(pykx.q('
a
--------
0
3.141593
1.570796
'))
Calculate the arccos value for each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').acos(iterator='each'))
pykx.Table(pykx.q('
b
------------
3.141593 0
1.570796 0
0
'))
asc
asc(iterator=None)
Sort the values within a column in ascending order
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Sort the values in a column ascending
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').asc())
pykx.Table(pykx.q('
a
--
-1
0
1
'))
Sort each row in a column ascending:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [3, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').asc(iterator='each'))
pykx.Table(pykx.q('
b
------
-1 1 2
1 2 3
1 2 3
'))
asin
asin(iterator=None)
Calculate arcsin for a column or items in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the arcsin value for all elements in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').asin())
pykx.Table(pykx.q('
a
---------
1.570796
-1.570796
0
'))
Calculate the arcsin value for each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').asin(iterator='each'))
pykx.Table(pykx.q('
b
---------------------------
-1.570796 1.570796
0 1.570796
1.570796
'))
atan
atan(iterator=None)
Calculate arctan for a column or items in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the arctan value for all elements in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').atan())
pykx.Table(pykx.q('
a
----------
0.7853982
-0.7853982
0
'))
Calculate the arctan value for each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').atan(iterator='each'))
pykx.Table(pykx.q('
b
------------------------------
-0.7853982 1.107149 0.7853982
0 0.7853982 1.107149
0.7853982 1.107149 1.249046
'))
avg
avg(iterator=None)
Calculate the average value for a column or items in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the value for all elements in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0.5],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').avg())
pykx.Table(pykx.q('
a
---------
0.1666667
'))
Calculate average value for each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').avg(iterator='each'))
pykx.Table(pykx.q('
b
---------
0.6666667
1
2
'))
avgs
avgs(iterator=None)
Calculate a running average value for a column or items in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the running average across all elements in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0.5],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').avgs())
pykx.Table(pykx.q('
a
---------
1
0
0.1666667
'))
Calculate average value for each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').avgs(iterator='each'))
pykx.Table(pykx.q('
b
----------------
-1 0.5 0.6666667
0 0.5 1
1 1.5 2
'))
ceiling
ceiling(iterator=None)
Calculate a nearest integer greater than or equal to items in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the ceiling of all elements in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [0.1, 0.4, 3.6],
... 'b': [[-1.1, 2.2, 1.6], [0.3, 1.4, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').ceiling())
pykx.Table(pykx.q('
a
-
1
1
4
'))
Calculate the ceiling for all values in each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [0.1, 0.4, 3.6],
... 'b': [[-1.1, 2.2, 1.6], [0.3, 1.4, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').ceiling(iterator='each'))
pykx.Table(pykx.q('
b
------
-1 3 2
1 2 2
1 2 3
'))
cor
cor(other, iterator=None, col_arg_ind=0, project_args=None)
Calculate the correlation between a column and one of:
- Another column
- A vector of equal length to the column
- A PyKX variable in q memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Calculate the correlation between two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.exec(kx.Column('a').cor(kx.Column('b')))
pykx.FloatAtom(pykx.q('-0.9946109'))
Calculate the correlation between a column and variable in q memory:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> kx.q('custom_var:100?1f')
>>> tab.exec(kx.Column('a').cor(kx.Variable('custom_var')))
pykx.FloatAtom(pykx.q('-0.1670133'))
Calculate the correlation between a column and a Python variable:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> kx.q('custom_var:100?1f')
>>> tab.exec(kx.Column('a').cor(kx.random.random(100, 10.0)))
pykx.FloatAtom(pykx.q('-0.01448725'))
cos
cos(iterator=None)
Calculate cosine for a column or items in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the cosine value for all elements in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').cos())
pykx.Table(pykx.q('
a
---------
0.5403023
0.5403023
1
'))
Calculate the cosine value for each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').cos(iterator='each'))
pykx.Table(pykx.q('
b
-------------------------------
0.5403023 -0.4161468 0.5403023
1 0.5403023 -0.4161468
0.5403023 -0.4161468 -0.9899925
'))
count
count(iterator=None)
Calculate the count of the number of elements in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the count of the number of elements in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.exec(kx.Column('a').count())
pykx.LongAtom(pykx.q('3'))
Count the number of elements in each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 2], 1]
... })
>>> tab.exec(kx.Column('b').count(iterator='each')))
pykx.LongVector(pykx.q('3 2 1'))
cov
cov(other, sample=False, iterator=None, col_arg_ind=0, project_args=None)
Calculate the covariance/sample covariance between a column and one of:
- Another column
- A vector of equal length to the column
- A PyKX variable in q memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
sample |
Should calculations of covariance return the sample covariance (set True) covariance (set False {default}) |
False
|
|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Calculate the covariance between two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.exec(kx.Column('a').cov(kx.Column('b')))
pykx.FloatAtom(pykx.q('-7.87451'))
Calculate the sample covariance between a column and variable in q memory:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> kx.q('custom_var:100?1f')
>>> tab.exec(kx.Column('a').cov(kx.Variable('custom_var'), sample=True))
pykx.FloatAtom(pykx.q('-0.1670133'))
Calculate the covariance between a column and a Python object:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.exec(kx.Column('a').cov(kx.random.random(100, 10.0)))
pykx.FloatAtom(pykx.q('-0.1093116'))
cross
cross(other, iterator=None, col_arg_ind=0, project_args=None)
Return the cross product (all possible combinations) between a column and:
- Another column
- A vector of items
- A PyKX variable in q memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Generate the cross product of all values in two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').cross(kx.Column('b')))
pykx.Table(pykx.q('
a
------------------
0.1392076 9.996082
0.1392076 9.797281
0.1392076 9.796094
..
'))
Calculate the cross product between a column and list in in q memory:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> kx.q('custom_var:til 3')
>>> tab.select(kx.Column('a').cross(kx.Variable('custom_var')))
pykx.Table(pykx.q('
a
-----------
0.1392076 0
0.1392076 1
0.1392076 2
0.2451336 0
..
'))
Calculate the cross product between a column and a Python object:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').cross([1, 2, 3]))
pykx.Table(pykx.q('
a
-----------
0.1392076 1
0.1392076 2
0.1392076 3
0.2451336 1
..
'))
deltas
deltas(iterator=None)
Calculate the difference between consecutive elements in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the difference between consecutive values in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.exec(kx.Column('a').deltas())
pykx.LongVector(pykx.q('1 -2 1'))
Calculate the difference between consecutive values in each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').deltas(iterator='each'))
pykx.Table(pykx.q('
b
-------
-1 3 -1
0 1 1
1 1 1
'))
desc
desc(iterator=None)
Sort the values within a column in descending order
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Sort the values in a column descending
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').desc())
pykx.Table(pykx.q('
a
--
1
0
-1
'))
Sort each row in a column descending:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [3, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').desc(iterator='each'))
pykx.Table(pykx.q('
b
------
2 1 -2
3 2 1
3 2 1
'))
dev
dev(sample=False, iterator=None)
Calculate the standard deviation or sample standard deviation for items in a column or rows in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample |
Should calculations of standard deviation return the square root of the sample variance (set True) or the square root of the variance (set False {default}) |
False
|
|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the standard deviation of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').dev())
pykx.FloatAtom(pykx.q('2.749494'))
Calculate the sample standard deviation for each row in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.select(kx.Column('b').dev(sample=True, iterator='each'))
pykx.Table(pykx.q('
b
---------
3.068428
3.832719
2.032402
2.553458
2.527216
1.497015
..
'))
differ
differ(iterator=None)
Find locations where items in a column or rows in a column change value from one item to the next
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Determine if consecutive rows in a column are different values
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 2),
... 'b': kx.random.random([100, 3], 2)
... })
>>> tab.select(kx.Column('a').differ())
pykx.Table(pykx.q('
a
-
1
1
0
1
..
'))
Determine if consecutive values in vectors within a row have different values
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 2),
... 'b': kx.random.random([100, 3], 2)
... })
>>> tab.select(kx.Column('b').differ(iterator='each'))
pykx.Table(pykx.q('
b
----
110b
101b
110b
110b
..
'))
distinct
distinct(iterator=None)
Find unique items in a column or rows in a column change value from one item to the next
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find all unique items in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').distinct())
pykx.LongVector(pykx.q('0 1 2 3 4'))
Find all unique items in each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').distinct(iterator='each'))
pykx.Table(pykx.q('
b
-----
0 2 3
1 3 4
4 2
1 4
1 0
,2
..
'))
div
div(other, iterator=None, col_arg_ind=0, project_args=None)
Return the greatest whole number divisor that does not exceed x%y between a column and:
- Another column
- An integer
- A vector of items equal in length to the column
- A PyKX variable in q memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Calculate the greatest whole number divisor between two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 5)),
... 'b': kx.q.desc(kx.random.random(100, 5))
... })
>>> tab.select(kx.Column('a').div(kx.Column('b')))
pykx.Table(pykx.q('
a
-
0
0
0
1
..
'))
Calculate the greatest whole number divisor between a column and an integer:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 5)),
... 'b': kx.q.desc(kx.random.random(100, 5))
... })
>>> tab.select(kx.Column('a').div(2))
pykx.Table(pykx.q('
a
-
1
0
2
0
..
'))
exp
exp(iterator=None)
Raise the expentional constant e
to a power determined by the elements of a column
or rows of the column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Raise the exponential constant e
to the power of values within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').exp())
pykx.FloatVector(pykx.q('1 2.718282 7.389056 20.08554..'))
Raise the exponential constant e
to the power of values within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').exp(iterator='each'))
pykx.Table(pykx.q('
b
--------------------------
1 7.389056 20.08554
2.718282 20.08554 54.59815
54.59815 7.389056 7.389056
2.718282 54.59815 2.718282
..
'))
fby
fby(by, aggregate, data, by_table=False, data_table=False)
Helper function to create an fby
inside a Column object
Creates: (fby;(enlist;aggregate;data);by)
data_table
and by_table
can be set to True to create Table ParseTree of their input
fills
fills(iterator=None)
Replace null values with the preceding non-null value within a column or vector within rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Replace nulls in a column with preceding null values
>>> import pykx as kx
>>> value_list = kx.q.til(2)
>>> value_list.append(kx.LongAtom.null)
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, value_list),
... 'b': kx.random.random([100, 3], value_list)
... })
>>> tab.select(kx.Column('a').fills())
pykx.Table(pykx.q('
a
-
0
0
1
1
..
'))
Replace null values within each row of a column
>>> import pykx as kx
>>> value_list = kx.q.til(2)
>>> value_list.append(kx.LongAtom.null)
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, value_list),
... 'b': kx.random.random([100, 3], value_list)
... })
>>> tab.select(kx.Column('b').fills(iterator='each'))
pykx.Table(pykx.q('
b
-----
1 1 0
0 1 1
1 1 1
1 1 1
0 1 1
..
'))
first
first(iterator=None)
Retrieve the first item of a column or first item of each row in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Retrieve the first element of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.exec(kx.Column('a').first())
pykx.LongAtom(pykx.q('1'))
Retrieve the first element of each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [3, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').first(iterator='each'))
pykx.Table(pykx.q('
b
--
-1
0
1
'))
floor
floor(iterator=None)
Calculate a nearest integer less than or equal to items in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the floor of all elements in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [0.1, 0.4, 3.6],
... 'b': [[-1.1, 2.2, 1.6], [0.3, 1.4, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').floor())
pykx.Table(pykx.q('
a
-
0
0
3
'))
Calculate the floor for all values in each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [0.1, 0.4, 3.6],
... 'b': [[-1.1, 2.2, 1.6], [0.3, 1.4, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').floor(iterator='each'))
pykx.Table(pykx.q('
b
------
-2 2 1
0 1 2
1 2 3
'))
null
null(iterator=None)
Determine if a value in a column or row of a column is a null value
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find null values within a column
>>> import pykx as kx
>>> value_list = kx.q.til(2)
>>> value_list.append(kx.LongAtom.null)
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, value_list),
... 'b': kx.random.random([100, 3], value_list)
... })
>>> tab.select(kx.Column('a').null())
pykx.Table(pykx.q('
a
-
1
0
0
0
1
..
'))
Calculate the floor for all values in each row of a specified column:
>>> import pykx as kx
>>> value_list = kx.q.til(2)
>>> value_list.append(kx.LongAtom.null)
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, value_list),
... 'b': kx.random.random([100, 3], value_list)
... })
>>> tab.select(kx.Column('b').null(iterator='each'))
pykx.Table(pykx.q('
b
----
000b
110b
011b
000b
..
'))
iasc
iasc(iterator=None)
Return the indexes needed to sort the values in a column/row in ascending order
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the indices needed to sort values in a column in ascending order
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.q.til(10)),
... 'b': kx.random.random([100, 3], kx.q.til(10))
... })
>>> tab.select(kx.Column('a').iasc())
pykx.Table(pykx.q('
a
--
19
25
30
40
50
..
'))
Find the indices needed to sort each row in a column in ascending order
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.q.til(10)),
... 'b': kx.random.random([100, 3], kx.q.til(10))
... })
>>> tab.select(kx.Column('b').iasc(iterator='each'))
pykx.Table(pykx.q('
b
----
2 0 1
1 0 2
0 1 2
0 1 2
..
'))
idesc
idesc(iterator=None)
Return the indexes needed to sort the values in a column/row in descending order
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the indices needed to sort values in a column in descending order
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.q.til(10)),
... 'b': kx.random.random([100, 3], kx.q.til(10))
... })
>>> tab.select(kx.Column('a').idesc())
pykx.Table(pykx.q('
a
--
39
43
45
56
60
..
'))
Find the indices needed to sort each row in a column in descending order
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.q.til(10)),
... 'b': kx.random.random([100, 3], kx.q.til(10))
... })
>>> tab.select(kx.Column('b').idesc(iterator='each'))
pykx.Table(pykx.q('
b
----
1 0 2
2 0 1
1 2 0
2 1 0
..
'))
inter
inter(other, iterator=None, col_arg_ind=0, project_args=None)
Return the intersection between a column and
- Another column
- A Python list/numpy array
- A PyKX variable in q memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Return the distinct intersection of values between two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 5)),
... 'b': kx.q.desc(kx.random.random(100, 10))
... })
>>> tab.exec(kx.Column('a').inter(kx.Column('b')).distinct())
pykx.LongVector(pykx.q('2 3 1 4 0'))
Return the distinct intersection of values between a column and variable in q memory:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 5)),
... 'b': kx.q.desc(kx.random.random(100, 10))
... })
>>> kx.q('custom_var:100?6')
>>> tab.exec(kx.Column('b').inter(kx.Variable('custom_var')).distinct())
pykx.LongVector(pykx.q('5 2 1 4 3 0'))
isin
isin(other, iterator=None, col_arg_ind=0, project_args=None)
Return a list of booleans indicating if the items in a column are in a specified
- Column
- Python list/numpy array
- A PyKX variable in q memory
Most commonly this function is used in where clauses to filter data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Query a table for anywhere where the column contains the element 'AAPL':
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, ['AAPL', 'GOOG', 'MSFT']),
... 'b': kx.random.random(100, 10)
... })
>>> tab.select(where=kx.Column('a').isin(['AAPL']))
pykx.Table(pykx.q('
a b
------
AAPL 7
AAPL 4
AAPL 2
..
'))
Return the distinct intersection of values between a column and variable in q memory:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, ['AAPL', 'GOOG', 'MSFT']),
... 'b': kx.random.random(100, 10)
... })
>>> kx.q('custom_var:1 2 3')
>>> tab.select(where=kx.Column('b').isin(kx.Variable('custom_var')))
pykx.Table(pykx.q('
a b
------
GOOG 2
MSFT 1
MSFT 2
GOOG 3
..
'))
last
last(iterator=None)
Retrieve the last item of a column or last item of each row in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Retrieve the last element of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.exec(kx.Column('a').last())
pykx.LongAtom(pykx.q('0'))
Retrieve the last element of each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [3, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').last(iterator='each'))
pykx.Table(pykx.q('
b
-
1
2
3
'))
like
like(other, iterator=None, col_arg_ind=0, project_args=None)
Return a list of booleans indicating whether an item in a column matches a supplied regex pattern. Most commonly this function is used in where clauses to filter data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
A string/byte array defining a regex pattern to be used for query |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Query a table for anywhere where the column contains the element 'AAPL':
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, ['TEST', 'tEsTing', 'string']),
... 'b': kx.random.random(100, 10)
... })
>>> tab.select(where=kx.Column('a').like('[tT]E*'))
pykx.Table(pykx.q('
a b
------
TEST 7
TEST 8
tEsTing 4
tEsTing 9
..
'))
log
log(iterator=None)
Calculate the natural logarithm of values in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the natural log of values within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').log())
pykx.FloatVector(pykx.q('0 1.386294 1.386294 1.098612..'))
Calculate the natural log of values within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').log(iterator='each'))
pykx.Table(pykx.q('
b
----------------------------
1.386294 -0w 0
-0w -0w 1.098612
1.098612 0 0
1.386294 1.386294 1.098612
..
'))
lower
lower(iterator=None)
Change the case of string/symbol objects within a column to be all lower case
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Lower all values within a symbol list
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': ['TeStiNG', 'lOwER', 'FuncTion'],
... 'b': [1, 2, 3]
... })
>>> tab.select(kx.Column('a').lower())
pykx.Table(pykx.q('
a
--------
testing
lower
function
'))
ltrim
ltrim(iterator=None)
Remove whitespace at the start of character vectors(strings) within items in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Remove leading whitespace from all values in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [b' test ', b' values ', b'trim '],
... 'b': [1, 2, 3]
... })
>>> tab.select(kx.Column('a').ltrim())
pykx.Table(pykx.q('
a
----------
"test "
"values "
"trim "
'))
mavg
mavg(other, iterator=None, col_arg_ind=1, project_args=None)
Calculate the simple moving average of items in a column for a specified window length. Any nulls after the first item are replaced by zero. The results are returned as a floating point.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the window to be used for calculation of the moving average |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Query a table for anywhere where the column contains the element 'AAPL':
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, ['TEST', 'tEsTing', 'string']),
... 'b': kx.random.random(100, 10)
... })
>>> tab.select(kx.Column('b').mavg(3))
pykx.Table(pykx.q('
b
--------
7
7.5
6.333333
5.333333
..
'))
max
max(iterator=None)
Find the maximum value in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the maximum values within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').max())
pykx.LongAtom(pykx.q('4'))
Find the maximum values within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').max(iterator='each'))
pykx.Table(pykx.q('
b
-
2
3
4
4
..
'))
maxs
maxs(iterator=None)
Find the running maximum value in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the running maximum values within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('a').maxs())
pykx.Table(pykx.q('
a
-
0
1
2
3
3
..
'))
Find the running maximum values within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').maxs(iterator='each'))
pykx.Table(pykx.q('
b
-----
2 2 2
3 3 3
4 4 4
0 3 4
..
'))
mcount
mcount(other, iterator=None, col_arg_ind=1, project_args=None)
Calculate the moving count of non-null items in a column for a specified window length. The first 'other' items of the result are the counts so far, thereafter the result is the moving average
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the window to be used for calculation of the moving count |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Calculate the moving count of non-null values within a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, [1, kx.LongAtom.null, 2]),
... 'b': kx.random.random(100, 10)
... })
>>> tab.select(kx.Column('a').mcount(3))
pykx.Table(pykx.q('
a
-
0
1
1
2
1
..
'))
md5
md5(iterator=None)
Apply MD5 hash algorithm on columns/rows within a column, it is suggested that this function should be used on rows rather than columns if being applied
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Apply the MD5 hash algorithm on each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [b' test ', b' values ', b'trim ']
... })
>>> tab.select(kx.Column('a').md5(iterator='each'))
pykx.Table(pykx.q('
a
----------------------------------
0x5609a772b21a22d88f3fb3d21f564eab
0xbb24d929a28559cc0aa65cb326d7662e
0xdeafa2fe0c90bcf8c722003bfdeb7c78
'))
mdev
mdev(other, iterator=None, col_arg_ind=1, project_args=None)
Calculate the moving standard deviation for items in a column over a specified window length. The first 'other' items of the result are the standard deviation of items so far, thereafter the result is the moving standard deviation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the window to be used for calculation of the moving standard deviation |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Calculate the moving standard deviation of values within a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, [1, kx.LongAtom.null, 2]),
... 'b': kx.random.random(100, 10)
... })
>>> tab.exec(kx.Column('b').mdev(3))
pykx.FloatVector(pykx.q('0 1.5 1.699673 1.247219..'))
med
med(iterator=None)
Find the median value in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the median value of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').med())
pykx.FloatAtom(pykx.q('2f'))
Find the median value for each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').med(iterator='each'))
pykx.Table(pykx.q('
b
-
3
2
3
3
..
'))
min
min(iterator=None)
Find the minimum value in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the minimum values within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').max())
pykx.LongAtom(pykx.q('0'))
Find the minimum values within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').min(iterator='each'))
pykx.Table(pykx.q('
b
-
0
0
2
0
..
'))
mins
mins(iterator=None)
Find the running minimum value in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the running minimum values within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('a').mins())
pykx.Table(pykx.q('
a
-
0
0
0
0
0
..
'))
Find the running minimum values within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').mins(iterator='each'))
pykx.Table(pykx.q('
b
-----
0 0 0
0 0 0
2 2 2
2 2 0
..
'))
mmax
mmax(other, iterator=None, col_arg_ind=1, project_args=None)
Calculate the moving maximum for items in a column over a specified window length. The first 'other' items of the result are the maximum of items so far, thereafter the result is the moving maximum
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the window to be used for calculation of the moving maximum |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Calculate the moving maximum of values within a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, [1, kx.LongAtom.null, 2]),
... 'b': kx.random.random(100, 10)
... })
>>> tab.exec(kx.Column('b').mmax(3))
pykx.LongVector(pykx.q('4 4 4 3 7..'))
mmin
mmin(other, iterator=None, col_arg_ind=1, project_args=None)
Calculate the moving minumum for items in a column over a specified window length. The first 'other' items of the result are the minimum of items so far, thereafter the result is the moving minimum
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the window to be used for calculation of the moving minimum |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Calculate the moving minimum of values within a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, [1, kx.LongAtom.null, 2]),
... 'b': kx.random.random(100, 10)
... })
>>> tab.exec(kx.Column('b').mmin(3))
pykx.LongVector(pykx.q('4 1 0 0 0 2..'))
mod
mod(other, iterator=None, col_arg_ind=0, project_args=None)
Calculate the modulus of items in a column for a given value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the divisor to be used when calculating the modulus |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Calculate the modulus for items within a column for a value 3:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, [1, kx.LongAtom.null, 2]),
... 'b': kx.random.random(100, 10)
... })
>>> tab.exec(kx.Column('b').mod(3))
pykx.LongVector(pykx.q('1 2 1 1 0..'))
msum
msum(other, iterator=None, col_arg_ind=1, project_args=None)
Calculate the moving sum of items in a column over a specified window length. The first 'other' items of the result are the sum of items so far, thereafter the result is the moving sum
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the window to be used for calculation of the moving sum |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Calculate the moving sum of values within a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, [1, kx.LongAtom.null, 2]),
... 'b': kx.random.random(100, 10)
... })
>>> tab.exec(kx.Column('b').msum(3))
pykx.LongVector(pykx.q('4 5 5 4 10 12..'))
neg
neg(iterator=None)
Compute the negative value for all items in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Compute the negative value for all items in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('a').neg())
pykx.Table(pykx.q('
a
--
0
-3
-4
-2
0
..
'))
prd
prd(iterator=None)
Calculate the product of all values in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the product of values within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5.0),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').prd())
pykx.FloatAtom(pykx.q('9.076436e+25'))
Find the product of values within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').prd(iterator='each'))
pykx.Table(pykx.q('
b
-
0
0
32
0
0
48
..
'))
prds
prds(iterator=None)
Find the running product of values in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the running product of values within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5.0),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('a').prds())
pykx.Table(pykx.q('
a
---------
0.8276359
3.833871
2.317464
3.940125
..
'))
Find the running product of values within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').prds(iterator='each'))
pykx.Table(pykx.q('
b
-------
0 0 0
0 0 0
2 8 32
2 6 0
0 0 0
3 12 48
..
'))
prev
prev(iterator=None)
Retrieve the immediately preceding item in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Shift the values in column 'a' within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5.0),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.update(kx.Column('a').prev())
pykx.Table(pykx.q('
a b
---------------
0 4 4
0.8276359 0 2 3
4.632315 2 4 4
0.6044712 2 3 0
'))
rank
rank(iterator=None)
Retrieve the positions items would take in a sorted list from a column
or items in a column, this is equivalent of calling iasc
twice`
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the rank of items in a list
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 1000),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.update(kx.Column('a').prev())
pykx.Table(pykx.q('
a b
--------
89 3 4 4
31 1 2 1
57 4 4 0
25 4 4 2
..
'))
ratios
ratios(iterator=None)
Calculate the ratio between consecutive elements in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the difference between consecutive values in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 1000),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').ratios())
pykx.FloatVector(pykx.q('908 0.3964758 1.45..'))
reciprocal
reciprocal(iterator=None)
Calculate the reciprocal of all elements in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the reciprocal of items in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 1000),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').reciprocal())
pykx.FloatVector(pykx.q('0.001101322 0.002777778 0.001915709..'))
reverse
reverse(iterator=None)
Reverse the elements of a column or contents of rows of the column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the reverse the items in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
>>> tab = kx.Table(data={
... 'a': kx.q.til(100),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').reverse())
pykx.LongVector(pykx.q('99 98 97..'))
rotate
rotate(other, iterator=None, col_arg_ind=1, project_args=None)
Shift the items in a column "left" or "right" by an integer amount denoted by the parameter other.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the number of elements left(positve) or right(negative) which the column list will be shifted |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Shift the items in column b by 2 left:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, ['TEST', 'tEsTing', 'string']),
... 'b': kx.random.random(100, 10)
... })
>>> tab.select(kx.Column('b') & kx.Column('b').rotate(2).name('rot_b'))
pykx.Table(pykx.q('
b rot_b
-------
7 4
8 4
4 6
4 9
6 9
9 2
..
'))
Round a column of times to 15 minute buckets
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.q('100?0t')),
... 'b': kx.random.random(100, 10)
... })
>>> tab.select(kx.Column('b').avg(), by=kx.Column('a').minute.xbar(15))
pykx.KeyedTable(pykx.q('
a | b
-----| --------
00:00| 5.666667
00:15| 3
00:45| 1
01:00| 4.5
'))
rtrim
rtrim(iterator=None)
Remove whitespace from the end of character vectors(strings) within items in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Remove trailing whitespace from all values in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [b' test ', b' values ', b'trim '],
... 'b': [1, 2, 3]
... })
>>> tab.select(kx.Column('a').ltrim())
pykx.Table(pykx.q('
a
---------
" test"
" values"
"trim"
'))
scov
scov(iterator=None)
Calculate the sample covariance for items in a column or rows in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the sample covariance of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').scov())
pykx.FloatAtom(pykx.q('8.983196'))
Calculate the sample covariance for each row in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.select(kx.Column('b').scov(iterator='each'))
pykx.Table(pykx.q('
b
---------
0.3333333
0.3333333
5.333333
1.333333
..
'))
sdev
sdev(iterator=None)
Calculate the sample deviation for items in a column or rows in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the sample deviation of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').sdev())
pykx.FloatAtom(pykx.q('8.983196'))
Calculate the sample deviation for each row in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.select(kx.Column('b').sdev(iterator='each'))
pykx.Table(pykx.q('
b
---------
0.3333333
0.3333333
5.333333
1.333333
..
'))
signum
signum(iterator=None)
Determine if the elements in a column or items in the row of a column is
- null or negative, returns -1i
- zero, returns 0i
- positive, returns 1i
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Determine if values are positive, null, zero or positive in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.exec(kx.Column('a').signum())
pykx.IntVector(pykx.q('1 -1 0i'))
Find if values are positive, null, zero or positive in each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').signum(iterator='each'))
pykx.Table(pykx.q('
b
------
-1 1 1
0 1 1
1 1 1
'))
sin
sin(iterator=None)
Calculate sine for a column or items in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the sine value for all elements in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').sin())
pykx.Table(pykx.q('
a
---------
0.841471
-0.841471
0
'))
Calculate the sine value for each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').sin(iterator='each'))
pykx.Table(pykx.q('
b
-----------------------------
-0.841471 0.9092974 0.841471
0 0.841471 0.9092974
0.841471 0.9092974 0.14112
'))
sqrt
sqrt(iterator=None)
Calculate the square root each element of a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the square root of each value within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5.0),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').sqrt())
pykx.FloatVector(pykx.q('1.152283 1.717071 1.253352..'))
Find the square root of each value within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5.0),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').sqrt(iterator='each'))
pykx.Table(pykx.q('
b
--------------------------
1.732051 1.414214 1.732051
2 1.732051 2
1.732051 1.732051 1.414214
0 0 1.414214
1.732051 1.414214 1.414214
..
'))
string
string(iterator=None)
Convert all elements of a column to a PyKX string (CharVector)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Convert all elements of a column to strings
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1234, 1.01, 12142],
... 'b': [1, 2, 3]
... })
>>> tab.select(kx.Column('a').string())
pykx.Table(pykx.q('
a
-------
"1234"
"1.01"
"12142"
'))
sum
sum(iterator=None)
Calculate the sum of all values in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the sum of values within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5.0),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').sum())
pykx.FloatAtom(pykx.q('249.3847'))
Find the sum of values within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5.0),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').sum(iterator='each'))
pykx.Table(pykx.q('
b
-
6
4
6
6
..
'))
sums
sums(iterator=None)
Find the running sum of values in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the running sum of values within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5.0),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('a').sums())
pykx.Table(pykx.q('
a
---------
4.396227
8.42457
8.87813
11.26718
..
'))
Find the running sum of values within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').sums(iterator='each'))
pykx.Table(pykx.q('
b
-------
0 3 6
3 4 4
1 3 6
3 3 6
..
'))
svar
svar(iterator=None)
Calculate the sample variance for items in a column or rows in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the sample variance of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').svar())
pykx.FloatAtom(pykx.q('8.394893'))
Calculate the sample variance for each row in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.select(kx.Column('b').svar(iterator='each'))
pykx.Table(pykx.q('
b
---------
6.023586
29.48778
6.318229
0.1609426
5.241295
..
'))
tan
tan(iterator=None)
Calculate tan for a column or items in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the tan value for all elements in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').tan())
pykx.Table(pykx.q('
a
---------
1.557408
-1.557408
0
'))
Calculate the tan value for each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').tan(iterator='each'))
pykx.Table(pykx.q('
b
-----------------------------
-1.557408 -2.18504 1.557408
0 1.557408 -2.18504
1.557408 -2.18504 -0.1425465
'))
trim
trim(iterator=None)
Remove whitespace from the start and end of character vectors(strings) within items in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Remove trailing and following whitespace from all values in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [b' test ', b' values ', b'trim '],
... 'b': [1, 2, 3]
... })
>>> tab.select(kx.Column('a').trim())
pykx.Table(pykx.q('
a
---------
"test"
"values"
"trim"
'))
union
union(other, iterator=None, col_arg_ind=0, project_args=None)
Return the union between a column and
- Another column
- A Python list/numpy array
- A PyKX variable in q memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Return the distinct union of values between two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 5)),
... 'b': kx.q.desc(kx.random.random(100, 10))
... })
>>> tab.exec(kx.Column('a').union(kx.Column('b')).distinct())
pykx.LongVector(pykx.q('0 1 2 3 4 9 8 7 6 5'))
Return the distinct union of values between a column and variable in q memory:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 5)),
... 'b': kx.q.desc(kx.random.random(100, 10))
... })
>>> kx.q('custom_var:100?6')
>>> tab.exec(kx.Column('b').union(kx.Variable('custom_var')).distinct())
pykx.LongVector(pykx.q('9 8 7 6 5 4 3 2 1 0'))
upper
upper(iterator=None)
Change the case of string/symbol objects within a column to be all upper case
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Convert all values within a symbol list to be upper case
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': ['TeStiNG', 'UpPer', 'FuncTion'],
... 'b': [1, 2, 3]
... })
>>> tab.select(kx.Column('a').upper())
pykx.Table(pykx.q('
a
--------
TESTING
UPPER
FUNCTION
'))
var
var(iterator=None, sample=False)
Calculate the variance or sample variance for items in a column or rows in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample |
Should calculation of variance return the sample variance (set True) or the variance (set False {default}) |
False
|
|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the variance of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').var())
pykx.FloatAtom(pykx.q('8.310944'))
Calculate the sample sample deviation for each row in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.select(kx.Column('b').var(sample=True, iterator='each'))
pykx.Table(pykx.q('
b
---------
6.023586
29.48778
6.318229
0.1609426
5.241295
..
'))
wavg
wavg(other, iterator=None, col_arg_ind=0, project_args=None)
Return the weighted average between a column and
- Another column
- A Python list/numpy array
- A PyKX variable in q memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Return the weighted average between two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 5)),
... 'b': kx.q.desc(kx.random.random(100, 10))
... })
>>> tab.exec(kx.Column('a').wavg(kx.Column('b')))
pykx.FloatAtom(pykx.q('2.456731'))
Return the weighted average between a column and variable in q memory:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 5)),
... 'b': kx.q.desc(kx.random.random(100, 10))
... })
>>> kx.q('custom_var:100?6')
>>> tab.exec(kx.Column('b').wavg(kx.Variable('custom_var')))
pykx.FloatAtom(pykx.q('2.431111'))
within
within(lower, upper, iterator=None, col_arg_ind=0, project_args=None)
Return a boolean list indicating whether the items of a column are within bounds of an lower and upper limite.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lower |
A sortable item defining the lower limit |
required | |
upper |
A sortable item defining the upper limit |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Return any rows where column a has a value within the range 1, 4:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.q.desc(kx.random.random(100, 10))
... })
>>> tab.select(where = kx.Column('a').within(1, 4))
pykx.Table(pykx.q('
a b
---
1 9
1 9
2 9
2 9
4 9
..
'))
Return any rows where column a has a value within a date range:
>>> import pykx as kx
>>> today = kx.DateAtom('today')
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, today - range(0, 10)),
... 'b': kx.q.desc(kx.random.random(100, 10))
... })
>>> tab.select(where=kx.Column('a').within(today - 5, today - 3))
pykx.FloatAtom(pykx.q('2.431111'))
wsum
wsum(other, iterator=None, col_arg_ind=0, project_args=None)
Return the weighted sum between a column and
- Another column
- A Python list/numpy array
- A PyKX variable in q memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Return the weighted sum between two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 5)),
... 'b': kx.q.desc(kx.random.random(100, 10))
... })
>>> tab.exec(kx.Column('a').wsum(kx.Column('b')))
pykx.FloatAtom(pykx.q('511f'))
Return the weighted sum between a column and variable in q memory:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 5)),
... 'b': kx.q.desc(kx.random.random(100, 10))
... })
>>> kx.q('custom_var:100?6')
>>> tab.exec(kx.Column('b').wsum(kx.Variable('custom_var')))
pykx.FloatAtom(pykx.q('1094f'))
xbar
xbar(other, iterator=None, col_arg_ind=1, project_args=None)
Round the elements of a column down to the nearest multiple of the supplied parameter other.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the multiple to which all values will be rounded |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Round the items of a column to multiples of 3:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, ['TEST', 'tEsTing', 'string']),
... 'b': kx.random.random(100, 10)
... })
>>> tab.select(kx.Column('b').xbar(3))
pykx.Table(pykx.q('
b
-
3
6
9
6
..
'))
Round a column of times to 15 minute buckets
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.q('100?0t')),
... 'b': kx.random.random(100, 10)
... })
>>> tab.select(kx.Column('b').avg(), by=kx.Column('a').minute.xbar(15))
pykx.KeyedTable(pykx.q('
a | b
-----| --------
00:00| 5.666667
00:15| 3
00:45| 1
01:00| 4.5
'))
xexp
xexp(other, iterator=None, col_arg_ind=1, project_args=None)
Raise the elements of a column down to power of the value supplied as the parameter other.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the power to which all values will be raised |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Round the items of a column to multiples of 3:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random(100, 10)
... })
>>> tab.select(kx.Column('b').xexp(2))
pykx.Table(pykx.q('
b
---
64
512
4
8
2
..
'))
xlog
xlog(other, iterator=None, col_arg_ind=1, project_args=None)
Return the base-N logarithm for the elements of a column where N is specified by the parameter other.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the logarithmic base to which all values will be set |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Round the items of a column to multiples of 3:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random(100, 10)
... })
>>> tab.select(kx.Column('b').xlog(2))
pykx.Table(pykx.q('
b
--------
1.584963
3.169925
2.321928
3.169925
..
'))
xprev
xprev(other, iterator=None, col_arg_ind=1, project_args=None)
For a specified column return for each item in the column the item N elements before it. Where N is specified by the parameter other.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the number of indices before elements in the list to retrieve the value of |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Shift the data in a column by 3 indexes:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random(100, 10)
... })
>>> tab.select(kx.Column('a') & kx.Column('a').xprev(3).name('lag_3_a'))
pykx.Table(pykx.q('
a lag_3_a
-------------------
3.927524
5.170911
5.159796
4.066642 3.927524
1.780839 5.170911
3.017723 5.159796
'))
hour
hour()
Retrieve the hour information from a temporal column
Examples:
Retrieve hour information from a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.TimestampAtom.inf),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').hour)
pykx.IntVector(pykx.q('11 1 13 12..'))
minute
minute()
Retrieve the minute information from a temporal column
Examples:
Retrieve minute information from a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.TimestampAtom.inf),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').minute)
pykx.MinuteVector(pykx.q('11:55 01:09 13:43..'))
date
date()
Retrieve the date information from a temporal column
Examples:
Retrieve date information from a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.TimestampAtom.inf),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').date)
pykx.DateVector(pykx.q('2122.07.05 2120.10.23..'))
year
year()
Retrieve the year information from a temporal column
Examples:
Retrieve year information from a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.TimestampAtom.inf),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').year)
pykx.IntVector(pykx.q('2122 2120 2185..'))
day
day()
Retrieve the day of the month information from a temporal column
Examples:
Retrieve day of month information from a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.TimestampAtom.inf),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').day)
pykx.IntVector(pykx.q('7 10 12..'))
month
month()
Retrieve the month information from a temporal column
Examples:
Retrieve month information from a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.TimestampAtom.inf),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').month)
pykx.IntVector(pykx.q('7 10 12..'))
second
second()
Retrieve the second information from a temporal column
Examples:
Retrieve year information from a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.TimestampAtom.inf),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').second)
pykx.SecondVector(pykx.q('11:55:50 01:09:35..'))
add
add(other, iterator=None, col_arg_ind=0, project_args=None)
Add the content of a column to one of
- Another column
- A vector of equal length to the column
- A PyKX variable in q memory
Note in it's most basic usage this is equivalent to
>>> kx.Column('x') + kx.Column('y')
It is supplied as a named function to allow the use of iterators when adding elements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Add together two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').add(kx.Column('b')))
pykx.Table(pykx.q('
a
--------
9.967087
9.870729
9.882342
9.95924
..
'))
Add a value of 3 to each element of a column.
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').add(3))
pykx.Table(pykx.q('
a
--------
3.021845
3.044166
3.062797
3.051352
..
'))
For each row in a column add 3 and 4 to the value of the column This makes use of each-left and each-right from q:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').add([3, 4], iterator='/:\:'))
pykx.Table(pykx.q('
a
-----------------
3.021845 4.021845
3.044166 4.044166
3.062797 4.062797
3.166843 4.166843
..
'))
name
name(name)
Rename the resulting column from a calculation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
The name to be given to the column following application of function |
required |
Examples:
Rename the column 'a' to 'average_a' following application of the function average
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0.5],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').average().name('average_a'))
pykx.Table(pykx.q('
average_a
---------
0.1666667
'))
average
average(iterator=None)
Calculate the average value for a column or items in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the value for all elements in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0.5],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').average())
pykx.Table(pykx.q('
a
---------
0.1666667
'))
Calculate average value for each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('b').average(iterator='each'))
pykx.Table(pykx.q('
b
---------
0.6666667
1
2
'))
cast
cast(other, iterator=None, col_arg_ind=1, project_args=None)
Convert the content of a column to another PyKX type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The name of the type to which your column should be cast or the lower case letter used to define it in q, for more information see https://code.kx.com/q/ref/cast/ |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Cast a column containing PyKX long objects to float objects
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.q.til(10)),
... 'b': kx.random.random(100, kx.q.til(10))
... })
>>> tab.dtypes
pykx.Table(pykx.q('
columns datatypes type
-----------------------------------
a "kx.LongAtom" "kx.LongAtom"
b "kx.LongAtom" "kx.LongAtom"
'))
>>> tab.select(
... kx.Column('a') &
... kx.Column('a').cast('float').name('a_float') &
... kx.Column('b')).dtypes
pykx.Table(pykx.q('
columns datatypes type
-------------------------------------
a "kx.LongAtom" "kx.LongAtom"
a_float "kx.FloatAtom" "kx.FloatAtom"
b "kx.LongAtom" "kx.LongAtom"
'))
correlation
correlation(other, iterator=None, col_arg_ind=0, project_args=None)
Calculate the correlation between a column and one of
- Another column
- A vector of equal length to the column
- A PyKX variable in q memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Calculate the correlation between two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.exec(kx.Column('a').cor(kx.Column('b')))
pykx.FloatAtom(pykx.q('-0.9946109'))
Calculate the correlation between a column and variable in q memory:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> kx.q('custom_var:100?1f')
>>> tab.exec(kx.Column('a').correlation(kx.Variable('custom_var')))
pykx.FloatAtom(pykx.q('-0.1670133'))
Calculate the correlation between a column and a Python variable:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> kx.q('custom_var:100?1f')
>>> tab.exec(kx.Column('a').correlation(kx.random.random(100, 10.0)))
pykx.FloatAtom(pykx.q('-0.01448725'))
covariance
covariance(
other, sample=False, iterator=None, col_arg_ind=0, project_args=None
)
Calculate the covariance/sample covariance between a column and one of:
- Another column
- A vector of equal length to the column
- A PyKX variable in q memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
sample |
Should calculations of covariance return the sample covariance (set True) covariance (set False {default}) |
False
|
|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Calculate the covariance between two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.exec(kx.Column('a').covariance(kx.Column('b')))
pykx.FloatAtom(pykx.q('-7.87451'))
Calculate the sample covariance between a column and variable in q memory:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> kx.q('custom_var:100?1f')
>>> tab.exec(kx.Column('a').covariance(kx.Variable('custom_var'), sample=True))
pykx.FloatAtom(pykx.q('-0.1670133'))
Calculate the covariance between a column and a Python object:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.exec(kx.Column('a').covariance(kx.random.random(100, 10.0)))
pykx.FloatAtom(pykx.q('-0.1093116'))
divide
divide(other, iterator=None, col_arg_ind=0, project_args=None)
Divide the content of one column by
- Another column
- Python/Numpy list/item
- A PyKX variable in q memory
Note in it's most basic usage this is equivalent to
>>> kx.Column('x') % kx.Column('y')
It is supplied as a named function to allow the use of iterators when adding elements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Divide on column by another column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').divide(kx.Column('b')))
pykx.Table(pykx.q('
a
-----------
0.0021965
0.004494546
0.006395103
0.01703797
..
'))
Divide each element of a column by 3.
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').divide(3))
pykx.Table(pykx.q('
a
-----------
0.007281574
0.01472198
0.02093233
0.05561419
..
'))
For each row in a column divide the row by both 3 and 4 independently. This makes use of each-left and each-right from q:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').divide([3, 4], iterator='/:\:'))
pykx.Table(pykx.q('
a
---------------------
0.06553417 0.08737889
0.1324978 0.1766638
0.188391 0.251188
0.5005277 0.6673703
..
'))
drop
drop(other, iterator=None, col_arg_ind=1, project_args=None)
Drop N rows from a column or N elements from items in a column using an iterator. Where N is specified by the other parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer defining the number of elements to drop |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Drop 3 rows from a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.q.til(10)),
... 'b': kx.random.random([100, 3], kx.q.til(10))
... })
>>> tab.select(kx.Column('a').drop(3).count())
pykx.Table(pykx.q('
a
--
10
12
24
27
..
'))
fill
fill(other, iterator=None, col_arg_ind=1, project_args=None)
Replace all null values in a column with a specified 'other' parameter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The value which should replace nulls within a column |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Replace all nulls in column a with a value 0, displaying that only 0, 1 and 2 exist in this column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, [1, kx.LongAtom.null, 2]),
... 'b': kx.random.random(100, 10)
... })
>>> tab.exec(kx.Column('a').fill(0).distinct())
pykx.LongVector(pykx.q('1 0 2'))
index_sort
index_sort(ascend=True, iterator=None)
Return the indexes needed to sort the values in a column/row in ascending order or descending order
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ascend |
A boolean indicating if the index return should be retrieved in ascending or descending order |
True
|
|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the indices needed to sort values in a column in ascending order
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.q.til(10)),
... 'b': kx.random.random([100, 3], kx.q.til(10))
... })
>>> tab.select(kx.Column('a').index_sort())
pykx.Table(pykx.q('
a
--
10
12
24
27
..
'))
Find the indices needed to sort values in a column in descending order
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.q.til(10)),
... 'b': kx.random.random([100, 3], kx.q.til(10))
... })
>>> tab.select(kx.Column('a').index_sort(ascend=False))
pykx.Table(pykx.q('
a
--
1
15
44
50
..
'))
join
join(other, iterator=None, col_arg_ind=0, project_args=None)
Join the content of one column with another or using an iterator produce complex combinations of items in a column with:
- Another Column
- A list/item which is to be joined to the column
- A variable in q memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The Column, list, item or variable to be joined to the original column |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Join the content of one column to another column (extend the column)
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> len(tab.select(kx.Column('a').join(kx.Column('b'))))
200
Join the value 3 to each row in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').join(3, iterator="'"))
pykx.Table(pykx.q('
a
---
1 3
2 3
1 3
3 3
..
'))
len
len(iterator=None)
Calculate the length of the number of elements in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the length of the number of elements in a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.exec(kx.Column('a').len())
pykx.LongAtom(pykx.q('3'))
Count the length of elements in each row of a specified column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 2], 1]
... })
>>> tab.exec(kx.Column('b').len(iterator='each')))
pykx.LongVector(pykx.q('3 3 3'))
modulus
modulus(other, iterator=None, col_arg_ind=1, project_args=None)
Calculate the modulus of items in a column for a given value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer denoting the divisor to be used when calculating the modulus |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 1. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Calculate the modulus for items within a column for a value 3:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, [1, kx.LongAtom.null, 2]),
... 'b': kx.random.random(100, 10)
... })
>>> tab.exec(kx.Column('b').mod(3))
pykx.LongVector(pykx.q('1 2 1 1 0..'))
multiply
multiply(other, iterator=None, col_arg_ind=0, project_args=None)
Multiply the content of a column to one of
- Another column
- Python/Numpy list
- A PyKX variable in q memory
Note in it's most basic usage this is equivalent to
>>> kx.Column('x') * kx.Column('y')
It is supplied as a named function to allow the use of iterators when adding elements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Multiply together two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').multiply(kx.Column('b')))
pykx.Table(pykx.q('
a
---------
0.2172511
0.4339994
0.616638
1.633789
..
'))
Multiply each element of a column by 3.
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').multiply(3))
pykx.Table(pykx.q('
a
----------
0.06553417
0.1324978
0.188391
0.5005277
..
'))
For each row in a column multiply the row by both 3 and 4 independently. This makes use of each-left and each-right from q:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').multiply([3, 4], iterator='/:\:'))
pykx.Table(pykx.q('
a
---------------------
0.06553417 0.08737889
0.1324978 0.1766638
0.188391 0.251188
0.5005277 0.6673703
..
'))
next_item
next_item(iterator=None)
Retrieve the immediately following item in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Shift the values in column 'a' within a column forward one
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5.0),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.update(kx.Column('a').next_item())
pykx.Table(pykx.q('
a b
---------------
0 4 4
0.8276359 0 2 3
4.632315 2 4 4
0.6044712 2 3 0
'))
previous_item
previous_item(iterator=None)
Retrieve the immediately preceding item in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Shift the values in column 'a' within a column back one
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5.0),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.update(kx.Column('a').previous_item())
pykx.Table(pykx.q('
a b
---------------
0 4 4
0.8276359 0 2 3
4.632315 2 4 4
0.6044712 2 3 0
'))
product
product(iterator=None)
Calculate the product of all values in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the product of values within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5.0),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.exec(kx.Column('a').product())
pykx.FloatAtom(pykx.q('9.076436e+25'))
Find the product of values within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').product(iterator='each'))
pykx.Table(pykx.q('
b
-
0
0
32
0
0
48
..
'))
products
products(iterator=None)
Find the running product of values in a column or rows of a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Find the running product of values within a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5.0),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('a').products())
pykx.Table(pykx.q('
a
---------
0.8276359
3.833871
2.317464
3.940125
..
'))
Find the running product of values within each row of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 5),
... 'b': kx.random.random([100, 3], 5)
... })
>>> tab.select(kx.Column('b').products(iterator='each'))
pykx.Table(pykx.q('
b
-------
0 0 0
0 0 0
2 8 32
2 6 0
0 0 0
3 12 48
..
'))
sort
sort(ascend=True, iterator=None)
Sort the values within a column in ascending or descending order
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ascend |
Should the data be sorted in ascending or descending order |
True
|
|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Sort the values in a column ascending
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').sort())
pykx.Table(pykx.q('
a
--
-1
0
1
'))
Sort the values in descending order:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': [1, -1, 0],
... 'b': [[-1, 2, 1], [0, 1, 2], [1, 2, 3]]
... })
>>> tab.select(kx.Column('a').sort(ascend=False))
pykx.Table(pykx.q('
a
--
1
0
-1
'))
subtract
subtract(other, iterator=None, col_arg_ind=0, project_args=None)
Subtract from a column one of
- The values of another column
- Python/Numpy list/value
- A PyKX variable in q memory
Note in it's most basic usage this is equivalent to
>>> kx.Column('x') - kx.Column('y')
It is supplied as a named function to allow the use of iterators when adding elements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
The second column or variable (Python/q) to be used |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
0
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Subtract the values of two columns:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').subtract(kx.Column('b')))
pykx.Table(pykx.q('
a
---------
-9.923397
-9.782397
-9.756748
-9.625555
..
'))
Substract 3 from each element of a column.
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').subtract(3))
pykx.Table(pykx.q('
a
---------
-2.978155
-2.955834
-2.937203
-2.833157
..
'))
For each row in a column subtract 3 and 4 from the row independently. This makes use of each-left and each-right from q:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.q.asc(kx.random.random(100, 10.0)),
... 'b': kx.q.desc(kx.random.random(100, 10.0))
... })
>>> tab.select(kx.Column('a').subtract([3, 4], iterator='/:\:'))
pykx.Table(pykx.q('
a
-------------------
-2.978155 -3.978155
-2.955834 -3.955834
-2.937203 -3.937203
-2.833157 -3.833157
..
'))
take
take(other, iterator=None, col_arg_ind=1, project_args=None)
Retrieve the first N rows from a column or N elements from items from a column using an iterator. Where N is specified by the other parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
An integer defining the number of elements to retrieve |
required | |
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
|
col_arg_ind |
Determines the index within the multivariate function where the column parameter will be used. Default 0. |
1
|
|
project_args |
The argument indices of a multivariate function which will be projected on the function before evocation with use of an iterator. |
None
|
Examples:
Retrieve 3 rows from a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, kx.q.til(10)),
... 'b': kx.random.random([100, 3], kx.q.til(10))
... })
>>> tab.select(kx.Column('a').take(3).count())
pykx.Table(pykx.q('
a
--
10
12
24
'))
value
value(iterator=None)
When passed an EnumVector will return the corresponding SymbolVector
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the variance of a column
>>> import pykx as kx
>>> tab = kx.q('([] a:`sym?`a`b`c`a)')
>>> tab.exec(kx.Column('a'))
pykx.EnumVector(pykx.q('`sym$`a`b`c`a'))
>>> tab.exec(kx.Column('a').value())
pykx.SymbolVector(pykx.q('`a`b`c`a'))
variance
variance(sample=False, iterator=None)
Calculate the variance or sample variance for items in a column or rows in a column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample |
Should calculation of variance return the sample variance (set True) or the variance (set False {default}) |
False
|
|
iterator |
What iterator to use when operating on the column
for example, to execute per row, use |
None
|
Examples:
Calculate the variance of a column
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.exec(kx.Column('a').variance())
pykx.FloatAtom(pykx.q('8.310944'))
Calculate the sample sample deviation for each row in a column:
>>> import pykx as kx
>>> tab = kx.Table(data={
... 'a': kx.random.random(100, 10.0),
... 'b': kx.random.random([100, 3], 10.0)
... })
>>> tab.select(kx.Column('b').variance(sample=True, iterator='each'))
pykx.Table(pykx.q('
b
---------
6.023586
29.48778
6.318229
0.1609426
5.241295
..
'))
QueryPhrase
QueryPhrase(phrase, names=None, are_trees=False)
Special wrapper for a list which will be treated as a QueryPhrase. For use with the Query API