Skip to content

Function application

Apply a function to the values of a table. These methods are available on pykx.Table and pykx.KeyedTable objects.

apply

apply(func, *args, axis=0, raw=None, result_type=None, **kwargs)

Apply a function along an axis of the table.

Parameters:

Name Type Description Default
func callable

The function to apply to each column or row, which is passed as a pykx list.

required
*args Any

Positional arguments passed to func after the list.

()
axis int

0 applies the function to each column and 1 to each row.

0
raw None

Not yet implemented, must be None.

None
result_type None

Not yet implemented, must be None.

None
**kwargs Any

Keyword arguments passed to func. Not supported when func is a q function.

{}

Returns:

Type Description
List, Dictionary or Table

The result of func for each column or row, as a list, dictionary or table.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.apply(kx.q.sum)
pykx.Dictionary(pykx.q('
a| 6
b| 15
'))

map

map(func, na_action=None, *args, **kwargs)

Apply a function to every element of the table.

A vectorized operation, such as tab * 2, is much faster than map with a function.

Parameters:

Name Type Description Default
func callable

The function to apply to each element.

required
na_action str

If 'ignore' nulls are returned without being passed to func.

None
*args Any

Positional arguments passed to func.

()
**kwargs Any

Keyword arguments passed to func. Not supported when func is a q function.

{}

Returns:

Type Description
Table

The table of the results.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.map(lambda x: x * 2)
pykx.Table(pykx.q('
a b
----
2 8
4 10
6 12
'))

applymap

applymap(func, na_action=None, *args, **kwargs)

Apply a function to every element of the table. This is an alias of map.

Parameters:

Name Type Description Default
func callable

The function to apply to each element.

required
na_action str

If 'ignore' nulls are returned without being passed to func.

None
*args Any

Positional arguments passed to func.

()
**kwargs Any

Keyword arguments passed to func. Not supported when func is a q function.

{}

Returns:

Type Description
Table

The table of the results.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.applymap(lambda x: x * 2)
pykx.Table(pykx.q('
a b
----
2 8
4 10
6 12
'))