# .apply() and passing callables

Some pandas functions, like

`df.apply(f)`

, support an arbitrary function as a parameter. Most of the time, Terality will support these functions without any change.

In a few situations, Terality won't be able to transfer the arbitrary function defined in your code to the Terality servers. This page lists which scenarios are supported, and which are currently not.

Terality accepts functions (and lambdas) that depend on known modules:

- functions from the Python 3.8 standard library:

from math import floor

# supported, function from the standard library

df.apply(floor)

- functions from pandas
- functions from numpy:

import numpy as np

# supported, numpy function

df.apply(np.float64)

- functions (or lambdas) defined by the user in the current module:

def my_function_1(x):

return x**2

def my_function_2(x):

return 1 / (1 + my_function_1(x))

# supported, functions are all defined in the same module

df.apply(my_function_1)

df.apply(my_function_2)

# supported, lambdas are ok too

df.apply(lambda x: 1 / (1 + x^2))

- and any combination of the supported cases mentioned previously

Terality currently does not support functions:

- using third party dependencies other than numpy and pandas
- defined in other modules:

from my_custom_module import my_custom_function

# Terality can not import such a function

df.apply(my_custom_function)

- referencing a Terality structure:

s = te.Series([0, 1, 2])

s2 = te.Series(["a", "b", "c"])

# unsupported

s.apply(lambda x: s2.iloc[x])

Additionally, functions with side effects (such as accessing the network or writing to disk) are not supported. They may return the correct result, raise an error, or silently return an incorrect result.

Last modified 1yr ago