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(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 |
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 |
None
|
data_dir
|
str | PathLike[str] | None
|
Directory with the libpostal model data. Defaults to the result of
|
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 |
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 |
None
|
Returns:
| Type | Description |
|---|---|
Expr
|
Expression of type |
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:
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 |
None
|
data_dir
|
str | PathLike[str] | None
|
Directory with the libpostal model data, as for |
None
|
Returns:
| Type | Description |
|---|---|
Expr
|
Expression of type |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
Match two tables on any shared expansion: