foundryts.functions.udf

foundryts.functions.udf(func, columns=None, types=None)

Returns a function that will call a user-defined function on the dataframe result of queries.

User defined functions (UDF) are a special time series feature that allow running custom Python code on the result of queries returning dataframes. The UDF is applied to the final dataframe in the result of all queries.

  • Parameters:
    • func (Callable[[pandas.DataFrame], Any]) – User defined function to apply.
    • columns (List [str ] , optional) – List of column names for the resulting dataframe when func returns pandas.DataFrame (default is the original column names in the input dataframe).
    • types (List *[*Any ] , optional) – List of column types for the resulting dataframe when func returns pandas.DataFrame (default is the original column types in the input dataframe).
  • Returns: The result of applying the UDF on the input :py`pandas.DataFrame`.
  • Return type: Any
See Also

dsl()

Examples

Copied!
1 2 3 4 5 6 7 >>> series = F.points((0, 0.0), (100, 100.0), (140, 140.0), (200, 200.0), name="series") >>> series.to_pandas() timestamp value 0 1970-01-01 00:00:00.000000000 0.0 1 1970-01-01 00:00:00.000000100 100.0 2 1970-01-01 00:00:00.000000140 140.0 3 1970-01-01 00:00:00.000000200 200.0
Copied!
1 2 3 4 5 6 7 8 9 10 >>> def double(df: pandas.DataFrame) -> pandas.DataFrame: ... df["value"] *= 2 ... return df >>> doubled_series = F.udf(double, ["timestamp", "value"], [int, float])(series) >>> doubled_series.to_pandas() timestamp value 0 1970-01-01 00:00:00.000000000 0.0 1 1970-01-01 00:00:00.000000100 200.0 2 1970-01-01 00:00:00.000000140 280.0 3 1970-01-01 00:00:00.000000200 400.0