Arrow interoperability¶
The Arrow PyCapsule interface
defines __arrow_c_stream__() as a standard method for exporting tabular data
as an Arrow C stream. A compatible consumer can call it on a dataframe to
obtain the data. Normally, pass the dataframe to the consumer instead of
handling the capsule yourself. Each capsule can be consumed only once.
Polars supports Arrow C streams and Arrow IPC without PyArrow. Other libraries may require PyArrow to consume or produce an Arrow representation.
Libraries supporting the interface¶
Support varies by object and direction. Examples include:
| Library | Documented support |
|---|---|
| PyArrow | Constructors consume Arrow-compatible arrays and streams. |
| Polars | Dataframes export streams and from_arrow accepts compatible objects. |
| pandas | DataFrame.from_arrow consumes compatible objects with pandas 3.0 or newer and PyArrow installed. |
| DuckDB | Python relations export Arrow streams. |
Interchange support does not imply that Uptimely's built-in runtime can evaluate every library's dataframe type. See Limits.
Uptimely passes values between functions unchanged. An annotation does not convert a dataframe automatically. Built-in validation, previews, and merges can convert Arrow tables to Polars for evaluation. The Uptimely examples use YAML authoring, which infers the return role from registered Arrow annotations.
PyArrow¶
Use Arrow compute functions directly on a table:
# script
import pyarrow as pa
import pyarrow.compute as pc
from uptimely import function
@function(category="calculate")
def double_arrow(dataset: pa.Table) -> pa.Table:
"""Add doubled values."""
return dataset.append_column("doubled", pc.multiply(dataset["value"], 2))
print(double_arrow(pa.table({"value": [1, 2, 3]})).to_pydict())
For the pyarrow.Table annotations, install PyArrow and configure:
See Python environment setup and project configuration for installing and registering PyArrow.
Polars¶
Convert Arrow to Polars, then return an Arrow table:
# script
import polars as pl
import pyarrow as pa
from uptimely import function
@function(category="calculate")
def double_polars(dataset: pa.Table) -> pa.Table:
"""Add doubled values using Polars."""
return pl.from_arrow(dataset).with_columns((pl.col("value") * 2).alias("doubled")).to_arrow()
print(double_polars(pa.table({"value": [1, 2, 3]})).to_pydict())
For this function's Arrow annotations, configure:
Polars DataFrame and LazyFrame annotations are recognized by default.
An eager Polars dataframe can also export an Arrow stream directly through
__arrow_c_stream__(). Pass the dataframe to a compatible consumer rather
than handling the single-use capsule yourself.
pandas¶
Use pandas to smooth one pump's readings in their existing order:
# script
import pandas as pd
import pyarrow as pa
from uptimely import function
def smooth(df: pd.DataFrame) -> pd.DataFrame:
"""Add an exponentially smoothed temperature."""
return df.assign(smoothed=df["temperature"].ewm(alpha=0.5, adjust=False).mean())
@function(category="calculate")
def smooth_arrow(dataset: pa.Table) -> pa.Table:
"""Smooth an Arrow table using pandas."""
return pa.Table.from_pandas(smooth(pd.DataFrame.from_arrow(dataset)), preserve_index=False)
print(smooth_arrow(pa.table({"temperature": [68.0, 70.0, 72.0]})).to_pydict())
For the Uptimely function's Arrow annotations, configure:
DataFrame.from_arrow requires pandas 3.0 or newer and PyArrow. The pandas
helper does not need registration. To inspect functions with pandas dataframe
annotations, add pandas.DataFrame to the eager list:
Even though pandas dataframes can export Arrow streams, registration does not
add pandas runtime support. Keep Uptimely function inputs and outputs
Arrow-typed as above. See
pandas DataFrame.from_arrow.
DuckDB¶
DuckDB functions can accept and return relations directly:
# script
import duckdb
def double_relation(dataset: duckdb.DuckDBPyRelation) -> duckdb.DuckDBPyRelation:
"""Add doubled values to a DuckDB relation."""
return dataset.project("value, value * 2 AS doubled")
relation = duckdb.sql("SELECT 1 AS value")
print(double_relation(relation))
This is a DuckDB example, not an Uptimely operator, so it requires no Uptimely configuration. To recognize relation annotations during inspection, register the lazy class:
Registration does not let Uptimely's built-in runtime evaluate relations directly. To use one inside an operator, convert its input and output to supported Arrow or Polars values. DuckDB relations can export Arrow streams directly. See DuckDB's Arrow export.
CUDA with cuDF¶
With an NVIDIA GPU and cuDF installed, convert a CPU Arrow table to cuDF, calculate on the GPU, and return a CPU Arrow table:
# script
import cudf
import pyarrow as pa
from uptimely import function
@function(category="calculate")
def double_gpu(dataset: pa.Table) -> pa.Table:
"""Add doubled values using cuDF."""
df = cudf.DataFrame.from_arrow(dataset)
df["doubled"] = df["value"] * 2
return df.to_arrow(preserve_index=False)
print(double_gpu(pa.table({"value": [1, 2, 3]})).to_pydict())
For the Arrow annotations, configure:
These conversions transfer data between CPU and GPU memory. Arrow also defines device-aware interfaces for GPU data, but Uptimely's built-in runtime does not evaluate GPU dataframes or device streams directly.
Snowpark¶
Given a configured Snowpark session, process an Arrow table remotely and
return the result as Arrow:
# example
import pyarrow as pa
from uptimely import function
@function(category="calculate")
def round_trip_snowpark(dataset: pa.Table) -> pa.Table:
"""Send an Arrow table to Snowpark and return the result."""
result = session.create_dataframe(dataset).to_pandas()
return pa.Table.from_pandas(result, preserve_index=False)
For the Arrow annotations, configure:
Snowpark
uploads the table to a temporary Snowflake table, and to_pandas() downloads
the result. This requires a configured session and permissions and is not an
in-memory exchange.
Limits¶
An Arrow C stream exchanges data within one process, not lazy query plans. Both libraries must support the interface and the data types. The exchange may share memory, but conversion or copying can be necessary.
These examples show library interoperability, not automatic conversion between Uptimely function calls. Functions receive upstream values unchanged and must accept their types. Uptimely currently recognizes Polars and PyArrow frames for runtime evaluation, not arbitrary Arrow-compatible objects.