Sub topic 15
A decision framework
This chapter assumes you can write basic Python and SQL, know what a DataFrame is, and have loaded data with pandas or SQL before. It also assumes you have read the earlier chapters on execution models, memory limits, and distributed costs. Here you turn those facts into a repeatable way to choose a tool.
Start with the questions, not the tools
Answer five questions in order. Stop at the first one that decides the choice.
- How big is the data, and how big will it get? Compare the working set to the RAM on one machine. A common rule of thumb is that pandas needs several times the file size in memory for joins and group-bys. Estimate with your own data, not the rule.
- Is the work a one-off exploration or a repeated pipeline? Exploration favors interactive tools. Pipelines favor tools that are predictable, testable, and easy to deploy.
- What does the team already know? A SQL-heavy team moves faster with DuckDB. A team with years of pandas code pays a real cost to switch.
- Where does the data live and where will the job run? A laptop, a single server, and a managed cluster point to different tools.
- What does the rest of the stack require? Some libraries accept only pandas DataFrames. Some platforms provide Spark and nothing else.
A first-pass table
| Situation | Usual first choice | Why |
|---|---|---|
| Data fits in RAM several times over, quick analysis, many libraries needed | pandas | Largest ecosystem, eager and simple |
| Data fits on one machine, you want speed and a DataFrame API | Polars | Lazy optimization, multi-threaded |
| Data fits on one machine or on local disk, team prefers SQL | DuckDB | Embedded SQL engine, reads Parquet directly, can spill to disk |
| Data exceeds one machine, or the platform is already a cluster | Spark | Distributes work across executors |
Treat this table as a starting guess. The benchmarking chapter shows how to test the guess on your own workload.
The diagram
Common errors
- Choosing Spark too early. Startup time, shuffles, and cluster operations cost more than they return on data that one machine can process. Many workloads that people send to Spark finish faster in DuckDB or Polars.
- Staying on pandas when memory fails. If you hit out-of-memory errors or wait minutes on a group-by, test a lazy engine before you buy a bigger machine.
- Trusting published benchmarks. They use other data, other hardware, and other queries.
- Ignoring the cost of switching. A rewrite needs tests, review, and retraining. Switching pays off only when the gain is large and recurring.
- Treating the choice as exclusive. Parquet and Arrow let you use DuckDB for a heavy aggregation and hand the small result to pandas.
Applying the checklist
Suppose you have a 40 GB set of Parquet files on a server with 64 GB of RAM. The team knows SQL well, and the job runs nightly. The data fits on one machine but not comfortably in memory for pandas. DuckDB and Polars lazy mode can both read only the needed columns and spill or stream. The SQL skills decide it: DuckDB. If the output feeds a pandas-only model library, convert only the small result.
import duckdb
summary = duckdb.sql(
"SELECT region, sum(revenue) AS revenue "
"FROM 'sales/*.parquet' GROUP BY region"
).df()
print(summary)Exercise
A team has 5 GB of CSV data, a laptop with 32 GB of RAM, existing pandas code, and a plotting library that accepts only pandas DataFrames. Which tool do you choose first, and which question decided it?
Show answer
pandas. The data fits in RAM with room to spare (question 1), and the team's existing code and the pandas-only library (questions 3 and 5) make switching costly with little gain. If a specific step is slow, run that step in DuckDB or Polars and convert the result back to pandas.
Exercise
Why is "Spark scales to any size, so use it everywhere" a weak rule?
Show answer
Spark adds startup time, shuffle costs, and cluster operations. For data that fits on one machine, a single-node engine such as DuckDB or Polars usually runs faster and is simpler to operate. Spark is the right choice when the data or the platform requires distribution.