Sub topic 8
Choosing a storage backend
This chapter assumes you know what the string dtype is, how pd.NA behaves in text columns, and how to use .str methods. Here you will learn the two ways pandas can store a string column, how to select one, and how to decide between them.
Two backends, one dtype
A column with the string dtype can keep its data in one of two places. This is the storage backend.
pythonkeeps each value as a Pythonstrobject inside a NumPy object array. This is the same layout as anobjectcolumn, with thestringdtype's missing-value rules on top.pyarrowkeeps the values in an Apache Arrow array. Characters are packed into one contiguous buffer, and a separate bitmap marks which entries are missing.
Both backends give the same .str methods and use pd.NA for missing entries. They differ in memory use, speed, and one extra dependency.
Selecting a backend
Pass the storage argument to pd.StringDtype. The string aliases "string[python]" and "string[pyarrow]" do the same thing.
import pandas as pd
# The pyarrow backend requires the pyarrow package to be installed.
values = ["red", "green", None, "blue"] * 1000
py = pd.Series(values, dtype=pd.StringDtype("python"))
pa = pd.Series(values, dtype=pd.StringDtype("pyarrow"))
print(py.dtype.storage, pa.dtype.storage)
print(py.memory_usage(deep=True))
print(pa.memory_usage(deep=True))
print(py.str.upper().isna().sum(), pa.str.upper().isna().sum())The storage attribute on the dtype tells you which backend a column uses. The last line prints 1000 1000 for both columns, because missing values pass through .str.upper() the same way on each backend.
If you write plain dtype="string", pandas picks the backend from the option mode.string_storage. You can set it for a whole session:
import pandas as pd
pd.set_option("mode.string_storage", "pyarrow")
s = pd.Series(["a", "b"], dtype="string")
print(s.dtype.storage)This needs pyarrow installed. Setting the option in one place keeps a project consistent, but explicit StringDtype(...) in a function makes its behavior clear to a reader.
What differs in practice
| Concern | python | pyarrow |
|---|---|---|
| Extra dependency | None | Needs the `pyarrow` package |
| Memory | One Python object per value, plus pointers | Packed buffer, usually much smaller |
| `.str` speed | Loops over Python objects | Compiled Arrow kernels for many methods |
| Regex semantics | Python `re` | Arrow's regex engine (RE2) for some methods |
| Interop | Python objects | Zero-copy exchange with Arrow, Parquet, and other Arrow tools |
Treat speed claims as likely, not guaranteed. Not every .str method has an Arrow kernel. When one is missing, pandas may fall back to a slow path that converts values to Python objects. Measure on your own data with %timeit or memory_usage(deep=True) before choosing on speed alone.
Common errors
- ImportError when selecting pyarrow.
pd.StringDtype("pyarrow")raises an error ifpyarrowis not installed. Install it, or choosepython. - Regex differences. Arrow uses RE2, which has no lookaheads or backreferences. A pattern that works on the
pythonbackend can raise an error onpyarrow. If you need those features, test the pattern on both. - Mixing backends. Operations that combine a
pythoncolumn with apyarrowcolumn work, but the result's backend can surprise you. Convert both sides to one backend before comparing or concatenating. - Assuming object-like mutation. Treat string columns as values you replace, not objects you edit in place. Build a new Series with
.strmethods.
Decision criteria
- Is
pyarrowalready a dependency (Parquet files, Arrow data, a recent pandas)? If so, preferpyarrow. - Is the column large, with long or many distinct strings?
pyarrowusually saves memory. - Do you need regex features Arrow lacks, or must you avoid the dependency? Use
python. - Does the column have few distinct values repeated many times? Consider
category, covered in a later chapter, before either backend.
Exercise
A teammate's script runs on a laptop with pyarrow but fails with an ImportError on a minimal server image, at the line pd.Series(names, dtype=pd.StringDtype("pyarrow")). Give two ways to fix this, and say what you would check before choosing the first.
Show answer
Install pyarrow on the server, or change the code to use pd.StringDtype("python"). Before choosing the first, check that adding the dependency is acceptable for the server image, and that your regex patterns and results still behave the same on the backend you pick. The python backend works everywhere without the extra package, but it uses more memory and may be slower on large columns.