py_research.geo module#
Utilities for working with geographical data, esp. data associated with countries.
- class GeoAlliance(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#
Bases:
StrEnum
List of international alliances used to define geo-regions of interest.
- EU = 'eu'#
- EU12 = 'eu12'#
- EU15 = 'eu15'#
- EU25 = 'eu25'#
- EU27 = 'eu27'#
- EU27_2007 = 'eu27_2007'#
- EU28 = 'eu28'#
- EEA = 'eea'#
- G7 = 'g7'#
- G20 = 'g20'#
- APEC = 'apec'#
- BRIC = 'bric'#
- BASIC = 'basic'#
- CIS = 'cis'#
- OECD = 'oecd'#
- class GeoScheme(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#
Bases:
StrEnum
List of schemes, which can be used to define geo-regions of interest.
- country_name = 'country_name'#
Short name of a country.
- continent = 'continent'#
Name of a continent.
- cc_iso3 = 'cc_iso3'#
ISO3 code of a country.
- cc_iso2 = 'cc_iso2'#
ISO2 code of a country.
- alliance = 'alliance'#
Name of an international alliance, to which a country belongs.
- world = 'world'#
Dummy scheme to match all of the world.
- class GeoRegion(label, scheme=GeoScheme.cc_iso3, display_label=None, exclude_already_covered=True)[source]#
Bases:
object
Define a geo-region according to some scheme (country, continent, etc.).
- Parameters:
- CountryScheme#
A
GeoScheme
which can be used to define a single country.`alias of
Literal
[country_name
,cc_iso2
,cc_iso3
]
- class Country(label, scheme=None)[source]#
Bases:
UserString
A country represented by ISO2 code, ISO3 code or name.
- Parameters:
- scheme: Literal[GeoScheme.country_name, GeoScheme.cc_iso2, GeoScheme.cc_iso3] | None = None#
The naming/classification scheme used.
- property data#
Return the country’s label.
- countries_to_scheme(countries, target=GeoScheme.cc_iso3, src=None)[source]#
Translate given series of country labels to
scheme
.- Parameters:
- Returns:
Series of translated country labels.
- Return type:
- expand_geo_col_to_cc(df, geo_col, scheme=GeoScheme.country_name, cc_scheme=GeoScheme.cc_iso3)[source]#
Expand geo-regions present in
geo_col
to country codes.Expand such that rows of
df
with multiple mapped CCs are multiplicated.- Parameters:
- Returns:
The expanded dataframe.
- Return type:
- merge_geo_regions(df, geo_col, geo_regions, input_scheme=GeoScheme.country_name, pretty_labels=True)[source]#
Right-merge
geo_regions
ontodf
based ongeo_col
.Merge such that rows with multiple mapped regions are multiplicated.
- Parameters:
df (DataFrame) – The dataframe to merge into.
geo_col (str) – The column containing geo-regions.
geo_regions (Iterable[GeoRegion | str]) – The geo-regions to merge.
input_scheme (GeoScheme) – The scheme used to define the geo-regions.
rest_of_world – Whether to add a “Rest of World” region.
pretty_labels (bool) – Whether to use pretty labels for regions.
- Returns:
The merged dataframe.
- Return type:
- match_to_geo_region(countries, geo_region, country_scheme=None)[source]#
Check whether countries are in given geo-region.
- Parameters:
- Returns:
Series of booleans indicating whether countries are in geo-region.
- Return type: