Skip to content

Data preprocessing

Convert and replace the data in a table. These methods are available on pykx.Table and pykx.KeyedTable objects.

astype

astype(dtype, copy=True, errors='raise')

Cast columns of the table to a different data type.

Parameters:

Name Type Description Default
dtype type or dict

A pykx or Python type to cast every column to, or a dictionary of column names to types.

required
copy bool

Only True is implemented.

True
errors str

'raise' raises an error if a cast fails. Any other value returns the table unchanged.

'raise'

Returns:

Type Description
Table

The table with the columns cast.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.astype(kx.FloatVector)
pykx.Table(pykx.q('
a b
---
1 4
2 5
3 6
'))

select_dtypes

select_dtypes(include=None, exclude=None)

Select the columns of the table by their data types.

At least one of include and exclude is required and they cannot overlap. If both are given exclude is ignored. For a KeyedTable the key columns are always kept and only the value columns are filtered. Use kx.CharAtom for a column of single characters, as kx.CharVector raises an error.

Parameters:

Name Type Description Default
include type, str or list

A data type or string, or a list of them, of the columns to keep.

None
exclude type, str or list

A data type or string, or a list of them, of the columns to remove.

None

Returns:

Type Description
Table

The table of the selected columns, or pykx.Identity(pykx.q("::")) if there are none.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:1.5 2.5 3.5)')
>>> tab.select_dtypes(include=kx.FloatVector)
pykx.Table(pykx.q('
b
---
1.5
2.5
3.5
'))

replace

replace(to_replace, value)

Replace all occurrences of a value in the table.

Parameters:

Name Type Description Default
to_replace Any

The value to replace.

required
value Any

The value to replace it with.

required

Returns:

Type Description
Table

The table with the values replaced.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.replace(5, 50)
pykx.Table(pykx.q('
a b
----
1 4
2 50
3 6
'))