Example #1
0
    def test_parse_collection(self):
        json_string_v1_one_dataset = """
        {
            "oecd" : {
                "value": [1],
                "dimension" : {
                    "id": ["one"],
                    "size": [1],
                    "one": { "category": { "index":{"2010":0}} }
                }
            }
        }
        """
        ret = jsonstat.from_string(json_string_v1_one_dataset)
        self.assertIsInstance(ret, jsonstat.JsonStatCollection)

        json_string_v1_two_datasets = """
        {
            "oecd" : {
                "value": [1],
                "dimension" : {
                    "id": ["one"],
                    "size": [1],
                    "one": { "category": { "index":{"2010":0}} }
                }
            },
            "canada" : {
                "value": [1],
                "dimension": {
                    "id": ["one"],
                    "size": [1],
                    "one": { "category": { "index":{"2010":0}} }
                }
            }
        }
        """
        ret = jsonstat.from_string(json_string_v1_two_datasets)
        self.assertIsInstance(ret, jsonstat.JsonStatCollection)
Example #2
0
def test_parse_collection():
    json_string_v1_one_dataset = """
        {
            "oecd" : {
                "value": [1],
                "dimension" : {
                    "id": ["one"],
                    "size": [1],
                    "one": { "category": { "index":{"2010":0}} }
                }
            }
        }
        """
    ret = jsonstat.from_string(json_string_v1_one_dataset)
    assert isinstance(ret, jsonstat.JsonStatCollection)

    json_string_v1_two_datasets = """
        {
            "oecd" : {
                "value": [1],
                "dimension" : {
                    "id": ["one"],
                    "size": [1],
                    "one": { "category": { "index":{"2010":0}} }
                }
            },
            "canada" : {
                "value": [1],
                "dimension": {
                    "id": ["one"],
                    "size": [1],
                    "one": { "category": { "index":{"2010":0}} }
                }
            }
        }
        """
    ret = jsonstat.from_string(json_string_v1_two_datasets)
    assert isinstance(ret, jsonstat.JsonStatCollection)
Example #3
0
 def test_parse_dimension(self):
     self.json_string_dimension = """
     {
         "version" : "2.0",
         "class" : "dimension",
         "label" : "sex",
         "category" : {
             "index" : ["T", "M", "F"],
             "label" : {
                 "T" : "total",
                 "M" : "male",
                 "F" : "female"
             }
         }
     }
     """
     ret = jsonstat.from_string(self.json_string_dimension)
     self.assertIsInstance(ret, jsonstat.JsonStatDimension)
Example #4
0
def test_parse_dimension():
    json_string_dimension = """
        {
            "version" : "2.0",
            "class" : "dimension",
            "label" : "sex",
            "category" : {
                "index" : ["T", "M", "F"],
                "label" : {
                    "T" : "total",
                    "M" : "male",
                    "F" : "female"
                }
            }
        }
        """
    ret = jsonstat.from_string(json_string_dimension)
    assert isinstance(ret, jsonstat.JsonStatDimension)
def llegir_indicador_world_bank(indicador):
    # Consultem via API la web del Banc Mundial.
    # La funciĆ³ retorna un Panda DataFrame amb les columnes "ISO3","year","<indicador>"

    filename = os.getcwd() + "/" + indicador + ".jsonstat"
    url = "https://api.worldbank.org/v2/country/all/indicator/" + indicador + "?format=jsonstat"

    if os.path.isfile(filename):
        jstat = jsonstat.from_file(filename)
    else:
        jstat = jsonstat.from_string(jsonstat.download(url, filename))

    df = jstat.dataset(0).to_data_frame(content='id')

    df.drop(columns="series", inplace=True)
    df.rename(columns={"country": "ISO3", "Value": indicador}, inplace=True)

    # Assimilem les dades de 2018 a l'any 2019 a l'espera de la seva publicaciĆ³
    j = 0
    for i in (df.loc[df.year == '2019']).index:
        df.at[i, indicador] = (df.loc[df.year == '2018', indicador]).values[j]
        j += 1

    return df