<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Pandas_UDF</id>
	<title>Pandas UDF - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Pandas_UDF"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Pandas_UDF&amp;action=history"/>
	<updated>2026-12-06T00:08:10Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Pandas_UDF&amp;diff=164&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Pandas_UDF&amp;diff=164&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:46Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Pandas UDF&lt;br /&gt;
| also_known_as   = Vectorized UDF, &amp;lt;code&amp;gt;pandas_udf&amp;lt;/code&amp;gt;&lt;br /&gt;
| field           = Distributed data processing&lt;br /&gt;
| introduced      = Apache Spark 2.3.0&lt;br /&gt;
| key_parts       = &amp;lt;code&amp;gt;pandas_udf&amp;lt;/code&amp;gt; decorator, Python type hints, Arrow record batches, Python worker&lt;br /&gt;
| implementations = [[Apache Spark]] (PySpark)&lt;br /&gt;
| related         = [[User-defined function (Spark)]], [[Arrow-optimized Python UDF]], [[Apache Arrow]], [[DataFrame]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (python/pyspark/sql/pandas/functions.py, the &amp;quot;Apache Arrow in PySpark&amp;quot; tutorial, examples/src/main/python/sql/arrow.py, SQLConf.scala, the PySpark migration guide), the Spark 2.3.0 and 4.2.0 release notes and the 2017 Databricks blog post that introduced the feature}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], a &amp;#039;&amp;#039;&amp;#039;pandas UDF&amp;#039;&amp;#039;&amp;#039; (also called a &amp;#039;&amp;#039;&amp;#039;vectorized UDF&amp;#039;&amp;#039;&amp;#039;) is a Python [[User-defined function (Spark)|user-defined function]] that Spark runs on batches of rows: Spark sends each batch to a Python worker as [[Apache Arrow]] data, the function receives and returns pandas &amp;lt;code&amp;gt;Series&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;DataFrame&amp;lt;/code&amp;gt; objects, and the results are sent back to the JVM.&amp;lt;ref name=&amp;quot;arrow&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/api/python/tutorial/sql/arrow_pandas.html |title=Apache Arrow in PySpark (PySpark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Pandas UDFs are defined with &amp;lt;code&amp;gt;pyspark.sql.functions.pandas_udf&amp;lt;/code&amp;gt;, need no extra configuration, and are called like any other PySpark column function.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt; They were added in Spark 2.3.0 to cut the serialization and per-row call overhead of row-at-a-time Python UDFs.&amp;lt;ref name=&amp;quot;api&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/pandas/functions.py |title=python/pyspark/sql/pandas/functions.py (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;blog&amp;quot;&amp;gt;{{Cite web |url=https://www.databricks.com/blog/2017/10/30/introducing-vectorized-udfs-for-pyspark.html |title=Introducing Pandas UDF for PySpark |publisher=Databricks |date=2017-10-30 |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How does a pandas UDF work? ==&lt;br /&gt;
Spark splits the input columns of a pandas UDF into Arrow record batches, calls the Python function once per batch with the batch as pandas objects, and concatenates the results.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt; The maximum number of rows in one batch is &amp;lt;code&amp;gt;spark.sql.execution.arrow.maxRecordsPerBatch&amp;lt;/code&amp;gt;, which defaults to 10,000; the PySpark guide advises lowering it when a table has many columns, to avoid out-of-memory errors in the JVM.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;sqlconf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala |title=SQLConf.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The pandas UDF was announced by Databricks engineers in 2017 for the Spark 2.3 release. Their post explained that row-at-a-time Python UDFs suffer from high serialization and invocation overhead, and reported a benchmark in which pandas UDFs were 3 to over 100 times faster than row-at-a-time UDFs.&amp;lt;ref name=&amp;quot;blog&amp;quot; /&amp;gt; The Spark 2.3.0 release notes list &amp;quot;fast data serialization and vectorized execution&amp;quot; for PySpark (SPARK-22216).&amp;lt;ref&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-2-3-0.html |title=Spark Release 2.3.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Which pandas UDF types exist? ==&lt;br /&gt;
Since Spark 3.0, the Python type hints of the function decide the kind of pandas UDF. The older &amp;lt;code&amp;gt;functionType&amp;lt;/code&amp;gt; argument with &amp;lt;code&amp;gt;PandasUDFType&amp;lt;/code&amp;gt; is still accepted, but PySpark 4.3.0 emits a &amp;lt;code&amp;gt;UserWarning&amp;lt;/code&amp;gt; saying that it &amp;quot;will be deprecated in the future releases&amp;quot; (SPARK-28264).&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;api&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Type !! Type hints !! Output length !! Typical use&lt;br /&gt;
|-&lt;br /&gt;
| Series to Series&lt;br /&gt;
| &amp;lt;code&amp;gt;pd.Series, ... -&amp;gt; pd.Series&amp;lt;/code&amp;gt;&lt;br /&gt;
| Same as input&lt;br /&gt;
| Column transformations&lt;br /&gt;
|-&lt;br /&gt;
| Iterator of Series to Iterator of Series&lt;br /&gt;
| &amp;lt;code&amp;gt;Iterator[pd.Series] -&amp;gt; Iterator[pd.Series]&amp;lt;/code&amp;gt;&lt;br /&gt;
| Same as input, in total&lt;br /&gt;
| Expensive set-up (for example loading a model) done once per partition&lt;br /&gt;
|-&lt;br /&gt;
| Iterator of multiple Series to Iterator of Series&lt;br /&gt;
| &amp;lt;code&amp;gt;Iterator[Tuple[pd.Series, ...]&amp;amp;#93; -&amp;gt; Iterator[pd.Series]&amp;lt;/code&amp;gt;&lt;br /&gt;
| Same as input, in total&lt;br /&gt;
| As above, with several input columns&lt;br /&gt;
|-&lt;br /&gt;
| Series to Scalar&lt;br /&gt;
| &amp;lt;code&amp;gt;pd.Series, ... -&amp;gt; Any&amp;lt;/code&amp;gt; (a scalar)&lt;br /&gt;
| One value per group or window&lt;br /&gt;
| Aggregations in &amp;lt;code&amp;gt;groupBy().agg()&amp;lt;/code&amp;gt; and window functions&lt;br /&gt;
|}&lt;br /&gt;
Source for the table: the PySpark 4.3.0 Arrow guide.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt; A &amp;lt;code&amp;gt;pandas.DataFrame&amp;lt;/code&amp;gt; hint replaces &amp;lt;code&amp;gt;pandas.Series&amp;lt;/code&amp;gt; when an input or output column is a &amp;lt;code&amp;gt;StructType&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Series-to-Scalar pandas UDFs do not support partial aggregation: all data for a group or window is loaded into memory.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt; Spark 4.2.0 added an iterator form for grouped aggregation.&amp;lt;ref name=&amp;quot;api&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What are pandas function APIs? ==&lt;br /&gt;
Pandas function APIs apply a Python function to a whole PySpark [[DataFrame]] instead of to a column. They use the same Arrow transfer as pandas UDFs.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;DataFrame.mapInPandas&amp;lt;/code&amp;gt; maps an iterator of &amp;lt;code&amp;gt;pandas.DataFrame&amp;lt;/code&amp;gt; to another iterator, and the output can have any length.&lt;br /&gt;
* &amp;lt;code&amp;gt;DataFrame.groupBy().applyInPandas()&amp;lt;/code&amp;gt; applies a function to each group as one &amp;lt;code&amp;gt;pandas.DataFrame&amp;lt;/code&amp;gt; (the split-apply-combine pattern). It replaces the grouped map pandas UDF of Spark 2.x.&lt;br /&gt;
* &amp;lt;code&amp;gt;DataFrame.groupBy().cogroup().applyInPandas()&amp;lt;/code&amp;gt; applies a function to two DataFrames that share a key.&lt;br /&gt;
&lt;br /&gt;
For grouped and cogrouped maps, all data for a group is loaded into memory before the function runs, and &amp;lt;code&amp;gt;maxRecordsPerBatch&amp;lt;/code&amp;gt; is not applied, so skewed groups can cause out-of-memory errors.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The following PySpark example works on Spark 4.3.0 with pandas and PyArrow installed. It defines a Series-to-Series UDF and a Series-to-Scalar UDF.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import pandas as pd&lt;br /&gt;
from pyspark.sql.functions import pandas_udf&lt;br /&gt;
&lt;br /&gt;
@pandas_udf(&amp;quot;long&amp;quot;)&lt;br /&gt;
def multiply(a: pd.Series, b: pd.Series) -&amp;gt; pd.Series:&lt;br /&gt;
    return a * b&lt;br /&gt;
&lt;br /&gt;
@pandas_udf(&amp;quot;double&amp;quot;)&lt;br /&gt;
def mean_udf(v: pd.Series) -&amp;gt; float:&lt;br /&gt;
    return v.mean()&lt;br /&gt;
&lt;br /&gt;
df = spark.createDataFrame([(1, 1.0), (1, 2.0), (2, 3.0)], [&amp;quot;id&amp;quot;, &amp;quot;v&amp;quot;])&lt;br /&gt;
df.select(multiply(&amp;quot;id&amp;quot;, &amp;quot;id&amp;quot;)).show()&lt;br /&gt;
df.groupBy(&amp;quot;id&amp;quot;).agg(mean_udf(&amp;quot;v&amp;quot;)).show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What are the requirements and type rules? ==&lt;br /&gt;
In PySpark 4.3.0 the minimum supported versions are pandas 2.2.0 and PyArrow 18.0.0.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt; All Spark SQL data types are supported by Arrow-based conversion except an &amp;lt;code&amp;gt;ArrayType&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;TimestampType&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Timestamps passed to a pandas UDF are converted to nanosecond precision and localized to the session time zone (&amp;lt;code&amp;gt;spark.sql.session.timeZone&amp;lt;/code&amp;gt;); timestamps returned are converted to UTC microseconds.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Spark 4.2, a nullable integer column whose batch contains nulls arrives as a pandas nullable integer extension type (&amp;lt;code&amp;gt;Int8&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;Int64&amp;lt;/code&amp;gt;) instead of &amp;lt;code&amp;gt;float64&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;upgrade&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/api/python/migration_guide/pyspark_upgrade.html |title=Upgrading PySpark (PySpark 4.3.0 migration guide) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Since Spark 4.1, &amp;lt;code&amp;gt;spark.sql.execution.pandas.convertToArrowArraySafely&amp;lt;/code&amp;gt; is on by default, so unsafe conversions in returned data, such as integer overflow, raise errors.&amp;lt;ref name=&amp;quot;upgrade&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pandas UDF vs Arrow UDF and Arrow-optimized Python UDF ==&lt;br /&gt;
An [[Arrow-optimized Python UDF]] also uses Arrow for transfer, but the function still runs once per row on Python objects.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt; An Arrow UDF (&amp;lt;code&amp;gt;arrow_udf&amp;lt;/code&amp;gt;, Spark 4.1.0 and later) is vectorized like a pandas UDF, but the function receives &amp;lt;code&amp;gt;pyarrow.Array&amp;lt;/code&amp;gt; objects instead of pandas objects and avoids the pandas conversion.&amp;lt;ref&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/api/python/tutorial/sql/arrow_python_udf.html |title=Arrow Python UDFs (PySpark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Pandas UDFs have supported [[Spark Connect]] since Spark 3.4.0.&amp;lt;ref name=&amp;quot;api&amp;quot; /&amp;gt; See [[User-defined function (Spark)]] for a comparison of all UDF types.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[User-defined function (Spark)]]&lt;br /&gt;
* [[Arrow-optimized Python UDF]]&lt;br /&gt;
* [[Python UDTF]]&lt;br /&gt;
* [[Apache Arrow]]&lt;br /&gt;
* [[DataFrame]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>