What you will be able to do
- Explain which limitation of single-node pandas pyspark.pandas removes, and why that helps both pandas users and PySpark users
- Move data between Spark DataFrames, pandas-on-Spark DataFrames and plain pandas, and say which conversions collect data to the driver
- Explain why enabling Arrow speeds up pandas conversions
- Recognise where Pandas API on Spark stops paying off: default index creation, row order, unsupported pandas behaviour and small data
Key concept
Pandas API on Spark (pyspark.pandas) — A pandas-compatible API whose DataFrames and Series are backed by Spark. You write the same pandas calls, but Spark runs them lazily as distributed jobs across the cluster, so a dataset no longer has to fit in one machine's memory.
1.The limit of plain pandas that pyspark.pandas removes
Plain pandas is a single-machine library. A pandas DataFrame lives entirely in the memory of the Python process that created it, and every method call runs immediately on that one machine. On a laptop with 16 GB of RAM, a 500 GB Delta table can't be loaded as a pandas DataFrame, and adding nodes to a cluster doesn't change that. pandas uses only the process it runs in.
Pandas API on Spark keeps the pandas programming model and changes where the work runs. You import it with import pyspark.pandas as ps, and a pandas-on-Spark DataFrame looks and behaves like a pandas DataFrame. Underneath it is a Spark plan. Calls such as dropna(), groupby() and sum() become Spark transformations that executors run in parallel over partitions, so the data never has to fit in a single process. Pandas API on Spark has shipped with Apache Spark since 3.2. Before that it was a separate project called Koalas, so treat "Koalas" in older material as the same idea under its old name.
The second difference is when the work runs. pandas evaluates eagerly: each line produces a finished in-memory result. Spark evaluates lazily: transformations build up a plan, and an action triggers the distributed computation. That lazy plan lets the engine optimise the whole chain before running it. It is also why some pandas habits that are cheap on one machine become expensive on a cluster.
Checkpoint 1 of 8· Check yourself
A data scientist's pandas script runs fine on a sample. On a 16-node Databricks cluster it fails with out-of-memory errors when loading a 500 GB Delta table. What is the underlying problem, and what does Pandas API on Spark change?
Plain pandas runs eagerly on a single machine and needs the whole dataset in that process's memory, so adding nodes does not help. Pandas API on Spark keeps the pandas calls but runs them as lazy, distributed Spark jobs. pandas already has groupby(), runs eagerly rather than lazily, and installs alongside PySpark, so the other options blame problems it does not have.
“pandas does not scale out to big data. Pandas API on Spark fills this gap”Source: docs.databricks.com
Checkpoint 2 of 8· Exam question
What is the primary limitation of native pandas that the Pandas API on Spark (pyspark.pandas) is designed to address?
Correct answer: A — pandas executes eagerly on a single machine and must hold the entire dataset in local memory, while pyspark.pandas runs the same pandas-style code as distributed Spark jobs across a cluster.
- A. This is correct: pandas is single-machine and in-memory, so datasets larger than the driver's RAM cause it to fail or slow to a crawl. The pandas API on Spark keeps the same method calls but executes them as distributed operations over a Spark cluster, which is exactly the scaling gap it closes.
- B. This is incorrect because native pandas already has full
groupby()and aggregation support; that was never a missing capability. The pandas API on Spark reuses this existing pandas surface rather than inventing aggregation functions pandas lacked. - C. This is incorrect because pandas and PySpark can coexist in the same Python environment without conflict, and namespace isolation was never the motivation for the project. The real driver is scaling pandas-style computation, not packaging separation.
- D. This is incorrect because there is no documented numerical-consistency problem between pandas and Spark SQL that prompted this project, and rounding-rule alignment is not a stated design goal. The actual motivation is memory and machine scale, not numeric parity.
- E. This is incorrect because pandas has never required explicit upfront schemas; it infers types from the data itself. Schema inference was therefore not a gap the pandas API on Spark needed to fill.
2.One codebase for pandas users and PySpark users
For a pandas user, the main advantage is that existing code keeps working. Say a cleaning script calls df.dropna() and then df.groupby('region')['sales'].sum(). To run it against a large Delta table, read the table into a Spark DataFrame, call sdf.pandas_api(), and run the same calls on the result. You don't translate it into na.drop() and agg(), wrap it in a UDF, or collect the table to the driver first. The same code can run against plain pandas on a small sample in unit tests and against pandas-on-Spark in production.
PySpark users gain something too. Some tasks that take extra work with the native DataFrame API are one call in pandas style. Plotting is one example: psdf.plot() works directly on distributed data. Other examples are descriptive summaries with describe(), transposing with .T, and label-based selection with an index. A PySpark engineer can switch to pandas syntax for one step and then switch back.
Conversion between the two worlds is a single method call in each direction. DataFrame.pandas_api() turns a Spark DataFrame into a pandas-on-Spark DataFrame. Its optional index_col argument picks which Spark column becomes the index. psdf.to_spark() goes the other way and returns a regular Spark DataFrame that you can pass to Spark SQL, Structured Streaming sinks or MLlib.
df = spark.createDataFrame(
[(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
df.pandas_api()
# age name
# 0 14 Tom
# 1 23 Alice
# 2 16 Bob
df.pandas_api(index_col="age")
# name
# age
# 14 Tom
# 23 Alice
# 16 Bob| From → to | Call | Where the data ends up |
|---|---|---|
| Spark DataFrame → pandas-on-Spark | sdf.pandas_api() | Stays distributed on the cluster |
| pandas → pandas-on-Spark | ps.from_pandas(pdf) | Distributed from the driver's local pandas object |
| pandas → Spark DataFrame | spark.createDataFrame(pdf) | Distributed from the driver's local pandas object |
| Spark DataFrame → pandas | toPandas() | Collected into driver memory as plain pandas |
Checkpoint 3 of 8· Check yourself
Your team has a pandas cleaning script that calls df.dropna() and then df.groupby('region')['sales'].sum(). The unit tests run it on small pandas samples. Now it has to run in production against a multi-terabyte Delta table. Which approach reuses the script and keeps the data distributed?
sdf.pandas_api() returns a pandas-on-Spark DataFrame. The same pandas calls run on it as distributed Spark jobs, so one codebase serves both the pandas tests and the Spark production run. toPandas() collects every row onto the driver. A pandas UDF is a separate feature, and you don't need a rewrite into native PySpark.
“with a single codebase that works with pandas (tests, smaller datasets) and with Spark (production, distributed datasets)”Source: docs.databricks.com
Checkpoint 4 of 8· Fill the gap
The second conversion makes the age column the index of the pandas-on-Spark DataFrame instead of a default 0, 1, 2 index. Which argument fills the blank?
df = spark.createDataFrame(
[(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
df.pandas_api()
# age name
# 0 14 Tom
# 1 23 Alice
# 2 16 Bob
df.pandas_api( ? ="age")
# name
# age
# 14 Tom
# 23 Alice
# 16 BobThe optional index_col argument of DataFrame.pandas_api() picks which Spark column becomes the index. Without it, pandas-on-Spark creates a default integer index.
Source: docs.databricks.comCheckpoint 5 of 8· Exam question
A data scientist has an existing pandas script that cleans a DataFrame using df.dropna() followed by df.groupby('region')['sales'].sum(). They want to reuse this exact logic against a 500 GB Delta table without rewriting the pandas calls. Which change lets them do this with the pandas API on Spark?
Correct answer: A — Read the Delta table into a Spark DataFrame, convert it with sdf.pandas_api(), and run the same dropna() and groupby() calls on it, executing them as distributed Spark jobs instead of local pandas operations.
- A. This is correct:
pandas_api()converts a Spark DataFrame into a pandas-on-Spark DataFrame in place, so the existingdropna()andgroupby()calls run unchanged but execute as distributed Spark operations instead of local pandas operations. - B. This is incorrect because the pandas API on Spark's entire purpose is to avoid a rewrite into native Spark SQL syntax; forcing a translation into
na.drop()/agg(sum())defeats the reason for using it. - C. This is incorrect because collecting a 500 GB table onto the driver with
toPandas()is exactly the memory bottleneck the pandas API on Spark exists to avoid, and it is not a prerequisite for usingpyspark.pandas. - D. This is incorrect because the pandas API on Spark reads Delta tables directly through Spark's data sources; exporting to a local CSV file is unnecessary and reintroduces the single-machine memory limit.
- E. This is incorrect because
pandas_udfis a separate mechanism for applying vectorized functions to Spark DataFrames, not the waypyspark.pandasreuses whole pandas scripts, and it is not row-by-row execution.
3.Arrow and the cost of crossing into Python
Spark runs in the JVM and pandas runs in Python, so data that crosses between them has to be serialised. Without Arrow, rows are converted one at a time. Apache Arrow is a columnar in-memory format that both sides can read, so whole column batches move at once. Pandas API on Spark uses the same PySpark configuration as the rest of the session, so the Arrow switch spark.sql.execution.arrow.pyspark.enabled speeds up its internal pandas conversions too.
spark.conf.set("spark.sql.execution.arrow.pyspark.enabled", True)
%timeit ps.range(300000).to_pandas()
900 ms ± 186 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
[36]:
spark.conf.set("spark.sql.execution.arrow.pyspark.enabled", False)
%timeit ps.range(300000).to_pandas()
3.08 s ± 227 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)Arrow speeds up the transfer, but it doesn't change where the data goes. A conversion to plain pandas still lands every row on the driver. Results are the same with Arrow on or off. Some data types aren't supported by Arrow, and when Spark hits one it can fall back to the slower path without raising an error. The fallback is controlled by spark.sql.execution.arrow.pyspark.fallback.enabled. Shuffle partitions, driver heap, AQE and storage levels don't affect the JVM-to-Python serialisation cost.
Checkpoint 6 of 8· Check yourself
With spark.sql.execution.arrow.pyspark.enabled set to true, an engineer plans to call to_pandas() on a 2 TB pandas-on-Spark DataFrame, reasoning that the conversion is now fast enough. What does Arrow actually change about this call?
Arrow only makes the JVM-to-Python serialisation faster by moving whole column batches. A conversion to plain pandas still collects every row onto the driver, and results are the same with Arrow on or off. A 2 TB table still runs into the single-node limit.
“Even with Arrow, toPandas() results in the collection of all records in the DataFrame to the driver program”Source: docs.databricks.com
Checkpoint 7 of 8· Exam question
An engineer benchmarks converting a 300,000-row Spark DataFrame to a pandas-on-Spark DataFrame and finds the operation takes several seconds. A teammate suggests a configuration change that previously cut a similar conversion time from about 3 seconds to under 1 second. Which setting most likely explains that speedup?
Correct answer: A — Enabling spark.sql.execution.arrow.pyspark.enabled so that data transfer between the JVM and Python process uses Apache Arrow's columnar format instead of row-by-row serialization, which pandas API on Spark depends on for speed.
- A. This is correct: Arrow-based transfer avoids expensive row-by-row (de)serialization between the JVM and Python, and pandas API on Spark documents large speedups from enabling it, matching the roughly 3x improvement described.
- B. This is incorrect because shuffle partition count controls how a wide transformation is split during execution, not the JVM-to-Python serialization cost that dominates pandas-on-Spark conversion time.
- C. This is incorrect because the bottleneck in this scenario is the format used to move data between processes, not the amount of driver heap available; more memory alone does not change the serialization path.
- D. This is incorrect because AQE optimizes shuffle and join behavior during query execution, not the row-versus-columnar transfer format used when materializing data into a pandas-on-Spark DataFrame.
- E. This is incorrect because storage level affects whether cached partitions are held in memory or spilled to disk, which is unrelated to how data is serialized when it crosses the JVM-Python boundary during conversion.
4.Where the advantages stop: index, order and small data
Most of the hidden cost comes from the index. Every pandas DataFrame has one, and a Spark DataFrame has none. If you call pandas_api() without index_col, or read a file through ps, pandas-on-Spark has to create a default index, and creating it is real distributed work. The option compute.default_index_type controls which kind you get. A sequence index numbers rows 0, 1, 2… by moving the data through a single partition, which stops scaling on large data. distributed-sequence also produces consecutive numbers but computes them across the cluster, which still costs an extra pass. distributed is the cheapest: it assigns increasing IDs that aren't consecutive and that can differ between runs. To skip index creation entirely, pass a real key column as index_col.
Row order is the second limit. A pandas DataFrame keeps rows in the order they were loaded. A Spark DataFrame spread over partitions has no natural order, so head() may not return the first rows of the file. You can turn on compute.ordered_head to make it do so, but that adds an internal sort to every call. Operations whose meaning depends on position, such as positional slicing or comparing a row with its neighbour, pay for that ordering on large data. Some pandas features aren't implemented at all or behave differently. For example, an operation that combines two different DataFrames is blocked by default, because it requires a join. For these gaps, native PySpark gives you more control.
Size is the last limit. For a few hundred megabytes that fit comfortably in memory, plain pandas on a single node is faster: it skips job scheduling, task launches and the JVM-to-Python transfer. Pandas API on Spark pays off once the data, or the memory pandas needs to process it, is too large for one machine. For data that small, Spark itself is often the wrong tool.
Checkpoint 8 of 8· Check yourself
After an analyst moves a pandas report to pandas-on-Spark, psdf.head() no longer reliably shows the first five rows of the source file. What explains this, and what does restoring the pandas behaviour cost?
A Spark DataFrame has no natural order, so head() may not return the first rows of the file. compute.ordered_head keeps the pandas behaviour, but it costs an internal sort on each call. Neither the index, Arrow nor AQE determines row order.
“The natural order can be preserved by setting compute.ordered_head option but it causes a performance overhead with sorting internally.”Source: spark.apache.org
Sources
(6)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“pandas does not scale out to big data. Pandas API on Spark fills this gap”
↩︎ The limit of plain pandas that pyspark.pandas removes“import pyspark.pandas as ps”
↩︎ The limit of plain pandas that pyspark.pandas removes“Pandas API on Spark is useful not only for pandas users but also PySpark users”
↩︎ One codebase for pandas users and PySpark users“pandas does not scale out to big data. Pandas API on Spark fills this gap”
↩︎ Key concept - 2.https://docs.databricks.com/aws/en/pysparkOfficial docs
“This varies drastically from eager execution, which is the model used by pandas DataFrames.”
↩︎ The limit of plain pandas that pyspark.pandas removes“with a single codebase that works with pandas (tests, smaller datasets) and with Spark (production, distributed datasets)”
↩︎ One codebase for pandas users and PySpark users - 3.
“Converts the existing DataFrame into a pandas-on-Spark DataFrame.”
↩︎ One codebase for pandas users and PySpark users“it will lose the index information and the original index will be turned into a normal column.”
↩︎ Where the advantages stop: index, order and small data“Converts the existing DataFrame into a pandas-on-Spark DataFrame.”
↩︎ Exam trap 1 - 4.
“Apache Arrow is an in-memory columnar data format used in Apache Spark to efficiently transfer data between JVM and Python processes.”
↩︎ Arrow and the cost of crossing into Python“toPandas() results in the collection of all records in the DataFrame to the driver program”
↩︎ Exam trap 2“Apache Arrow is an in-memory columnar data format used in Apache Spark to efficiently transfer data between JVM and Python processes.”
↩︎ Exam trap 3“Even with Arrow, toPandas() results in the collection of all records in the DataFrame to the driver program”
↩︎ Checkpoint - 5.
“you can enable Arrow optimization to hugely speed up internal pandas conversion.”
↩︎ Arrow and the cost of crossing into Python“The natural order can be preserved by setting compute.ordered_head option but it causes a performance overhead with sorting internally.”
↩︎ Where the advantages stop: index, order and small data“The natural order can be preserved by setting compute.ordered_head option but it causes a performance overhead with sorting internally.”
↩︎ Exam trap 4 - 6.https://docs.databricks.com/aws/en/languages/pythonOfficial docs
“PySpark is more flexible than the Pandas API on Spark”
↩︎ Where the advantages stop: index, order and small data“For small workloads which only require single nodes, data scientists can use single node compute for cost savings.”
↩︎ Where the advantages stop: index, order and small data