Sub topic 11
Arrow-backed dtypes
This chapter assumes you know NumPy dtypes, the object dtype, missing-value promotion, and pandas extension dtypes such as Int64 and string. Arrow-backed dtypes are one more family of extension dtypes. They store the column data in a different memory format.
What Arrow-backed means
Apache Arrow is a columnar memory format. The pyarrow library implements it. An Arrow-backed pandas column keeps its values in a pyarrow.ChunkedArray instead of a NumPy array. The column is still a pandas extension array, so it works with the index, memory_usage, and the usual DataFrame methods.
Arrow stores missing values in a separate validity bitmap, with one bit per value. The data buffer keeps its fixed width. Missing values therefore never force a type change, and no NaN sentinel is involved. Missing entries show up as <NA>, as they do with Int64.
Strings are stored as one contiguous block of UTF-8 bytes plus an offsets array. They are not stored as an array of pointers to separate Python str objects. This avoids the main cost of the object dtype.
Creating Arrow-backed columns
Install pyarrow, then name the dtype with a [pyarrow] suffix. You can also wrap an Arrow type in pd.ArrowDtype.
import pandas as pd
import pyarrow as pa
ints = pd.Series([1, 2, None], dtype="int64[pyarrow]")
words = pd.Series(["Oslo", "Lima", None], dtype=pd.ArrowDtype(pa.string()))
print(ints.dtype)
print(ints)
print(words.dtype)ints keeps an integer type and shows <NA> for the third value. The default NumPy path would have promoted it to float64 with NaN. words has the dtype string[pyarrow].
Arrow type names differ slightly from NumPy names. NumPy's float64 is called double in Arrow, so the dtype prints as double[pyarrow].
Converting existing data
Use astype on a column. Use convert_dtypes or dtype_backend to convert a whole frame.
import pandas as pd
df = pd.DataFrame({
"id": [1, 2, 3],
"city": ["Oslo", "Lima", "Oslo"],
"score": [0.5, None, 0.9],
})
arrow_df = df.convert_dtypes(dtype_backend="pyarrow")
print(arrow_df.dtypes)
print(df.memory_usage(deep=True).sum())
print(arrow_df.memory_usage(deep=True).sum())The dtype_backend argument exists in pandas 2.0 and later. It also works for readers, so you can get Arrow dtypes at load time without building a NumPy-backed frame first:
import io
import pandas as pd
csv = io.StringIO("id,city,score\n1,Oslo,0.5\n2,Lima,0.7\n3,Oslo,0.9\n")
df = pd.read_csv(csv, dtype_backend="pyarrow")
print(df.dtypes)The result is int64[pyarrow], string[pyarrow], and double[pyarrow]. The block manager does not merge Arrow-backed columns into shared blocks. Each one is a separate extension array.
Trade-offs and common errors
| Aspect | NumPy-backed | Arrow-backed |
|---|---|---|
| Missing values | `NaN` promotes ints to float | Validity bitmap, dtype unchanged |
| Strings | Pointers to Python objects | Contiguous bytes |
| Interop with Arrow, Parquet, Polars | Needs conversion | Often shares buffers without copying |
| Maturity | Longest tested | Newer, some operations are slower or missing |
Watch for these problems:
ImportErrorfor pyarrow. The dtypes need thepyarrowpackage, which is a separate install.- Mixing backends. An operation between
int64andint64[pyarrow]may return an Arrow-backed result, or it may convert one side. Checkdtypesafter the operation. - Assuming every method is fast. Some operations on Arrow-backed data fall back to slower element-wise paths. Time your real workload before you convert everything.
- Comparing memory without
deep=True. Usememory_usage(deep=True)for both versions of the frame, as in the example above.
When to choose Arrow-backed dtypes
Choose them when your data comes from or goes to Parquet, Arrow, or a library such as Polars. Choose them when you have many string columns, or integer columns with missing values. Keep NumPy-backed dtypes when you need the broadest compatibility with older code, or when a profiled operation is faster on NumPy.
Exercise
A column read from a CSV contains the integers 10, 20, and an empty cell. With default settings its dtype is float64. Name two ways to keep integer values, and say how the missing entry is displayed.
Show answer
Use the nullable NumPy-based extension dtype "Int64" or the Arrow-backed dtype "int64[pyarrow]". For example, pass dtype="int64[pyarrow]" to astype or readcsv, or pass dtypebackend="pyarrow" to read_csv. In both cases the missing entry is displayed as <NA>, not NaN, and the other values stay integers.