Esempio n. 1
0
    def test_no_data_specified(
        self, coords_cols, coords_defaults, coords_terminologies, coords_value_mapping
    ):
        file_input_long = DATA_PATH / "long.csv"

        del coords_cols["sec_cats__Class"]
        del coords_defaults["sec_cats__Type"]
        del coords_terminologies["sec_cats__Class"]
        del coords_terminologies["sec_cats__Type"]

        coords_cols["time"] = "year"

        with pytest.raises(
            ValueError,
            match="No data column in the CSV specified in coords_cols, so nothing to"
            " read.",
        ):
            pm2io.read_long_csv_file_if(
                file_input_long,
                coords_cols=coords_cols,
                coords_defaults=coords_defaults,
                coords_terminologies=coords_terminologies,
                coords_value_mapping=coords_value_mapping,
                time_format="%Y",
            )
Esempio n. 2
0
    def test_compare_wide(
        self,
        coords_cols,
        coords_defaults,
        coords_terminologies,
        coords_value_mapping,
    ):
        file_input_wide = DATA_PATH / "test_csv_data.csv"
        file_input_long = DATA_PATH / "long.csv"

        del coords_cols["sec_cats__Class"]
        del coords_defaults["sec_cats__Type"]
        del coords_terminologies["sec_cats__Class"]
        del coords_terminologies["sec_cats__Type"]

        meta_data = {"references": "Just ask around"}

        df_result_wide = pm2io.read_wide_csv_file_if(
            file_input_wide,
            coords_cols=coords_cols,
            coords_defaults=coords_defaults,
            coords_terminologies=coords_terminologies,
            coords_value_mapping=coords_value_mapping,
            meta_data=meta_data,
        )

        coords_cols["time"] = "year"
        coords_cols["data"] = "emissions"
        df_result_long = pm2io.read_long_csv_file_if(
            file_input_long,
            coords_cols=coords_cols,
            coords_defaults=coords_defaults,
            coords_terminologies=coords_terminologies,
            coords_value_mapping=coords_value_mapping,
            meta_data=meta_data,
            time_format="%Y",
        )

        pd.testing.assert_frame_equal(df_result_wide, df_result_long)
        assert df_result_wide.attrs == df_result_long.attrs
Esempio n. 3
0
    def test_compare_wide_add_cols(
        self,
        coords_cols,
        add_coords_cols,
        coords_defaults,
        coords_terminologies,
        coords_value_mapping,
    ):
        file_input_wide = DATA_PATH / "test_csv_data_category_name.csv"
        file_input_long = DATA_PATH / "test_csv_data_category_name_long.csv"

        del coords_cols["sec_cats__Class"]
        del coords_defaults["sec_cats__Type"]
        del coords_terminologies["sec_cats__Class"]
        del coords_terminologies["sec_cats__Type"]

        df_result_wide = pm2io.read_wide_csv_file_if(
            file_input_wide,
            coords_cols=coords_cols,
            add_coords_cols=add_coords_cols,
            coords_defaults=coords_defaults,
            coords_terminologies=coords_terminologies,
            coords_value_mapping=coords_value_mapping,
        )

        coords_cols["time"] = "year"
        coords_cols["data"] = "emissions"
        df_result_long = pm2io.read_long_csv_file_if(
            file_input_long,
            coords_cols=coords_cols,
            add_coords_cols=add_coords_cols,
            coords_defaults=coords_defaults,
            coords_terminologies=coords_terminologies,
            coords_value_mapping=coords_value_mapping,
            time_format="%Y",
        )

        pd.testing.assert_frame_equal(df_result_wide, df_result_long)
        assert df_result_wide.attrs == df_result_long.attrs
Esempio n. 4
0
    def test_no_time(
        self, coords_cols, coords_defaults, coords_terminologies, coords_value_mapping
    ):
        file_input_long = DATA_PATH / "long_no_time.csv"

        del coords_cols["sec_cats__Class"]
        del coords_defaults["sec_cats__Type"]
        del coords_terminologies["sec_cats__Class"]
        del coords_terminologies["sec_cats__Type"]

        coords_cols["data"] = "emissions"
        coords_defaults["time"] = datetime.datetime(2020, 1, 1)

        df = pm2io.read_long_csv_file_if(
            file_input_long,
            coords_cols=coords_cols,
            coords_defaults=coords_defaults,
            coords_terminologies=coords_terminologies,
            coords_value_mapping=coords_value_mapping,
            time_format="%Y",
        )

        df_expected = pd.DataFrame(
            [
                ("TESTcsv2021", "HISTORY", "AUS", "CO2", "Gg CO2 / yr", "1", 4.1),
                ("TESTcsv2021", "HISTORY", "ZAM", "CH4", "Mt CH4 / yr", "2", 7.0),
            ],
            columns=[
                "source",
                "scenario (general)",
                "area (ISO3)",
                "entity",
                "unit",
                "category (IPCC2006)",
                "2020",
            ],
        )

        pd.testing.assert_frame_equal(df, df_expected)