def test_init_df_datetime_error(test_pd_df):
    tdf = ScmDataFrame(test_pd_df).data
    tdf["time"] = 2010

    error_msg = r"^All time values must be convertible to datetime\. The following values are not:(.|\s)*$"
    with pytest.raises(ValueError, match=error_msg):
        ScmDataFrame(tdf)
Exemple #2
0
def test_write_read_datafile(test_pd_df, tmp_path):
    tfile = str(tmp_path / "testfile.csv")
    tdf = ScmDataFrame(test_pd_df)

    tdf.to_csv(tfile)

    ScmDataFrame(tfile)
Exemple #3
0
def combo_df(request):
    combination = deepcopy(request.param)
    vals = combination._asdict()
    source = combination.source

    # For average timeseries we drop the last time value so that the data and times are same length
    if combination.timeseries_type == ParameterType.AVERAGE_TIMESERIES:
        assert source[-1] - source[-2] == source[-2] - source[-3]
        source = source[:-1]
        vals["target"] = combination.target.copy()[:-1]
        assert len(vals["target"]) == len(combination.target_values)
        assert len(source) == len(combination.source_values)

    df = ScmDataFrame(
        combination.source_values,
        columns={
            "scenario": ["a_scenario"],
            "model": ["a_model"],
            "region": ["World"],
            "variable": ["Emissions|BC"],
            "unit": ["Mg /yr"],
            "parameter_type":
            [("point" if combination.timeseries_type
              == ParameterType.POINT_TIMESERIES else "average")],
        },
        index=source,
    )

    return Combination(**vals), df
Exemple #4
0
def combo_df(request):
    combo = deepcopy(request.param)
    vals = combo._asdict()
    df_dts = [convert_openscm_time_to_datetime(d) for d in combo.source]

    # For average timeseries we drop the last time value so that the data and times are same length
    if combo.timeseries_type == ParameterType.AVERAGE_TIMESERIES:
        assert df_dts[-1] - df_dts[-2] == df_dts[-2] - df_dts[-3]
        df_dts = df_dts[:-1]
        vals["target"] = combo.target.copy()[:-1]
        assert len(vals["target"]) == len(combo.target_values)
        assert len(df_dts) == len(combo.source_values)

    df = ScmDataFrame(
        combo.source_values,
        columns={
            "scenario": ["a_scenario"],
            "model": ["a_model"],
            "region": ["World"],
            "variable": ["Emissions|BC"],
            "unit": ["Mg /yr"],
            "parameter_type": [
                (
                    "point"
                    if combo.timeseries_type == ParameterType.POINT_TIMESERIES
                    else "average"
                )
            ],
        },
        index=df_dts,
    )

    return Combination(**vals), df
Exemple #5
0
def test_write_read_datafile(test_pd_df, tmp_path):
    tfile = str(tmp_path / "testfile.csv")
    tdf = ScmDataFrame(test_pd_df)

    tdf.to_csv(tfile)

    rdf = ScmDataFrame(tfile)

    pd.testing.assert_frame_equal(tdf.timeseries(), rdf.timeseries())
Exemple #6
0
def test_scm_datetime_df():
    tdf = TEST_DF.copy()
    tdf.rename(
        {
            2005: datetime(2005, 6, 17, 12),
            2010: datetime(2010, 1, 3, 0),
            2015: datetime(2015, 1, 4, 0),
        },
        axis="columns",
        inplace=True,
    )

    yield ScmDataFrame(tdf)
Exemple #7
0
def test_processing_scm_df():
    yield ScmDataFrame(
        data=np.array([[1, 6.0, 7], [0.5, 3, 2], [2, 7, 0], [-1, -2, 3]]).T,
        columns={
            "model": ["a_iam"],
            "climate_model": ["a_model"],
            "scenario": ["a_scenario", "a_scenario", "a_scenario2", "a_scenario3"],
            "region": ["World"],
            "variable": [
                "Primary Energy",
                "Primary Energy|Coal",
                "Primary Energy",
                "Primary Energy",
            ],
            "unit": ["EJ/yr"],
        },
        index=[datetime(2005, 1, 1), datetime(2010, 1, 1), datetime(2015, 6, 12)],
    )
Exemple #8
0
def rcp26():
    fname = join(TEST_DATA, "rcp26_emissions.csv")
    return ScmDataFrame(fname)
Exemple #9
0
def test_scm_df(request):
    if IamDataFrame is None:
        pytest.skip("pyam is not installed")
    yield ScmDataFrame(**request.param)
Exemple #10
0
append_scm_df_pairs_vars = [
    "Primary Energy",
    "Primary Energy|Coal",
    "Primary Energy",
    "Primary Energy",
]
append_scm_df_pairs_times = [
    datetime(2005, 1, 1),
    datetime(2010, 1, 1),
    datetime(2015, 6, 12),
]
append_scm_df_base = ScmDataFrame(
    data=np.array([[1, 6.0, 7], [0.5, 3, 2], [2, 7, 0], [-1, -2, 3]]).T,
    index=append_scm_df_pairs_times,
    columns={
        "scenario": append_scm_df_pairs_scens,
        "variable": append_scm_df_pairs_vars,
        **append_scm_df_pairs_cols,
    },
)
append_scm_df_pairs = [
    {
        "base":
        append_scm_df_base,
        "other":
        ScmDataFrame(
            data=np.array([[-1, 0, 1]]).T,
            index=append_scm_df_pairs_times,
            columns={
                "scenario": ["a_scenario"],
                "variable": ["Primary Energy"],
def test_init_df_long_timespan(test_pd_df):
    df = ScmDataFrame(test_pd_df)

    pd.testing.assert_frame_equal(
        df.timeseries().reset_index(), test_pd_df, check_like=True
    )