Skip to content

API reference

Generated from the source, so it cannot drift from the code.

Expressions

Countries — ISO 3166-1

Every country expression takes a string column and the same three keywords:

  • fuzzy — whether an exact miss may fall back to pycountry's search_fuzzy. Off by default; see Matching.
  • historic — whether ISO 3166-3 is consulted after ISO 3166-1. Off by default.
  • parallel — whether the plugin may split large columns across rayon threads. Columns below 100k rows run single-threaded regardless.

extract

extract(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr

Resolve a country and return every ISO field as a struct.

One pass produces all of them, so this is cheaper than calling several single-field expressions on the same column.

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of country names, codes, or flag emoji.

required
fuzzy bool

Whether an exact miss may fall back to pycountry's search_fuzzy, which also reaches countries through their subdivisions -- "Texas" resolves to US. Off by default because it is a guess, and a wrong guess is harder to notice than a null.

False
historic bool

Whether ISO 3166-3 (countries withdrawn from the standard) is consulted when ISO 3166-1 has no match. Current countries always win, so a reused code like AI still resolves to Anguilla.

False
parallel bool

Whether the plugin may split large columns across rayon threads. Columns below 100k rows run single-threaded regardless.

True

Returns:

Type Description
Expr

Struct of alpha_2, alpha_3, alpha_4, numeric, name, official_name, common_name, flag, withdrawal_date and historic. Every field is null when nothing matched. alpha_4 and withdrawal_date exist only in ISO 3166-3, so they are populated only for a historic match; they are present in the schema either way, so a keyword never changes the struct's shape.

Source code in python/polars_pycountry/_expr.py
def extract(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Resolve a country and return every ISO field as a struct.

    One pass produces all of them, so this is cheaper than calling several
    single-field expressions on the same column.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of country names, codes, or flag emoji.
    fuzzy : bool, default False
        Whether an exact miss may fall back to `pycountry`'s `search_fuzzy`,
        which also reaches countries through their subdivisions -- `"Texas"`
        resolves to `US`. Off by default because it is a guess, and a wrong
        guess is harder to notice than a null.
    historic : bool, default False
        Whether ISO 3166-3 (countries withdrawn from the standard) is
        consulted when ISO 3166-1 has no match. Current countries always win,
        so a reused code like `AI` still resolves to Anguilla.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.
        Columns below 100k rows run single-threaded regardless.

    Returns
    -------
    pl.Expr
        Struct of `alpha_2`, `alpha_3`, `alpha_4`, `numeric`, `name`,
        `official_name`, `common_name`, `flag`, `withdrawal_date` and
        `historic`. Every field is null when nothing matched. `alpha_4` and
        `withdrawal_date` exist only in ISO 3166-3, so they are populated only
        for a historic match; they are present in the schema either way, so a
        keyword never changes the struct's shape.
    """
    return _country_plugin(
        "country_extract",
        expr,
        fuzzy=fuzzy,
        historic=historic,
        parallel=parallel,
    )

match

match(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr

Report how each value resolved, not just what it resolved to.

This is the expression to reach for when auditing a messy column: it says which field matched, whether the answer came from the fuzzy search, and how confident that search was.

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of country names, codes, or flag emoji.

required
fuzzy bool

Whether an exact miss may fall back to the fuzzy search.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Struct of alpha_2, matched_on, score and historic. matched_on names the ISO field that matched -- "alpha_2", "name", "numeric" and so on -- or "fuzzy" when the fuzzy search produced the answer. score carries the fuzzy points and is null for an exact match, so score.is_null() separates the answers you can trust from the ones worth reviewing.

Source code in python/polars_pycountry/_expr.py
def match(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Report *how* each value resolved, not just what it resolved to.

    This is the expression to reach for when auditing a messy column: it says
    which field matched, whether the answer came from the fuzzy search, and
    how confident that search was.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of country names, codes, or flag emoji.
    fuzzy : bool, default False
        Whether an exact miss may fall back to the fuzzy search.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Struct of `alpha_2`, `matched_on`, `score` and `historic`.
        `matched_on` names the ISO field that matched -- `"alpha_2"`,
        `"name"`, `"numeric"` and so on -- or `"fuzzy"` when the fuzzy search
        produced the answer. `score` carries the fuzzy points and is null for
        an exact match, so `score.is_null()` separates the answers you can
        trust from the ones worth reviewing.
    """
    return _country_plugin(
        "country_match",
        expr,
        fuzzy=fuzzy,
        historic=historic,
        parallel=parallel,
    )

alpha_2

alpha_2(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the ISO 3166-1 alpha-2 code, e.g. "US".

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of country names, codes, or flag emoji.

required
fuzzy bool

Whether an exact miss may fall back to the fuzzy search.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of alpha-2 codes, null where nothing matched.

Source code in python/polars_pycountry/_expr.py
def alpha_2(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the ISO 3166-1 alpha-2 code, e.g. `"US"`.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of country names, codes, or flag emoji.
    fuzzy : bool, default False
        Whether an exact miss may fall back to the fuzzy search.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of alpha-2 codes, null where nothing matched.
    """
    return _country_plugin(
        "country_alpha_2",
        expr,
        fuzzy=fuzzy,
        historic=historic,
        parallel=parallel,
    )

alpha_3

alpha_3(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the ISO 3166-1 alpha-3 code, e.g. "USA".

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of country names, codes, or flag emoji.

required
fuzzy bool

Whether an exact miss may fall back to the fuzzy search.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of alpha-3 codes, null where nothing matched.

Source code in python/polars_pycountry/_expr.py
def alpha_3(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the ISO 3166-1 alpha-3 code, e.g. `"USA"`.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of country names, codes, or flag emoji.
    fuzzy : bool, default False
        Whether an exact miss may fall back to the fuzzy search.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of alpha-3 codes, null where nothing matched.
    """
    return _country_plugin(
        "country_alpha_3",
        expr,
        fuzzy=fuzzy,
        historic=historic,
        parallel=parallel,
    )

numeric

numeric(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the ISO 3166-1 numeric code, e.g. "840".

The result is Utf8 rather than an integer because the standard's codes are three digits wide and "004" is not "4". Cast it yourself if you want the number; going the other way, an integer column needs pl.col("n").cast(pl.String).str.zfill(3) before it will match.

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of country names, codes, or flag emoji.

required
fuzzy bool

Whether an exact miss may fall back to the fuzzy search.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of zero-padded numeric codes, null where nothing matched.

Source code in python/polars_pycountry/_expr.py
def numeric(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the ISO 3166-1 numeric code, e.g. `"840"`.

    The result is Utf8 rather than an integer because the standard's codes are
    three digits wide and `"004"` is not `"4"`. Cast it yourself if you want
    the number; going the other way, an integer column needs
    `pl.col("n").cast(pl.String).str.zfill(3)` before it will match.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of country names, codes, or flag emoji.
    fuzzy : bool, default False
        Whether an exact miss may fall back to the fuzzy search.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of zero-padded numeric codes, null where nothing matched.
    """
    return _country_plugin(
        "country_numeric",
        expr,
        fuzzy=fuzzy,
        historic=historic,
        parallel=parallel,
    )

name

name(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the country's short name, e.g. "United States".

This is the standard's name field, which is the one to group or join on. It is not always the name people use: the short name of BO is "Bolivia, Plurinational State of", and common_name holds "Bolivia".

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of country names, codes, or flag emoji.

required
fuzzy bool

Whether an exact miss may fall back to the fuzzy search.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of short names, null where nothing matched.

Source code in python/polars_pycountry/_expr.py
def name(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the country's short name, e.g. `"United States"`.

    This is the standard's `name` field, which is the one to group or join on.
    It is not always the name people use: the short name of `BO` is
    `"Bolivia, Plurinational State of"`, and `common_name` holds `"Bolivia"`.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of country names, codes, or flag emoji.
    fuzzy : bool, default False
        Whether an exact miss may fall back to the fuzzy search.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of short names, null where nothing matched.
    """
    return _country_plugin(
        "country_name", expr, fuzzy=fuzzy, historic=historic, parallel=parallel
    )

official_name

official_name(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the official name, e.g. "United States of America".

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of country names, codes, or flag emoji.

required
fuzzy bool

Whether an exact miss may fall back to the fuzzy search.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column, null where the country has no official name distinct from its short name -- most do not.

Source code in python/polars_pycountry/_expr.py
def official_name(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the official name, e.g. `"United States of America"`.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of country names, codes, or flag emoji.
    fuzzy : bool, default False
        Whether an exact miss may fall back to the fuzzy search.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column, **null** where the country has no official name distinct
        from its short name -- most do not.
    """
    return _country_plugin(
        "country_official_name",
        expr,
        fuzzy=fuzzy,
        historic=historic,
        parallel=parallel,
    )

common_name

common_name(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the common name, e.g. "Bolivia" for BO.

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of country names, codes, or flag emoji.

required
fuzzy bool

Whether an exact miss may fall back to the fuzzy search.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column, null for the great majority of countries, whose common name is their short name. pl.coalesce(common_name(c), name(c)) is the usual way to get a display name.

Source code in python/polars_pycountry/_expr.py
def common_name(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the common name, e.g. `"Bolivia"` for `BO`.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of country names, codes, or flag emoji.
    fuzzy : bool, default False
        Whether an exact miss may fall back to the fuzzy search.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column, **null** for the great majority of countries, whose
        common name is their short name. `pl.coalesce(common_name(c), name(c))`
        is the usual way to get a display name.
    """
    return _country_plugin(
        "country_common_name",
        expr,
        fuzzy=fuzzy,
        historic=historic,
        parallel=parallel,
    )

flag

flag(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the flag emoji, e.g. "🇺🇸" for US.

The emoji is a pair of regional indicator symbols, which is also a value the lookup accepts as input.

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of country names, codes, or flag emoji.

required
fuzzy bool

Whether an exact miss may fall back to the fuzzy search.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of flag emoji, null where nothing matched. Withdrawn countries have no flag, so a historic match is null here.

Source code in python/polars_pycountry/_expr.py
def flag(
    expr: IntoExprColumn,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the flag emoji, e.g. `"\U0001f1fa\U0001f1f8"` for `US`.

    The emoji is a pair of regional indicator symbols, which is also a value
    the lookup accepts as *input*.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of country names, codes, or flag emoji.
    fuzzy : bool, default False
        Whether an exact miss may fall back to the fuzzy search.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of flag emoji, null where nothing matched. Withdrawn
        countries have no flag, so a historic match is null here.
    """
    return _country_plugin(
        "country_flag", expr, fuzzy=fuzzy, historic=historic, parallel=parallel
    )

Subdivisions — ISO 3166-2

Each takes an optional country to scope the search by, which you should pass whenever you have it — subdivision names are not unique across the standard.

subdivision

subdivision(
    expr: IntoExprColumn,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr

Resolve an ISO 3166-2 subdivision and return every field as a struct.

Pass country whenever you have it. Subdivision names are not unique across the standard -- "Central" names a region in a dozen countries, and "Georgia" is both a US state and a country -- so an unscoped name resolves to whichever record ISO lists first. With a country the answer is well defined.

The value may be a full code ("US-CA"), a bare code when a country is given ("CA"), or a name ("California").

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of subdivision codes or names.

required
country IntoExprColumn | None

The country to search within. Follows the usual Polars convention, so a bare str names a column; a fixed country is pl.lit("US"). Whichever form, the value may be anything the country lookup accepts -- "US", "USA", "840", "United States". A country that does not itself resolve yields null rather than falling back to a global search, so a bad scope cannot quietly return another country's state.

None
fuzzy bool

Whether to fall back to accent-folded and then partial name matches. The country scope is resolved with the same setting.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Struct of code, name, type, country_code and parent_code. Every field is null when nothing matched; parent_code is null for top-level subdivisions.

Source code in python/polars_pycountry/_expr.py
def subdivision(
    expr: IntoExprColumn,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Resolve an ISO 3166-2 subdivision and return every field as a struct.

    Pass `country` whenever you have it. Subdivision names are not unique
    across the standard -- `"Central"` names a region in a dozen countries,
    and `"Georgia"` is both a US state and a country -- so an unscoped name
    resolves to whichever record ISO lists first. With a country the answer is
    well defined.

    The value may be a full code (`"US-CA"`), a bare code when a country is
    given (`"CA"`), or a name (`"California"`).

    Parameters
    ----------
    expr : IntoExprColumn
        String column of subdivision codes or names.
    country : IntoExprColumn | None, optional
        The country to search within. Follows the usual Polars convention, so
        a bare `str` names a **column**; a fixed country is `pl.lit("US")`.
        Whichever form, the value may be anything the country lookup accepts
        -- `"US"`, `"USA"`, `"840"`, `"United States"`. A country that does
        not itself resolve yields null rather than falling back to a global
        search, so a bad scope cannot quietly return another country's state.
    fuzzy : bool, default False
        Whether to fall back to accent-folded and then partial name matches.
        The country scope is resolved with the same setting.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Struct of `code`, `name`, `type`, `country_code` and `parent_code`.
        Every field is null when nothing matched; `parent_code` is null for
        top-level subdivisions.
    """
    return _subdivision_plugin(
        "subdivision_extract", expr, country, fuzzy=fuzzy, parallel=parallel
    )

subdivision_code

subdivision_code(
    expr: IntoExprColumn,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the ISO 3166-2 code, e.g. "US-CA".

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of subdivision codes or names.

required
country IntoExprColumn | None

The country to scope the search to. A bare str names a column, per the usual Polars convention; use pl.lit("US") for a fixed country.

None
fuzzy bool

Whether to fall back to accent-folded and partial name matches.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of subdivision codes, null where nothing matched.

Source code in python/polars_pycountry/_expr.py
def subdivision_code(
    expr: IntoExprColumn,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the ISO 3166-2 code, e.g. `"US-CA"`.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of subdivision codes or names.
    country : IntoExprColumn | None, optional
        The country to scope the search to. A bare `str` names a column, per
        the usual Polars convention; use `pl.lit("US")` for a fixed country.
    fuzzy : bool, default False
        Whether to fall back to accent-folded and partial name matches.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of subdivision codes, null where nothing matched.
    """
    return _subdivision_plugin(
        "subdivision_code", expr, country, fuzzy=fuzzy, parallel=parallel
    )

subdivision_name

subdivision_name(
    expr: IntoExprColumn,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the subdivision's name, e.g. "California".

This is the standard's spelling, which is how it normalizes a messy column: "CA", "calif." and "California" all land on one string.

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of subdivision codes or names.

required
country IntoExprColumn | None

The country to scope the search to. A bare str names a column, per the usual Polars convention; use pl.lit("US") for a fixed country.

None
fuzzy bool

Whether to fall back to accent-folded and partial name matches.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of subdivision names, null where nothing matched.

Source code in python/polars_pycountry/_expr.py
def subdivision_name(
    expr: IntoExprColumn,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the subdivision's name, e.g. `"California"`.

    This is the standard's spelling, which is how it normalizes a messy
    column: `"CA"`, `"calif."` and `"California"` all land on one string.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of subdivision codes or names.
    country : IntoExprColumn | None, optional
        The country to scope the search to. A bare `str` names a column, per
        the usual Polars convention; use `pl.lit("US")` for a fixed country.
    fuzzy : bool, default False
        Whether to fall back to accent-folded and partial name matches.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of subdivision names, null where nothing matched.
    """
    return _subdivision_plugin(
        "subdivision_name", expr, country, fuzzy=fuzzy, parallel=parallel
    )

subdivision_type

subdivision_type(
    expr: IntoExprColumn,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the subdivision's type, e.g. "State" or "Province".

The standard uses 100-odd distinct types, and they are English words chosen per country rather than a closed vocabulary -- "Province", "Autonomous province" and "Overseas territorial collectivity" are all in there.

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of subdivision codes or names.

required
country IntoExprColumn | None

The country to scope the search to. A bare str names a column, per the usual Polars convention; use pl.lit("US") for a fixed country.

None
fuzzy bool

Whether to fall back to accent-folded and partial name matches.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of subdivision types, null where nothing matched.

Source code in python/polars_pycountry/_expr.py
def subdivision_type(
    expr: IntoExprColumn,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the subdivision's type, e.g. `"State"` or `"Province"`.

    The standard uses 100-odd distinct types, and they are English words
    chosen per country rather than a closed vocabulary -- `"Province"`,
    `"Autonomous province"` and `"Overseas territorial collectivity"` are all
    in there.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of subdivision codes or names.
    country : IntoExprColumn | None, optional
        The country to scope the search to. A bare `str` names a column, per
        the usual Polars convention; use `pl.lit("US")` for a fixed country.
    fuzzy : bool, default False
        Whether to fall back to accent-folded and partial name matches.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of subdivision types, null where nothing matched.
    """
    return _subdivision_plugin(
        "subdivision_type", expr, country, fuzzy=fuzzy, parallel=parallel
    )

subdivision_country

subdivision_country(
    expr: IntoExprColumn,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the alpha-2 code of the country a subdivision belongs to.

Useful without a country scope: it answers "which country is this state in?" for a column that holds only subdivision codes.

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of subdivision codes or names.

required
country IntoExprColumn | None

The country to scope the search to. A bare str names a column, per the usual Polars convention; use pl.lit("US") for a fixed country.

None
fuzzy bool

Whether to fall back to accent-folded and partial name matches.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of alpha-2 codes, null where nothing matched.

Source code in python/polars_pycountry/_expr.py
def subdivision_country(
    expr: IntoExprColumn,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the alpha-2 code of the country a subdivision belongs to.

    Useful without a `country` scope: it answers "which country is this state
    in?" for a column that holds only subdivision codes.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of subdivision codes or names.
    country : IntoExprColumn | None, optional
        The country to scope the search to. A bare `str` names a column, per
        the usual Polars convention; use `pl.lit("US")` for a fixed country.
    fuzzy : bool, default False
        Whether to fall back to accent-folded and partial name matches.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of alpha-2 codes, null where nothing matched.
    """
    return _subdivision_plugin(
        "subdivision_country", expr, country, fuzzy=fuzzy, parallel=parallel
    )

subdivision_parent_code

subdivision_parent_code(
    expr: IntoExprColumn,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the parent subdivision's code, where there is one.

Subdivisions nest: a French department sits inside a region, and both are ISO 3166-2 records. The parent code carries the country prefix even where the standard's raw data omits it, so it can be joined straight back against subdivision_code.

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of subdivision codes or names.

required
country IntoExprColumn | None

The country to scope the search to. A bare str names a column, per the usual Polars convention; use pl.lit("US") for a fixed country.

None
fuzzy bool

Whether to fall back to accent-folded and partial name matches.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of parent codes, null for a top-level subdivision as well as for no match.

Source code in python/polars_pycountry/_expr.py
def subdivision_parent_code(
    expr: IntoExprColumn,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the parent subdivision's code, where there is one.

    Subdivisions nest: a French department sits inside a region, and both are
    ISO 3166-2 records. The parent code carries the country prefix even where
    the standard's raw data omits it, so it can be joined straight back
    against `subdivision_code`.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of subdivision codes or names.
    country : IntoExprColumn | None, optional
        The country to scope the search to. A bare `str` names a column, per
        the usual Polars convention; use `pl.lit("US")` for a fixed country.
    fuzzy : bool, default False
        Whether to fall back to accent-folded and partial name matches.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of parent codes, **null** for a top-level subdivision as
        well as for no match.
    """
    return _subdivision_plugin(
        "subdivision_parent_code",
        expr,
        country,
        fuzzy=fuzzy,
        parallel=parallel,
    )

Currencies — ISO 4217

Exact matching only; there is no fuzzy path, because pycountry has none to match.

currency

currency(expr: IntoExprColumn, *, parallel: bool = True) -> pl.Expr

Resolve an ISO 4217 currency and return every field as a struct.

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of currency codes, numbers, or names.

required
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Struct of alpha_3, numeric and name, all null when nothing matched.

Source code in python/polars_pycountry/_expr.py
def currency(expr: IntoExprColumn, *, parallel: bool = True) -> pl.Expr:
    """Resolve an ISO 4217 currency and return every field as a struct.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of currency codes, numbers, or names.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Struct of `alpha_3`, `numeric` and `name`, all null when nothing
        matched.
    """
    return _currency_plugin("currency_extract", expr, parallel=parallel)

currency_alpha_3

currency_alpha_3(expr: IntoExprColumn, *, parallel: bool = True) -> pl.Expr

Extract the ISO 4217 alpha-3 code, e.g. "USD".

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of currency codes, numbers, or names.

required
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of currency codes, null where nothing matched.

Source code in python/polars_pycountry/_expr.py
def currency_alpha_3(
    expr: IntoExprColumn, *, parallel: bool = True
) -> pl.Expr:
    """Extract the ISO 4217 alpha-3 code, e.g. `"USD"`.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of currency codes, numbers, or names.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of currency codes, null where nothing matched.
    """
    return _currency_plugin("currency_alpha_3", expr, parallel=parallel)

currency_numeric

currency_numeric(expr: IntoExprColumn, *, parallel: bool = True) -> pl.Expr

Extract the ISO 4217 numeric code, e.g. "840".

Utf8 for the same reason as the country code: the standard's codes are three digits wide and the leading zero is part of the value.

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of currency codes, numbers, or names.

required
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of zero-padded numeric codes, null where nothing matched.

Source code in python/polars_pycountry/_expr.py
def currency_numeric(
    expr: IntoExprColumn, *, parallel: bool = True
) -> pl.Expr:
    """Extract the ISO 4217 numeric code, e.g. `"840"`.

    Utf8 for the same reason as the country code: the standard's codes are
    three digits wide and the leading zero is part of the value.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of currency codes, numbers, or names.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of zero-padded numeric codes, null where nothing matched.
    """
    return _currency_plugin("currency_numeric", expr, parallel=parallel)

currency_name

currency_name(expr: IntoExprColumn, *, parallel: bool = True) -> pl.Expr

Extract the currency's name, e.g. "US Dollar".

Parameters:

Name Type Description Default
expr IntoExprColumn

String column of currency codes, numbers, or names.

required
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of currency names, null where nothing matched.

Source code in python/polars_pycountry/_expr.py
def currency_name(expr: IntoExprColumn, *, parallel: bool = True) -> pl.Expr:
    """Extract the currency's name, e.g. `"US Dollar"`.

    Parameters
    ----------
    expr : IntoExprColumn
        String column of currency codes, numbers, or names.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of currency names, null where nothing matched.
    """
    return _currency_plugin("currency_name", expr, parallel=parallel)

The .country namespace

Importing polars_pycountry registers this, so pl.col("c").country.alpha_2() works without importing anything else. Each method mirrors the module-level function of the same name.

CountryNamespace

CountryNamespace(expr: Expr)

ISO 3166 and ISO 4217 lookup, as pl.col(...).country.*.

Bind the namespace to an expression.

Parameters:

Name Type Description Default
expr Expr

The string expression the namespace methods operate on.

required
Source code in python/polars_pycountry/_namespace.py
def __init__(self, expr: pl.Expr) -> None:
    """Bind the namespace to an expression.

    Parameters
    ----------
    expr : pl.Expr
        The string expression the namespace methods operate on.
    """
    self._expr = expr

extract

extract(
    *, fuzzy: bool = False, historic: bool = False, parallel: bool = True
) -> pl.Expr

Resolve a country and return every ISO field as a struct.

Parameters:

Name Type Description Default
fuzzy bool

Whether an exact miss may fall back to pycountry's search_fuzzy.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Struct of every ISO 3166 field, plus historic.

Source code in python/polars_pycountry/_namespace.py
def extract(
    self,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Resolve a country and return every ISO field as a struct.

    Parameters
    ----------
    fuzzy : bool, default False
        Whether an exact miss may fall back to `pycountry`'s
        `search_fuzzy`.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Struct of every ISO 3166 field, plus `historic`.
    """
    return _expr.extract(
        self._expr, fuzzy=fuzzy, historic=historic, parallel=parallel
    )

match

match(
    *, fuzzy: bool = False, historic: bool = False, parallel: bool = True
) -> pl.Expr

Report how each value resolved, not just what it resolved to.

Parameters:

Name Type Description Default
fuzzy bool

Whether an exact miss may fall back to pycountry's search_fuzzy.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Struct of alpha_2, matched_on, score and historic.

Source code in python/polars_pycountry/_namespace.py
def match(
    self,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Report *how* each value resolved, not just what it resolved to.

    Parameters
    ----------
    fuzzy : bool, default False
        Whether an exact miss may fall back to `pycountry`'s
        `search_fuzzy`.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Struct of `alpha_2`, `matched_on`, `score` and `historic`.
    """
    return _expr.match(
        self._expr, fuzzy=fuzzy, historic=historic, parallel=parallel
    )

alpha_2

alpha_2(
    *, fuzzy: bool = False, historic: bool = False, parallel: bool = True
) -> pl.Expr

Extract the ISO 3166-1 alpha-2 code, e.g. "US".

Parameters:

Name Type Description Default
fuzzy bool

Whether an exact miss may fall back to pycountry's search_fuzzy.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of alpha-2 codes, null where nothing matched.

Source code in python/polars_pycountry/_namespace.py
def alpha_2(
    self,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the ISO 3166-1 alpha-2 code, e.g. `"US"`.

    Parameters
    ----------
    fuzzy : bool, default False
        Whether an exact miss may fall back to `pycountry`'s
        `search_fuzzy`.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of alpha-2 codes, null where nothing matched.
    """
    return _expr.alpha_2(
        self._expr, fuzzy=fuzzy, historic=historic, parallel=parallel
    )

alpha_3

alpha_3(
    *, fuzzy: bool = False, historic: bool = False, parallel: bool = True
) -> pl.Expr

Extract the ISO 3166-1 alpha-3 code, e.g. "USA".

Parameters:

Name Type Description Default
fuzzy bool

Whether an exact miss may fall back to pycountry's search_fuzzy.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of alpha-3 codes, null where nothing matched.

Source code in python/polars_pycountry/_namespace.py
def alpha_3(
    self,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the ISO 3166-1 alpha-3 code, e.g. `"USA"`.

    Parameters
    ----------
    fuzzy : bool, default False
        Whether an exact miss may fall back to `pycountry`'s
        `search_fuzzy`.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of alpha-3 codes, null where nothing matched.
    """
    return _expr.alpha_3(
        self._expr, fuzzy=fuzzy, historic=historic, parallel=parallel
    )

numeric

numeric(
    *, fuzzy: bool = False, historic: bool = False, parallel: bool = True
) -> pl.Expr

Extract the ISO 3166-1 numeric code, e.g. "840".

Parameters:

Name Type Description Default
fuzzy bool

Whether an exact miss may fall back to pycountry's search_fuzzy.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of zero-padded numeric codes, null where nothing matched.

Source code in python/polars_pycountry/_namespace.py
def numeric(
    self,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the ISO 3166-1 numeric code, e.g. `"840"`.

    Parameters
    ----------
    fuzzy : bool, default False
        Whether an exact miss may fall back to `pycountry`'s
        `search_fuzzy`.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of zero-padded numeric codes, null where nothing
        matched.
    """
    return _expr.numeric(
        self._expr, fuzzy=fuzzy, historic=historic, parallel=parallel
    )

name

name(
    *, fuzzy: bool = False, historic: bool = False, parallel: bool = True
) -> pl.Expr

Extract the country's short name, e.g. "United States".

Parameters:

Name Type Description Default
fuzzy bool

Whether an exact miss may fall back to pycountry's search_fuzzy.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of short names, null where nothing matched.

Source code in python/polars_pycountry/_namespace.py
def name(
    self,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the country's short name, e.g. `"United States"`.

    Parameters
    ----------
    fuzzy : bool, default False
        Whether an exact miss may fall back to `pycountry`'s
        `search_fuzzy`.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of short names, null where nothing matched.
    """
    return _expr.name(
        self._expr, fuzzy=fuzzy, historic=historic, parallel=parallel
    )

official_name

official_name(
    *, fuzzy: bool = False, historic: bool = False, parallel: bool = True
) -> pl.Expr

Extract the official name, e.g. "United States of America".

Parameters:

Name Type Description Default
fuzzy bool

Whether an exact miss may fall back to pycountry's search_fuzzy.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column, null where the country has no distinct official name.

Source code in python/polars_pycountry/_namespace.py
def official_name(
    self,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the official name, e.g. `"United States of America"`.

    Parameters
    ----------
    fuzzy : bool, default False
        Whether an exact miss may fall back to `pycountry`'s
        `search_fuzzy`.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column, null where the country has no distinct official name.
    """
    return _expr.official_name(
        self._expr, fuzzy=fuzzy, historic=historic, parallel=parallel
    )

common_name

common_name(
    *, fuzzy: bool = False, historic: bool = False, parallel: bool = True
) -> pl.Expr

Extract the common name, e.g. "Bolivia" for BO.

Parameters:

Name Type Description Default
fuzzy bool

Whether an exact miss may fall back to pycountry's search_fuzzy.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column, null for the majority of countries, which have none.

Source code in python/polars_pycountry/_namespace.py
def common_name(
    self,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the common name, e.g. `"Bolivia"` for `BO`.

    Parameters
    ----------
    fuzzy : bool, default False
        Whether an exact miss may fall back to `pycountry`'s
        `search_fuzzy`.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column, null for the majority of countries, which have none.
    """
    return _expr.common_name(
        self._expr, fuzzy=fuzzy, historic=historic, parallel=parallel
    )

flag

flag(
    *, fuzzy: bool = False, historic: bool = False, parallel: bool = True
) -> pl.Expr

Extract the flag emoji, e.g. the regional indicators for US.

Parameters:

Name Type Description Default
fuzzy bool

Whether an exact miss may fall back to pycountry's search_fuzzy.

False
historic bool

Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of flag emoji, null where nothing matched.

Source code in python/polars_pycountry/_namespace.py
def flag(
    self,
    *,
    fuzzy: bool = False,
    historic: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the flag emoji, e.g. the regional indicators for `US`.

    Parameters
    ----------
    fuzzy : bool, default False
        Whether an exact miss may fall back to `pycountry`'s
        `search_fuzzy`.
    historic : bool, default False
        Whether ISO 3166-3 is consulted when ISO 3166-1 has no match.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of flag emoji, null where nothing matched.
    """
    return _expr.flag(
        self._expr, fuzzy=fuzzy, historic=historic, parallel=parallel
    )

subdivision

subdivision(
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr

Resolve an ISO 3166-2 subdivision and return it as a struct.

Parameters:

Name Type Description Default
country IntoExprColumn | None

The country to scope the search to. A bare str names a column, per the usual Polars convention; use pl.lit("US") for a fixed country. Pass it whenever you have it: subdivision names are not unique across the standard.

None
fuzzy bool

Whether to fall back to accent-folded and partial name matches.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Struct of code, name, type, country_code and parent_code.

Source code in python/polars_pycountry/_namespace.py
def subdivision(
    self,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Resolve an ISO 3166-2 subdivision and return it as a struct.

    Parameters
    ----------
    country : IntoExprColumn | None, optional
        The country to scope the search to. A bare `str` names a column,
        per the usual Polars convention; use `pl.lit("US")` for a fixed
        country. Pass it whenever you have it: subdivision names are not
        unique across the standard.
    fuzzy : bool, default False
        Whether to fall back to accent-folded and partial name matches.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Struct of `code`, `name`, `type`, `country_code` and
        `parent_code`.
    """
    return _expr.subdivision(
        self._expr, country, fuzzy=fuzzy, parallel=parallel
    )

subdivision_code

subdivision_code(
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the ISO 3166-2 code, e.g. "US-CA".

Parameters:

Name Type Description Default
country IntoExprColumn | None

The country to scope the search to. A bare str names a column, per the usual Polars convention; use pl.lit("US") for a fixed country. Pass it whenever you have it: subdivision names are not unique across the standard.

None
fuzzy bool

Whether to fall back to accent-folded and partial name matches.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of subdivision codes, null where nothing matched.

Source code in python/polars_pycountry/_namespace.py
def subdivision_code(
    self,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the ISO 3166-2 code, e.g. `"US-CA"`.

    Parameters
    ----------
    country : IntoExprColumn | None, optional
        The country to scope the search to. A bare `str` names a column,
        per the usual Polars convention; use `pl.lit("US")` for a fixed
        country. Pass it whenever you have it: subdivision names are not
        unique across the standard.
    fuzzy : bool, default False
        Whether to fall back to accent-folded and partial name matches.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of subdivision codes, null where nothing matched.
    """
    return _expr.subdivision_code(
        self._expr, country, fuzzy=fuzzy, parallel=parallel
    )

subdivision_name

subdivision_name(
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the subdivision's name, e.g. "California".

Parameters:

Name Type Description Default
country IntoExprColumn | None

The country to scope the search to. A bare str names a column, per the usual Polars convention; use pl.lit("US") for a fixed country. Pass it whenever you have it: subdivision names are not unique across the standard.

None
fuzzy bool

Whether to fall back to accent-folded and partial name matches.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of subdivision names, null where nothing matched.

Source code in python/polars_pycountry/_namespace.py
def subdivision_name(
    self,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the subdivision's name, e.g. `"California"`.

    Parameters
    ----------
    country : IntoExprColumn | None, optional
        The country to scope the search to. A bare `str` names a column,
        per the usual Polars convention; use `pl.lit("US")` for a fixed
        country. Pass it whenever you have it: subdivision names are not
        unique across the standard.
    fuzzy : bool, default False
        Whether to fall back to accent-folded and partial name matches.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of subdivision names, null where nothing matched.
    """
    return _expr.subdivision_name(
        self._expr, country, fuzzy=fuzzy, parallel=parallel
    )

subdivision_type

subdivision_type(
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the subdivision's type, e.g. "State".

Parameters:

Name Type Description Default
country IntoExprColumn | None

The country to scope the search to. A bare str names a column, per the usual Polars convention; use pl.lit("US") for a fixed country. Pass it whenever you have it: subdivision names are not unique across the standard.

None
fuzzy bool

Whether to fall back to accent-folded and partial name matches.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of subdivision types, null where nothing matched.

Source code in python/polars_pycountry/_namespace.py
def subdivision_type(
    self,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the subdivision's type, e.g. `"State"`.

    Parameters
    ----------
    country : IntoExprColumn | None, optional
        The country to scope the search to. A bare `str` names a column,
        per the usual Polars convention; use `pl.lit("US")` for a fixed
        country. Pass it whenever you have it: subdivision names are not
        unique across the standard.
    fuzzy : bool, default False
        Whether to fall back to accent-folded and partial name matches.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of subdivision types, null where nothing matched.
    """
    return _expr.subdivision_type(
        self._expr, country, fuzzy=fuzzy, parallel=parallel
    )

subdivision_country

subdivision_country(
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the alpha-2 code of the country it belongs to.

Parameters:

Name Type Description Default
country IntoExprColumn | None

The country to scope the search to. A bare str names a column, per the usual Polars convention; use pl.lit("US") for a fixed country. Pass it whenever you have it: subdivision names are not unique across the standard.

None
fuzzy bool

Whether to fall back to accent-folded and partial name matches.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of alpha-2 codes, null where nothing matched.

Source code in python/polars_pycountry/_namespace.py
def subdivision_country(
    self,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the alpha-2 code of the country it belongs to.

    Parameters
    ----------
    country : IntoExprColumn | None, optional
        The country to scope the search to. A bare `str` names a column,
        per the usual Polars convention; use `pl.lit("US")` for a fixed
        country. Pass it whenever you have it: subdivision names are not
        unique across the standard.
    fuzzy : bool, default False
        Whether to fall back to accent-folded and partial name matches.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of alpha-2 codes, null where nothing matched.
    """
    return _expr.subdivision_country(
        self._expr, country, fuzzy=fuzzy, parallel=parallel
    )

subdivision_parent_code

subdivision_parent_code(
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr

Extract the parent subdivision's code, where there is one.

Parameters:

Name Type Description Default
country IntoExprColumn | None

The country to scope the search to. A bare str names a column, per the usual Polars convention; use pl.lit("US") for a fixed country. Pass it whenever you have it: subdivision names are not unique across the standard.

None
fuzzy bool

Whether to fall back to accent-folded and partial name matches.

False
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of parent codes, null for a top-level subdivision.

Source code in python/polars_pycountry/_namespace.py
def subdivision_parent_code(
    self,
    country: IntoExprColumn | None = None,
    *,
    fuzzy: bool = False,
    parallel: bool = True,
) -> pl.Expr:
    """Extract the parent subdivision's code, where there is one.

    Parameters
    ----------
    country : IntoExprColumn | None, optional
        The country to scope the search to. A bare `str` names a column,
        per the usual Polars convention; use `pl.lit("US")` for a fixed
        country. Pass it whenever you have it: subdivision names are not
        unique across the standard.
    fuzzy : bool, default False
        Whether to fall back to accent-folded and partial name matches.
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of parent codes, null for a top-level subdivision.
    """
    return _expr.subdivision_parent_code(
        self._expr, country, fuzzy=fuzzy, parallel=parallel
    )

currency

currency(*, parallel: bool = True) -> pl.Expr

Resolve an ISO 4217 currency and return every field as a struct.

Parameters:

Name Type Description Default
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Struct of alpha_3, numeric and name.

Source code in python/polars_pycountry/_namespace.py
def currency(self, *, parallel: bool = True) -> pl.Expr:
    """Resolve an ISO 4217 currency and return every field as a struct.

    Parameters
    ----------
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Struct of `alpha_3`, `numeric` and `name`.
    """
    return _expr.currency(self._expr, parallel=parallel)

currency_alpha_3

currency_alpha_3(*, parallel: bool = True) -> pl.Expr

Extract the ISO 4217 alpha-3 code, e.g. "USD".

Parameters:

Name Type Description Default
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of currency codes, null where nothing matched.

Source code in python/polars_pycountry/_namespace.py
def currency_alpha_3(self, *, parallel: bool = True) -> pl.Expr:
    """Extract the ISO 4217 alpha-3 code, e.g. `"USD"`.

    Parameters
    ----------
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of currency codes, null where nothing matched.
    """
    return _expr.currency_alpha_3(self._expr, parallel=parallel)

currency_numeric

currency_numeric(*, parallel: bool = True) -> pl.Expr

Extract the ISO 4217 numeric code, e.g. "840".

Parameters:

Name Type Description Default
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of zero-padded numeric codes, null where nothing matched.

Source code in python/polars_pycountry/_namespace.py
def currency_numeric(self, *, parallel: bool = True) -> pl.Expr:
    """Extract the ISO 4217 numeric code, e.g. `"840"`.

    Parameters
    ----------
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of zero-padded numeric codes, null where nothing
        matched.
    """
    return _expr.currency_numeric(self._expr, parallel=parallel)

currency_name

currency_name(*, parallel: bool = True) -> pl.Expr

Extract the currency's name, e.g. "US Dollar".

Parameters:

Name Type Description Default
parallel bool

Whether the plugin may split large columns across rayon threads.

True

Returns:

Type Description
Expr

Utf8 column of currency names, null where nothing matched.

Source code in python/polars_pycountry/_namespace.py
def currency_name(self, *, parallel: bool = True) -> pl.Expr:
    """Extract the currency's name, e.g. `"US Dollar"`.

    Parameters
    ----------
    parallel : bool, default True
        Whether the plugin may split large columns across rayon threads.

    Returns
    -------
    pl.Expr
        Utf8 column of currency names, null where nothing matched.
    """
    return _expr.currency_name(self._expr, parallel=parallel)

Scalars

For code that is not holding a DataFrame — the same Rust core, no Polars round-trip. Each returns a dict, or None when nothing matched.

lookup_country builtin

lookup_country(
    value: str | None, *, fuzzy: bool = False, historic: bool = False
) -> Record | None

Resolve one country string, for callers that are not holding a DataFrame.

Returns None when nothing matched.

search_countries builtin

search_countries(value: str, *, limit: int | None = None) -> list[Record]

Rank every country the fuzzy search scores against value, best first.

This is pycountry.countries.search_fuzzy with the scores exposed, which the reference keeps to itself.

lookup_subdivision builtin

lookup_subdivision(
    value: str | None, *, country: str | None = None, fuzzy: bool = False
) -> Record | None

Resolve one subdivision string, optionally scoped to a country.

lookup_currency builtin

lookup_currency(value: str | None) -> Record | None

Resolve one currency string against ISO 4217.

The ISO tables

iso_version builtin

iso_version() -> str

The iso-codes release stamp of the tables actually in use.

table_sizes builtin

table_sizes() -> dict[str, int]

How many records each table holds, for provenance and smoke tests.

load_iso_data

load_iso_data(source: str | Path) -> str

Replace the live ISO tables, and return the new version stamp.

Takes effect for every lookup that starts after it returns. A query already running keeps the tables it started with, so no column can be resolved against two different snapshots.

Parameters:

Name Type Description Default
source str | Path

Directory holding iso3166-1.json, iso3166-2.json, iso3166-3.json and iso4217.json, in the shape the iso-codes project publishes them. A VERSION file alongside them is read as the stamp if present.

required

Returns:

Type Description
str

The VERSION stamp of the newly loaded tables, or "unknown" when the directory carries no VERSION file.

Raises:

Type Description
ValueError

If a table is missing, unreadable, unparseable, or too small to be a complete copy. The previously loaded tables stay in use.

Examples:

>>> import polars_pycountry as pc
>>> pc.load_iso_data("/mnt/reference/iso-codes")
'4.20.1'
Source code in python/polars_pycountry/_data.py
def load_iso_data(source: str | Path) -> str:
    """Replace the live ISO tables, and return the new version stamp.

    Takes effect for every lookup that *starts* after it returns. A query
    already running keeps the tables it started with, so no column can be
    resolved against two different snapshots.

    Parameters
    ----------
    source : str | pathlib.Path
        Directory holding `iso3166-1.json`, `iso3166-2.json`,
        `iso3166-3.json` and `iso4217.json`, in the shape the `iso-codes`
        project publishes them. A `VERSION` file alongside them is read as the
        stamp if present.

    Returns
    -------
    str
        The `VERSION` stamp of the newly loaded tables, or `"unknown"` when
        the directory carries no `VERSION` file.

    Raises
    ------
    ValueError
        If a table is missing, unreadable, unparseable, or too small to be a
        complete copy. The previously loaded tables stay in use.

    Examples
    --------
    >>> import polars_pycountry as pc
    >>> pc.load_iso_data("/mnt/reference/iso-codes")  # doctest: +SKIP
    '4.20.1'
    """
    return load_iso_dir(str(source))

refresh_iso_data

refresh_iso_data(
    url: str = ISO_CODES_URL,
    *,
    timeout: float = 60.0,
    save_to: str | Path | None = None,
) -> str

Download the current ISO tables and load them.

This is the only function in the package that touches the network, and only when you call it. Call it before the lookup whose results should reflect the newer tables.

Parameters:

Name Type Description Default
url str

Directory URL to fetch the table files from. Point this at an internal mirror if outbound access is restricted; it must serve the four iso_*.json files under their upstream names.

`ISO_CODES_URL`
timeout float

Seconds to wait for each download.

60.0
save_to str | Path | None

If given, also write the downloaded tables here, so a later run can load_iso_data them (or POLARS_COUNTRY_DATA can point at the directory) without going back to the network. Only written once the tables have been validated and loaded.

None

Returns:

Type Description
str

The version stamp of the newly loaded tables. The upstream directory carries no VERSION file, so this is "unknown" unless save_to pointed at a directory that already had one.

Raises:

Type Description
OSError

If a download fails. The previously loaded tables stay in use.

ValueError

If what comes back is not a usable set of ISO tables. The previously loaded tables stay in use.

Examples:

>>> import polars_pycountry as pc
>>> pc.refresh_iso_data(save_to="iso-codes/")
'unknown'
Source code in python/polars_pycountry/_data.py
def refresh_iso_data(
    url: str = ISO_CODES_URL,
    *,
    timeout: float = 60.0,
    save_to: str | Path | None = None,
) -> str:
    """Download the current ISO tables and load them.

    This is the only function in the package that touches the network, and
    only when you call it. Call it before the lookup whose results should
    reflect the newer tables.

    Parameters
    ----------
    url : str, default `ISO_CODES_URL`
        Directory URL to fetch the table files from. Point this at an internal
        mirror if outbound access is restricted; it must serve the four
        `iso_*.json` files under their upstream names.
    timeout : float, default 60.0
        Seconds to wait for each download.
    save_to : str | pathlib.Path | None, optional
        If given, also write the downloaded tables here, so a later run can
        `load_iso_data` them (or `POLARS_COUNTRY_DATA` can point at the
        directory) without going back to the network. Only written once the
        tables have been validated and loaded.

    Returns
    -------
    str
        The version stamp of the newly loaded tables. The upstream directory
        carries no `VERSION` file, so this is `"unknown"` unless `save_to`
        pointed at a directory that already had one.

    Raises
    ------
    OSError
        If a download fails. The previously loaded tables stay in use.
    ValueError
        If what comes back is not a usable set of ISO tables. The previously
        loaded tables stay in use.

    Examples
    --------
    >>> import polars_pycountry as pc
    >>> pc.refresh_iso_data(save_to="iso-codes/")  # doctest: +SKIP
    'unknown'
    """
    base = url.rstrip("/")
    downloaded: dict[str, bytes] = {}
    for remote, local in TABLE_FILES:
        with urllib.request.urlopen(
            f"{base}/{remote}", timeout=timeout
        ) as response:
            downloaded[local] = response.read()

    # The plugin loads from a directory, so the download lands in a temporary
    # one first. Validation happens inside `load_iso_data`, which means a bad
    # download never reaches `save_to`.
    with tempfile.TemporaryDirectory() as staging:
        staged = Path(staging)
        for local, payload in downloaded.items():
            (staged / local).write_bytes(payload)
        if save_to is not None:
            # An existing stamp beside the destination is the caller's, so it
            # is carried through rather than overwritten with "unknown".
            existing = Path(save_to) / VERSION_FILE
            if existing.is_file():
                (staged / VERSION_FILE).write_bytes(existing.read_bytes())
        version = load_iso_data(staged)

        if save_to is not None:
            destination = Path(save_to)
            destination.mkdir(parents=True, exist_ok=True)
            for local, payload in downloaded.items():
                (destination / local).write_bytes(payload)

    return version