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.
defextract(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,)
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.
defmatch(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,)
defalpha_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,)
defalpha_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,)
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.
defnumeric(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,)
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.
defname(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)
defofficial_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,)
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.
defcommon_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,)
defflag(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)
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.
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.
defsubdivision(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)
defsubdivision_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)
defsubdivision_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)
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.
defsubdivision_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)
defsubdivision_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)
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.
defsubdivision_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,)
defcurrency(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)
defcurrency_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)
defcurrency_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)
defcurrency_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)
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.
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
defextract(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)
defmatch(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)
defalpha_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)
defalpha_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)
defnumeric(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)
defname(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)
defofficial_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)
defcommon_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)
defflag(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)
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
defsubdivision(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)
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
defsubdivision_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)
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
defsubdivision_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)
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
defsubdivision_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)
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
defsubdivision_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)
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
defsubdivision_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)
defcurrency(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)
defcurrency_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)
defcurrency_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)
defcurrency_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)
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.
defload_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' """returnload_iso_dir(str(source))
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.
defrefresh_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]={}forremote,localinTABLE_FILES:withurllib.request.urlopen(f"{base}/{remote}",timeout=timeout)asresponse: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`.withtempfile.TemporaryDirectory()asstaging:staged=Path(staging)forlocal,payloadindownloaded.items():(staged/local).write_bytes(payload)ifsave_toisnotNone:# 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_FILEifexisting.is_file():(staged/VERSION_FILE).write_bytes(existing.read_bytes())version=load_iso_data(staged)ifsave_toisnotNone:destination=Path(save_to)destination.mkdir(parents=True,exist_ok=True)forlocal,payloadindownloaded.items():(destination/local).write_bytes(payload)returnversion