Built-in Spark functions cover the vast majority of column transformation needs, but occasionally a business rule cannot be expressed with built-ins and requires custom Python logic. User-Defined Functions (UDFs) let Python functions be applied to DataFrame columns, but they cross from the JVM's Tungsten execution engine into Python's runtime, serialising each row through pickle — a process that can be 10 to 100 times slower than equivalent built-in functions. Understanding when to use UDFs and when Pandas UDFs provide a faster alternative is essential for any PySpark practitioner.
Pandas UDFs solve the row-at-a-time serialisation problem by operating on pandas Series or DataFrames — batches of rows — rather than individual values. Apache Arrow transfers data between the JVM and Python in a zero-copy columnar format, eliminating pickle serialisation entirely. The result is a UDF that is 10 to 100 times faster than a standard Python UDF while still allowing arbitrary Python and pandas logic. Pandas UDFs are the correct choice for any custom transformation logic that must operate on batches of rows efficiently.