Pandas API on Spark
| Also known as | pandas-on-Spark, pyspark.pandas; formerly Koalas
|
|---|---|
| Field | Data processing, Python DataFrames |
| Introduced | Apache Spark 3.2.0 (2021) |
| Key parts | pyspark.pandas DataFrame, Series and Index; default index; options (compute.*); conversion to and from Spark DataFrames
|
| Open source implementations | Apache Spark (PySpark) 3.2.0 and later; Koalas for Spark 3.1 and earlier |
| Related | Apache Spark, Spark Connect, pyspark-client |
Pandas API on Spark (the pyspark.pandas module, also called pandas-on-Spark) is the part of PySpark in Apache Spark that implements the pandas DataFrame, Series and Index API on top of Spark, so code written for pandas can run distributed on a Spark cluster.[1] It was added in Spark 3.2.0 through SPARK-34849, which brought the Koalas project into PySpark.[2] The API looks like pandas, but results depend on Spark's distributed execution, which changes how the index, row order and some operations behave.[3]
What is pandas API on Spark?
Pandas API on Spark is imported as import pyspark.pandas as ps. A pandas-on-Spark DataFrame is backed by a Spark DataFrame and runs through the Spark SQL engine, so Spark configuration, the web UI and deployment modes apply to it unchanged.[4] The Spark 3.2.0 release notes describe it as letting pandas users scale out their applications on Spark with a one-line code change.[1]
The pandas-on-Spark FAQ recommends the pandas API for users who already know pandas and want to work with large data, and the PySpark DataFrame API for users learning Spark from the start.[5] In Spark 4.3.0, the pandas-on-Spark extra of the pyspark package requires pandas 2.2.0 or later and PyArrow 18.0.0 or later.[6]
How is pandas API on Spark used?
A pandas-on-Spark DataFrame can be created directly, converted from a Spark DataFrame with DataFrame.pandas_api(), and converted back with to_spark(). Converting to a plain pandas DataFrame with to_pandas() collects all data to the client, so it suits only small results.[7]
import pyspark.pandas as ps
sdf = spark.createDataFrame(
[(1, "a", 10.0), (2, "b", 20.0), (3, "a", 30.0)],
["order_id", "category", "amount"],
)
# Use a real column as the index instead of generating a default index
psdf = sdf.pandas_api(index_col="order_id")
psdf["amount_with_tax"] = psdf["amount"] * 1.2
print(psdf.groupby("category")["amount"].sum().sort_index())
result = psdf.to_spark(index_col="order_id") # back to a Spark DataFrame
Pandas API on Spark also works over Spark Connect: Spark 3.5.0 added pandas API support to the Python Spark Connect client (SPARK-42497).[8] The pure-Python pyspark-client package includes pyspark.pandas.[9]
How does pandas API on Spark differ from pandas?
Default index
A pandas DataFrame always has an index. When pandas API on Spark creates a DataFrame from a Spark DataFrame without an index column, it attaches a default index, whose type is set by compute.default_index_type.[3] The three types are:[3]
sequence: a sequence increasing by one, computed with a Window function without a partition specification, which moves all data to one partition; the documentation says to avoid it for large data.distributed-sequence(the default): a globally sequential index computed in a distributed way with a group-by and group-map approach.distributed:monotonically_increasing_idvalues, fully distributed but not consecutive and not deterministic.
The default was sequence in Spark 3.2.0 and has been distributed-sequence since Spark 3.3.0.[10][11] The best practices guide warns that distributed-sequence does not keep the same row-to-index mapping across operations such as apply(), groupby() or transform(), which can attach results to the wrong rows. It recommends setting an explicit index column, persisting the DataFrame, or using sequence.[4]
Row order
Pandas API on Spark does not guarantee row order. With compute.ordered_head at its default of False, head() can return rows from any partitions; setting it to True makes head() follow natural order at extra cost.[3][12]
Operations that are slow or behave differently
- Single-partition operations. Some APIs, such as
DataFrame.rank, use a Window without a partition specification and move all data to one machine; the guide suggestsGroupBy.rankinstead.[4] - Operations on different DataFrames. Combining two DataFrames or Series needs an internal join.
compute.ops_on_diff_framesdefaulted toFalsethrough Spark 3.5 and defaults toTruefrom Spark 4.0.0.[13][14][12] - Column names. Names with leading and trailing double underscores are reserved, duplicate column names are not allowed, and case-sensitive column names are disallowed unless
spark.sql.caseSensitiveis enabled.[4] - Shortcut limits.
compute.shortcut_limitandcompute.max_rows(both 1000 by default) control when small inputs are computed by collecting them and using pandas.[3] - Streaming. Pandas API on Spark does not officially support Structured Streaming; the FAQ suggests using it inside
foreachBatch.[5]
ANSI mode
Spark 4.0.0 enables ANSI SQL mode by default. In Spark 4.0.0, pandas API on Spark raises PANDAS_API_ON_SPARK_FAIL_ON_ANSI_MODE when ANSI mode is on, unless compute.fail_on_ansi_mode is set to False (SPARK-52026).[15][14] Spark 4.1.0 added the option compute.ansi_mode_support, which defaults to True; the fail-on-ANSI check raises only when that option is False.[16][12]
Is pandas API on Spark deprecated?
No. As of Spark 4.3.0, pyspark.pandas ships in PySpark, and its module code and documentation contain no deprecation notice.[12][3] Individual methods have been removed: the Spark 4.0.0 release notes list the removal of deprecated APIs from pandas API on Spark (SPARK-45550), and the migration guide lists DataFrame.koalas and the PySpark DataFrame.to_koalas and to_pandas_on_spark aliases as removed in Spark 4.0, replaced by pandas_on_spark and pandas_api.[17][18]
History
Pandas API on Spark began as Koalas, a separate open source project in the databricks/koalas GitHub repository, created on 3 January 2019 and licensed under the Apache License 2.0.[19] Koalas 0.1.0 was published on PyPI on 23 April 2019, and its last release, 1.8.2, on 19 October 2021.[20]
The SPIP SPARK-34849, "Support pandas API layer on PySpark", was filed on 24 March 2021 to port Koalas into PySpark, and the work shipped in Spark 3.2.0 as the pyspark.pandas module.[2][1] The Koalas README now marks the project as deprecated: it supports Spark 3.1 and earlier, the repository is in maintenance mode, and users of Spark 3.2 and later are told to use PySpark directly. The repository was not archived on the date checked.[19]
Migration from Koalas mostly means changing import databricks.koalas as ks to import pyspark.pandas as ps. The Spark migration guide lists the renamed and removed Koalas-specific names, such as databricks.koalas.__version__, replaced by pyspark.__version__.[18] The usage-logger environment variable KOALAS_USAGE_LOGGER is still read for compatibility, after PYSPARK_PANDAS_USAGE_LOGGER.[21]
See also
References
- ↑ 1.0 1.1 1.2 Spark Release 3.2.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 2.0 2.1 SPARK-34849: SPIP: Support pandas API layer on PySpark. Apache Software Foundation JIRA.Retrieved 2026-11-23.
- ↑ 3.0 3.1 3.2 3.3 3.4 3.5 Options and settings (pandas API on Spark, v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 4.0 4.1 4.2 4.3 Best Practices (pandas API on Spark, v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 5.0 5.1 FAQ (pandas API on Spark, v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ pyspark 4.3.0. Python Package Index.Retrieved 2026-11-23.
- ↑ From/to pandas and PySpark DataFrames (pandas API on Spark, v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Release 3.5.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ python/packaging/client/setup.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Options and settings (pandas API on Spark, v3.2.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Options and settings (pandas API on Spark, v3.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 12.0 12.1 12.2 12.3 python/pyspark/pandas/config.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Options and settings (pandas API on Spark, v3.5.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 14.0 14.1 Options and settings (pandas API on Spark, v4.0.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SPARK-52026: Block pandas API on Spark on ANSI mode by default. Apache Software Foundation JIRA.Retrieved 2026-11-23.
- ↑ Options and settings (pandas API on Spark, v4.1.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Release 4.0.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 18.0 18.1 Migrating from Koalas to pandas API on Spark (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 19.0 19.1 databricks/koalas: Koalas: pandas API on Apache Spark. GitHub.Retrieved 2026-11-23.
- ↑ koalas release history. Python Package Index.Retrieved 2026-11-23.
- ↑ python/pyspark/pandas/__init__.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.