pypsa-eur-sec icon indicating copy to clipboard operation
pypsa-eur-sec copied to clipboard

Use `country_codes` to make country mapping less hard-coded

Open fneum opened this issue 3 years ago • 0 comments

First step is to write a conversion function for the different datasets:

import pandas as pd
import country_converter as coco

def country_converter():
    """
    Returns a patched CountryConverter() object
    with conversion options for the country codes
    in the following datasets:
    
    - eurostat energy balances
    - UNFCCC emissions database
    - JRC IDEES database
    """
    
    cc = coco.CountryConverter()

    # patch The Netherlands for hotmaps database
    idx = (cc.data.name_short == 'Netherlands').idxmax()
    cc.data.at[idx, "regex"] = cc.data.at[idx, 'regex'][:-1]

    # eurostat country codes from ISO2
    to_replace = {
        'MT': 'MA',
        'TR': 'TU',
        'XK': 'KO',
        'MD': 'MO',
        'UA': 'UK'
    }
    cc.data['eurostat'] = cc.data.ISO2.replace(to_replace)

    # JRC IDEES country codes from ISO2
    to_replace = {
        'GB': 'UK',
        'GR': 'EL'
    }
    cc.data['idees'] = cc.data.ISO2.replace(to_replace)

    # UNFCCC country codes from ISO2
    to_replace = {
        'GB': 'UK',
    }
    cc.data['unfccc'] = cc.data.ISO2.replace(to_replace)

    return coco.CountryConverter(cc.data)

Example use:

cc = country_converter()

rename = {
    "Austriae" : "AT",
    "Bulgaria" : "BG",
    "Belgiume" : "BE",
    "Czechia" : "CZ",
    "Germany" : "DE",
    "Greece" : "GR",
    "United Kingdom" : "GB",
}

cc.convert(rename.keys(), to='eurostat')

cc.EU28as('ISO2').ISO2.values

cc.EEAas('idees').idees.values

fneum avatar Jun 27 '21 19:06 fneum