Skip to content

polars_libpostal

Parallel libpostal address parsing as a Polars expression.

import polars as pl from polars_libpostal import parse_address df = pl.DataFrame({"address": ["10 Downing St, London"]}) df.with_columns(parse_address("address").alias("p")).unnest("p") # doctest: +SKIP

Functions:

data_dir()

Find the libpostal data directory.

The first of these that exists wins: $LIBPOSTAL_DATA_DIR, an existing system install at /usr/local/share/libpostal, then the per-user directory download_data writes to.

Returns:

Type Description
Path

A directory that may not hold data yet (when nothing was found, the per-user directory is returned so download_data knows where to go).

download_data(dest=None, *, force=False, base_url=DEFAULT_BASE_URL)

Download the libpostal model data (about 3 GB on disk).

Only the archives whose folder is missing are fetched, so running it on an older install adds what is new (such as the language classifier) without downloading the rest again.

Parameters:

Name Type Description Default
dest str | PathLike[str] | None

Directory to install into. Defaults to data_dir().

None
force bool

Download every archive again, even when its folder is present.

False
base_url str

Where the archives are served from.

DEFAULT_BASE_URL

Returns:

Type Description
Path

The data directory, ready to use.

parse_address(address, *, labels=None, data_dir=None)

Parse addresses into libpostal components, in parallel.

Parameters:

Name Type Description Default
address Expr | str

String expression (or column name) holding the address text.

required
labels Sequence[str] | None

Labels to return, in output order. Defaults to all of LABELS.

None
data_dir str | PathLike[str] | None

Directory with the libpostal model data. Defaults to the result of polars_libpostal.data_dir(). The models load once per process, on the first call, from the first directory used.

None

Returns:

Type Description
Expr

Struct expression with one string field per requested label. A label that libpostal returns more than once is joined with a space. Labels that are absent, and null or empty addresses, give null fields.

Raises:

Type Description
ValueError

If labels is empty, repeats a label, or names a label that libpostal does not have.

Notes

libpostal ignores the country when parsing, so there is no country argument: the same address parses the same way everywhere.

parse_address_list(address, *, data_dir=None)

Parse addresses into libpostal components, in address order, in parallel.

Unlike parse_address, this keeps libpostal's own response: one element per component, in the order the components appear in the address text, with repeated labels left as separate elements.

Parameters:

Name Type Description Default
address Expr | str

String expression (or column name) holding the address text.

required
data_dir str | PathLike[str] | None

Directory with the libpostal model data, as for parse_address.

None

Returns:

Type Description
Expr

Expression of type List(Struct({"label": String, "value": String})). Labels are those in LABELS. Null or empty addresses give null.

Examples:

Fold every label except a few into one column, in address order:

>>> kept = ["house", "house_number", "road", "unit", "level", "po_box"]
>>> label = pl.element().struct.field("label")
>>> value = pl.element().struct.field("value")
>>> df.with_columns(
...     parse_address_list("address")
...     .list.eval(value.filter(~label.is_in(kept)))
...     .list.join(" ")
...     .alias("other")
... )

Get one row per component, for grouping or joining:

>>> df.with_columns(parts=parse_address_list("address")).explode(
...     "parts"
... ).unnest("parts")

expand_address(address, *, languages=None, data_dir=None)

Expand addresses into libpostal's normalized forms, in parallel.

Each address becomes the list of strings libpostal considers equivalent spellings of it: abbreviations spelled out ("St" gives "street" and "saint"), numbers written as digits, accents and punctuation normalized, and so on. Two addresses that share an expansion are likely the same place, which makes this useful for matching and deduplication.

Parameters:

Name Type Description Default
address Expr | str

String expression (or column name) holding the address text.

required
languages Sequence[str] | None

Language codes to expand for, such as ["en"] or ["fr", "de"]. By default libpostal's language classifier picks them for each address. Naming them is faster, avoids wrong guesses on short inputs, and does not need the language classifier data.

None
data_dir str | PathLike[str] | None

Directory with the libpostal model data, as for parse_address. Expansion does not load the parser model.

None

Returns:

Type Description
Expr

Expression of type List(String), in libpostal's order, without repeats. Null or empty addresses give null.

Raises:

Type Description
ValueError

If languages is empty, repeats a code, or holds something that is not a 2 or 3 letter lowercase language code.

Examples:

>>> df.with_columns(
...     expanded=expand_address("address", languages=["en"])
... )

Match two tables on any shared expansion:

>>> def keyed(df: pl.DataFrame) -> pl.DataFrame:
...     return df.with_columns(key=expand_address("address")).explode("key")
>>> keyed(left).join(keyed(right), on="key").unique(
...     ["left_id", "right_id"]
... )