Choose pandas API on Spark or a vectorized UDF
Preserve distributed execution while using pandas-style operations and batch-oriented Python functions.
Choose pandas API on Spark or a vectorized UDF
Work through the decision
A pandas notebook currently runs on one machine. The team wants familiar operations on a much larger table and one custom numerical transformation. Does calling toPandas on the whole table meet either scaling requirement?
Check your reasoning
Use pandas API on Spark for supported pandas-style operations backed by Spark execution. Inspect plans for expensive shuffles and index operations. Converting the full result to local pandas materializes it in one process. Keep that conversion bounded to a small sample or final result that fits.
Compare the choices
- pandas API on Spark
- Use distributed Spark-backed pandas-style DataFrames for supported operations. Check semantics, index costs and partition movement when porting code.
- Scalar pandas UDF
- Declare a column function operating on batches of values using pandas and Arrow, with a matching return type and output length for the scalar form.
- Built-in column expression
- Prefer a supported native expression when it already provides the transformation. It can participate in Spark SQL planning and avoids a Python boundary.
- Bounded local conversion
- Convert only a bounded result known to fit in memory when a local pandas consumer is required. toPandas collects the selected output locally.
Test the decision
For a custom element-wise transformation on batches, declare a scalar pandas UDF with a precise return type, accept a pandas Series, and return a Series of the same length for that UDF form. Spark uses Arrow for columnar transfer. A vectorized UDF still requires compatible Python dependencies on the workers and explicit handling of nulls. Compare with built-in expressions first.
Cross the Python boundary deliberately
A scalar pandas UDF receives batches of values. Declare its return type and return one element per input element in each batch. Activate your Python virtual environment before starting the local SparkSession so its workers use the same interpreter. Install the SQL dependencies there using python -m pip install "pyspark[sql]==4.0.1". The two-row sample is safe to inspect locally.
Apache Spark · Arrow and pandas UDFs
Predict the fares returned by the vectorized function
import pandas as pd
from pyspark.sql.functions import pandas_udf
@pandas_udf("double")
def dollars(cents: pd.Series) -> pd.Series:
return cents / 100.0
fares = spark.createDataFrame([(425,), (150,)], "cents long")
fares.select(dollars("cents").alias("dollars")).orderBy("dollars").show()
Check the result
The sorted values are 1.50 and 4.25. Arrow transfers batches between Spark and Python. Series arithmetic preserves the batch length. For this simple division, a built-in Spark column expression is sufficient.
Practice the guide skills
- Replace a full toPandas collection with a pandas API on Spark groupby and identify the remaining distributed boundary. Watch 2:27:00
- Declare a scalar pandas UDF, apply it to a column and check that each input batch yields the same number of output values. Watch 2:29:00 · Continue at 2:29:24 · Continue at 2:31:56
Official objectives for this lesson
Related lesson · Spark DataFrame API
Choose pandas API on Spark or a vectorized UDF quiz
Answer 3 questions. Check each answer for an explanation.