Sub topic 11
Arrow-backed dtypes
This chapter assumes you have read the earlier chapters on NumPy dtypes, the object dtype, missing values, extension dtypes with pd.NA, and memory measurement. You also need pandas 2.0 or later and the pyarrow package installed. Version notes appear where behavior differs.
What "Arrow-backed" means
Apache Arrow is a specification for how columnar data sits in memory. The pyarrow library implements it. An Arrow-backed pandas column keeps its values in an Arrow array instead of a NumPy array. pandas wraps that array in an extension array class, ArrowExtensionArray, and describes it with pd.ArrowDtype.
Arrow arrays differ from NumPy arrays in three ways that matter for pandas:
- Validity bitmap. Every Arrow array can carry one bit per value that marks it as present or missing. Missing values need no sentinel such as
NaN, so an integer column stays an integer column. - Variable-width types. Strings are stored as one contiguous data buffer plus an offsets buffer. There is no Python object per string.
- Rich types. Arrow has native decimals, lists, structs, dates, durations, and timestamps with units. NumPy has no equivalent for most of these.
Arrow-backed dtypes are an opt-in extension dtype family. They sit beside NumPy dtypes and the pandas masked dtypes such as Int64.
Creating Arrow-backed columns
You can name the dtype with a string suffix [pyarrow], or build it from a pyarrow type with pd.ArrowDtype. Both forms give the same dtype.
import pandas as pd
import pyarrow as pa
s = pd.Series([1, 2, None], dtype="int64[pyarrow]")
print(s.dtype)
print(s)
s2 = pd.Series([1, 2, None], dtype=pd.ArrowDtype(pa.int64()))
print(s2.dtype == s.dtype)
print(s.isna().tolist())The column keeps int64[pyarrow] even though a value is missing. The missing entry prints as <NA>, and isna() returns [False, False, True]. Compare this with the missing-values chapter: a default integer column would have become float64 with NaN.
The name of the dtype follows the Arrow type, not the NumPy name. A 64-bit float is double[pyarrow], and a boolean is bool[pyarrow]. Other examples are string[pyarrow], timestamp[ns][pyarrow], and decimal128(10, 2)[pyarrow].
Comparing the three ways to store a missing integer
You now have three ways to hold an integer column with a gap. Each stores the gap differently.
| Dtype | Values stored as | Missing value marked by | Approximate cost per value |
|---|---|---|---|
| `float64` (default) | NumPy float array | `NaN` inside the values | 8 bytes |
| `Int64` (pandas masked) | NumPy int64 array plus a NumPy bool mask | The mask | 8 bytes plus 1 byte |
| `int64[pyarrow]` | Arrow int64 buffer plus validity bitmap | One bit in the bitmap | 8 bytes plus 1 bit |
The Arrow column is slightly smaller than the masked column because a bit is smaller than a byte. For a 10-million-row column this saves roughly 9 MB. That is rarely the reason to choose Arrow. The bigger gains come from strings and from interoperability.
Strings: where Arrow pays off
The object dtype stores one 8-byte pointer per row, and each pointer leads to a separate Python str object. That is why memory_usage(deep=True) is much larger than the shallow figure. An Arrow string column avoids this layout. It stores all the characters in one buffer and uses an offsets array to mark where each string begins.
The next example measures the same repeated city names as object, string[pyarrow], and category.
import pandas as pd
cities = ["Oslo", "Lima", "Pune"] * 100_000
obj = pd.Series(cities, dtype="object")
arrow = pd.Series(cities, dtype="string[pyarrow]")
cat = obj.astype("category")
for name, s in [("object", obj), ("string[pyarrow]", arrow), ("category", cat)]:
print(name, s.memory_usage(index=False, deep=True))For 300,000 short strings, the object column costs about 18 MB with deep=True: 2.4 MB of pointers plus roughly 50 bytes per Python string. The Arrow column costs about 2.4 MB: 4 bytes of offset per row and 4 bytes of character data per row. The categorical column is smallest, near 0.3 MB, because it stores each distinct string once and one int8 code per row.
This shows how to choose between them:
- Use
categorywhen the column has few distinct values that repeat many times. - Use
string[pyarrow]when strings are mostly distinct, such as names, URLs, or free text. A categorical would store nearly every string anyway and add a codes array.
Arrow memory figures are also easier to trust. ArrowExtensionArray reports its real buffer size, so the shallow and deep=True figures agree for Arrow columns. The object column is the one where the shallow figure misleads.
Loading data straight into Arrow dtypes
The best time to choose Arrow dtypes is at load time, which matches the earlier chapter on setting dtypes while reading files. The dtype_backend argument tells readers which family of nullable dtypes to produce.
import io
import pandas as pd
csv = io.StringIO("id,city,score\n1,Oslo,0.5\n2,,0.7\n3,Oslo,\n")
df = pd.read_csv(csv, dtype_backend="pyarrow")
print(df.dtypes)
print(df.isna().sum())The dtypes come out as int64[pyarrow], string[pyarrow], and double[pyarrow]. The empty city and the empty score are both <NA>. isna().sum() reports one missing value in each of city and score.
read_csv also accepts engine="pyarrow", which uses the Arrow CSV parser. This is a separate choice. engine="pyarrow" picks the parser. dtype_backend="pyarrow" picks the resulting dtypes. You can use either one without the other. The Arrow parser is multithreaded and often faster on large files, but it supports fewer options than the default C engine.
For a frame you already hold, use convert_dtypes or astype:
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3], "b": ["x", "y", None]})
arrow_df = df.convert_dtypes(dtype_backend="pyarrow")
print(arrow_df.dtypes)
# Convert one column explicitly.
df["a"] = df["a"].astype("int32[pyarrow]")
print(df.dtypes)convert_dtypes infers the best Arrow type per column. Use astype when you want to pick the width yourself, for example int32[pyarrow] instead of int64[pyarrow]. Downcasting works the same way as before: pick the smallest type that holds your values.
Types NumPy cannot express
Arrow brings nested and exact types into a DataFrame column. A list column is one example:
import pandas as pd
import pyarrow as pa
tags = pd.Series(
[["a", "b"], ["c"], None],
dtype=pd.ArrowDtype(pa.list_(pa.string())),
)
print(tags.dtype)
print(tags.list.len())The dtype is list<item: string>[pyarrow]. The .list accessor needs pandas 2.2 or later. It returns 2, 1, and <NA> for the three rows. With an object column, you would run Python code per row to get the same result.
Other useful types:
pa.decimal128(precision, scale)holds exact decimal values, which suits money. It does not suffer from binary float rounding.pa.timestamp("us")andpa.date32()hold timestamps and dates with explicit units.pa.dictionary(pa.int32(), pa.string())is Arrow's own categorical. pandas shows it as a dictionary-typedArrowDtype. It is not the same thing as pandascategory, and the two have different APIs.
Gotchas and failure modes
Arrow columns are not consolidated into blocks
Earlier you saw that the block manager merges same-dtype NumPy columns into one two-dimensional block. Extension arrays, including Arrow arrays, do not take part in that merge. Each Arrow column lives in its own block. A frame with 200 int64[pyarrow] columns therefore has 200 blocks. That is harmless for most work, but operations that go across many columns, such as df.sum(axis=1), cannot treat the frame as one NumPy matrix.
Conversion to NumPy may produce `object`
to_numpy() on an Arrow column with missing values returns an object array holding pd.NA. You usually want a numeric array instead. Tell pandas how to fill the gaps:
import numpy as np
import pandas as pd
s = pd.Series([1.5, None, 3.0], dtype="double[pyarrow]")
arr = s.to_numpy(dtype="float64", na_value=np.nan)
print(arr, arr.dtype)This prints [1.5 nan 3. ] float64. Libraries that expect NumPy input, such as older scikit-learn code, may hit this conversion. Convert at the boundary and keep Arrow dtypes inside your pandas code.
Item assignment is expensive
Arrow arrays are immutable and are stored as chunked arrays. Setting values in an Arrow-backed column generally builds a new array rather than writing in place. A loop that assigns single cells will be slow. Prefer vectorized operations such as where, mask, or whole-column assignment. This also fits the Copy-on-Write rules from the next chapter's perspective: treat each column as a value you replace, not a buffer you edit.
Semantics differ from NumPy dtypes
- Comparisons with
pd.NAreturn<NA>, notFalse. In a boolean mask,NArows can raise errors or be dropped, depending on the operation. Usefillna(False)first if you need a plain mask. NaNand missing are different in Arrow. Adouble[pyarrow]column can hold both aNaNvalue and a missing entry.isna()treats both as missing in pandas, but the stored data is different.- Some operations are not implemented natively for every Arrow type. pandas may fall back to a slower path or raise
NotImplementedError. Test the operations you depend on. - Integer overflow on
int8[pyarrow]and similar types raises an error in arithmetic rather than silently wrapping. This is safer, but it can surprise code that expects NumPy behavior.
Dependencies and portability
Arrow dtypes require pyarrow, which is a large binary dependency. Pickled frames with Arrow dtypes load only where pyarrow is installed. Check deployment images and serverless environments before adopting these dtypes.
When to choose Arrow-backed dtypes
Choose them when:
- Your data has many distinct strings, and
objectmemory is the bottleneck. - You pass data between pandas and tools that use Arrow, such as Parquet readers, Polars, DuckDB, or Spark. Conversion between these can often avoid copying.
- You need nested, decimal, or unit-aware time types.
- You want one consistent missing-value marker across integer, float, string, and boolean columns.
Stay with NumPy dtypes when:
- Columns are dense numeric data with no missing values. NumPy
float64andint64are well tested, and block consolidation helps row-wise work. - You depend on the broadest library compatibility.
- Your workload edits cells in place.
Exercise
A DataFrame has a column user_id of distinct 12-character strings (1 million rows, no repeats) and a column country with 30 distinct values. Which dtype would you choose for each column to minimize memory, and why? Also name the argument you would pass to pd.read_csv to load both columns as Arrow-backed dtypes in one step.
Show answer
Use string[pyarrow] for user_id. Every value is distinct, so a categorical would store all 1 million strings plus a codes array, and object would add one Python string object per row. Arrow stores the characters in one buffer plus offsets. Use category for country. With only 30 distinct values, each string is stored once and each row costs one int8 code. To load Arrow-backed dtypes in one step, pass dtype_backend="pyarrow" to pd.read_csv. Passing engine="pyarrow" instead only changes the parser, not the resulting dtypes.