Sub topic 4
pandas: the default for in-memory analysis
This chapter assumes you know basic Python, can read a tabular dataset as rows and columns with data types, and have loaded and transformed data with pandas or SQL before. It covers how pandas works, where it is strong, and where it stops being the right choice.
The working model
pandas is an in-memory, eager, single-node library. Each of those words has a consequence.
- In-memory: data is loaded into RAM as a
DataFrame. Every column is stored as a NumPy array, or as an Arrow-backed array if you opt in. - Eager: each operation runs the moment you call it. pandas has no query plan and no optimizer, so it cannot reorder your steps or skip work you did not need.
- Single-node: the common operations run on one thread. Many do not use your other CPU cores.
The first example loads the needed columns and aggregates them.
import pandas as pd
df = pd.read_parquet("sales.parquet", columns=["region", "revenue"])
result = df.groupby("region", as_index=False)["revenue"].sum()
print(result)The columns= argument matters. pandas reads only those two columns from the Parquet file. If you leave it out, pandas loads every column into memory, even ones you never use. A lazy engine would work this out for you. In pandas, you must do it yourself.
Where pandas is strong
- Interactive work. Results appear immediately, so you can inspect each step in a notebook.
- Ecosystem. scikit-learn, Matplotlib, statsmodels, and many other libraries accept DataFrames directly.
- Flexibility. Time series, reshaping, string handling, and row-wise custom logic are all well supported.
- Familiarity. Most data teams already know it, which lowers the cost of hiring and code review.
- Small to medium data. For data that fits comfortably in memory, startup cost is near zero and the code is short.
Where pandas runs into limits
| Limit | What happens |
|---|---|
| Memory | A DataFrame often needs several times the size of the source file. Intermediate copies add more. |
| Larger-than-memory data | pandas has no built-in out-of-core mode. You get a `MemoryError` or the operating system kills the process. |
| Speed | Most operations use one core, and there is no optimizer to remove unnecessary work. |
| Python loops | `apply` and `iterrows` run Python code per row and are much slower than vectorized operations. |
| Data types | Missing integers can turn a column into floats unless you use nullable types. |
Common errors
- Loading everything. Reading a wide file when you need two columns wastes memory. Pass
columns=. - Row loops. Replace
df.apply(lambda row: ..., axis=1)with column expressions such asdf["a"] * df["b"]. - Chained assignment. Writing
df[df["x"] > 0]["y"] = 1may change a copy, notdf. Usedf.loc[df["x"] > 0, "y"] = 1. - Ignoring dtypes. Text columns with few distinct values use far less memory as
category.
import pandas as pd
df = pd.DataFrame({"region": ["eu", "us", "eu"] * 1000, "revenue": [10, 20, 5] * 1000})
print(df.memory_usage(deep=True).sum())
df["region"] = df["region"].astype("category")
print(df.memory_usage(deep=True).sum())Run this and compare the two numbers. The second is much smaller because each distinct string is stored once.
When to look elsewhere
Stay with pandas while your data fits in memory with room to spare and speed is acceptable. Consider another tool when you hit memory errors, when a pipeline takes too long on one core, or when you want a query optimizer. Later chapters cover Polars, DuckDB, and Spark for those cases.
Exercise
A teammate reads a 40-column Parquet file with pd.read_parquet("big.parquet") and then uses only two columns. What change reduces memory use, and why does pandas not do it automatically?
Show answer
Pass columns=["col_a", "col_b"] to read_parquet, so only those columns are loaded. pandas is eager and has no query plan, so it cannot see which columns later steps use. It loads exactly what you ask for at the time of the call.