pandas, Polars, DuckDB, Spark and PyArrow all store tables differently inside, and every time data crosses from one to another it is converted. Arrow is an agreement about one layout that all of them can use, so a table built by one can be read by another with no conversion at all. This topic looks at that layout itself.
The idea in plain words
Where we are. The first topic of the Arrow skill. The Parquet skill covered the file format on disk; Arrow is the matching format in memory, and PyArrow is what reads Parquet into it. The next topics build on this one: tables and batches, computing on columns, the buffers underneath, and sharing the memory between libraries.
A block of values of one type stored back to back in memory, with a count of nulls. A table is a set of named arrays of equal length, so a row is not an object but the same position read from every column.
A table is columns
column_info reads the schema: each column's name, type and null count. Every value in a column has the same type, so Arrow can store the values of a double column as raw 8-byte numbers in a row. rows_head shows the price of that design: to see row 1 it reads position 1 of every column and glues the pieces together, the opposite of a row store.
# The schema of the bars table, and the first two rows read across the columnsfor name, type_text, nulls in column_info(table): print(f"{name:7} {type_text:12} nulls: {nulls}")print(rows_head(table, 2))
symbol string nulls: 0
date date32[day] nulls: 0
open double nulls: 0
high double nulls: 0
low double nulls: 0
close double nulls: 0
volume int64 nulls: 0
[('AAPL', datetime.date(2000, 1, 3), 0.785568, 0.842382, 0.761624, 0.83826, 638640787), ('AAPL', datetime.date(2000, 1, 4), 0.810884, 0.828742, 0.758094, 0.76771, 610726329)]# One column is an array: ask for a slice of the closes, then count its bytesprint(column_slice(table, "close", 0, 5))sizes = column_nbytes(table)print(sizes)print("close:", table.num_rows, "values x 8 bytes =", table.num_rows * 8, "bytes; arrays say", sizes["close"])
[0.83826, 0.76771, 0.778895, 0.711585, 0.745631]
{'symbol': 234451, 'date': 133972, 'open': 267944, 'high': 267944, 'low': 267944, 'close': 267944, 'volume': 267944}
close: 33493 values x 8 bytes = 267944 bytes; arrays say 267944Weigh the arrays
Because the layout is so plain you can predict the size. 33,493 doubles at 8 bytes each are 267,944 bytes, exactly what the close array occupies. A date32 takes 4 bytes. A string column needs more than its text: an offsets array that says where each string starts, covered in topic 4. A column with nulls adds a validity bitmap, one bit per value.
Reaching for rows, for row in table, is the habit that makes Arrow code slow: it undoes the layout one value at a time. Ask for the whole column and let a kernel work on it (topic 3). Use rows_head-style row reads only for small displays and tests.
What the tasks ask
- 1`column_info`Loop
table.schema; for each field return(field.name, str(field.type), table[field.name].null_count). - 2`rows_head` and `column_slice`
table[name].slice(offset, length).to_pylist(); for rows,zipthe per-column lists. - 3`column_nbytes`
{name: table[name].nbytes for name in table.column_names}.
double values take 267,944 bytes?Lab · what's graded
4 tasks · 100 pts- Describe the columns:
column_info25 pts - Read across the columns:
rows_head25 pts - Slice one column:
column_slice25 pts - Weigh the columns:
column_nbytes25 pts