Skip to main content
Version: Python

User-Defined Functions

Python, Groovy, and Java functions can be used inside query strings. These functions can be from libraries, or they can be user-defined.

The list of default Java imports can be reviewed in the deephaven-core repository on GitHub.

Examples

User-defined function

In the following example, a custom, user-defined function is used inside a query string to compute new column values.

from deephaven import new_table
from deephaven.column import int_col

source = new_table([int_col("X", [2, 4, 6]), int_col("Y", [8, 10, 12])])


def f(a, b):
return a * b


result = source.update(formulas=["X", "Y", "Product = f(X, Y)"])

Passing tables to functions

In the following example, the compute function uses the source table and the input parameter a.

from deephaven import new_table
from deephaven.column import int_col

import numpy as np


def f(a, b) -> np.intc:
return a * b


def compute(source, a):
return source.update(formulas=["X = f(a, A)"])


source = new_table([int_col("A", [1, 2, 3])])

result1 = compute(source, 10)
result2 = compute(source, 3)

Function from a library

In the following example, a function from a library is called.

from deephaven import empty_table
import numpy as np

result = empty_table(63).update(formulas=["X = 0.1 * i", "Y = (double)np.sin(X)"])

Imported Java methods

In the following example, imported Java methods are used inside the query string.

note

For details on how to install Java packages, see this guide.

from deephaven import new_table
from deephaven.column import int_col

source = new_table([int_col("X", [2, 4, 6])])
result = source.update(formulas=["Z = sqrt(X)"])