Beispiel #1
0
def test_append_time_domain(test_pd_df, test_df_mixed, other, time, inplace):

    df_year = IamDataFrame(test_pd_df[IAMC_IDX + [2005]],
                           meta=test_df_mixed.meta)
    df_time = IamDataFrame(test_pd_df[IAMC_IDX + [2010]].rename(
        {2010: time}, axis="columns"))

    # append `df_time` to `df_year`
    if other == "time":
        if inplace:
            obs = df_year.copy()
            obs.append(df_time, inplace=True)
        else:
            obs = df_year.append(df_time)
            # assert that original object was not modified
            assert df_year.year == [2005]

    # append `df_year` to `df_time`
    else:
        if inplace:
            obs = df_time.copy()
            obs.append(df_year, inplace=True)
        else:
            obs = df_time.append(df_year)
            # assert that original object was not modified
            assert df_time.time == pd.Index([datetime(2010, 7, 21)])

    assert_iamframe_equal(obs, test_df_mixed)
Beispiel #2
0
def test_interpolate_extra_cols():
    # check that interpolation with non-matching extra_cols has no effect
    # (#351)
    EXTRA_COL_DF = pd.DataFrame(
        [
            ["foo", 2005, 1],
            ["foo", 2010, 2],
            ["bar", 2005, 2],
            ["bar", 2010, 3],
        ],
        columns=["extra_col", "year", "value"],
    )
    df = IamDataFrame(
        EXTRA_COL_DF,
        model="model_a",
        scenario="scen_a",
        region="World",
        variable="Primary Energy",
        unit="EJ/yr",
    )

    # create a copy, interpolate
    df2 = df.copy()
    df2.interpolate(2007)

    # interpolate should work as if extra_cols is in the _data index
    assert_iamframe_equal(df, df2.filter(year=2007, keep=False))
    obs = df2.filter(year=2007)["value"]
    exp = pd.Series([2.4, 1.4], name="value")
    pd.testing.assert_series_equal(obs, exp)
Beispiel #3
0
def test_interpolate_extra_cols():
    # check that interpolation with non-matching extra_cols has no effect
    # (#351)
    EXTRA_COL_DF = pd.DataFrame(
        [
            ['foo', 2005, 1],
            ['foo', 2010, 2],
            ['bar', 2005, 2],
            ['bar', 2010, 3],
        ],
        columns=['extra_col', 'year', 'value'],
    )
    df = IamDataFrame(EXTRA_COL_DF,
                      model='model_a',
                      scenario='scen_a',
                      region='World',
                      variable='Primary Energy',
                      unit='EJ/yr')

    # create a copy, interpolate
    df2 = df.copy()
    df2.interpolate(2007)

    # interpolate should work as if extra_cols is in the _data index
    assert_iamframe_equal(df, df2.filter(year=2007, keep=False))
    obs = df2.filter(year=2007)['value']
    exp = pd.Series([2.4, 1.4], name='value')
    pd.testing.assert_series_equal(obs, exp)
Beispiel #4
0
def test_interpolate_extra_cols():
    # check hat interpolation with non-matching extra_cols has no effect (#351)
    EXTRA_COL_DF = pd.DataFrame([
        ['foo', 2005, 1],
        ['bar', 2010, 3],
    ],
        columns=['extra_col', 'year', 'value'],
    )
    df = IamDataFrame(EXTRA_COL_DF, model='model_a', scenario='scen_a',
                      region='World', variable='Primary Energy', unit='EJ/yr')

    # create a copy, interpolate
    df2 = df.copy()
    df2.interpolate(2007)

    # assert that interpolation didn't change any data
    assert_iamframe_equal(df, df2)
Beispiel #5
0
 def test_limit_of_similar_xs(self, test_db):
     # Check that the function returns the correct behaviour in the limit of similar
     # lead values. We construct a db for exactly identical CH4 and for near-
     # identical CH4 (one point is far away and in the wrong direction).
     range_db = pd.DataFrame(
         [[_ma, str(val), "World", _ech4, _gtc, val] for val in range(10)],
         columns=_msrvu + [2010],
     )
     range_db = IamDataFrame(range_db)
     range_db = self._adjust_time_style_to_match(range_db, test_db)
     same_db = range_db.copy()
     same_db["variable"] = _eco2
     same_db["value"] = 1
     same_db.append(range_db, inplace=True)
     nearly_same_db = same_db.copy()
     # We change the value of one point on the nearly_same and remove it on the same
     nearly_same_db.data["value"].iloc[1] = 0
     same_db.filter(scenario="0", keep=False, inplace=True)
     same_db = IamDataFrame(same_db.data)
     nearly_same_db = IamDataFrame(nearly_same_db.data)
     # Derive crunchers and compare results from the two values
     same_cruncher = self.tclass(same_db)
     nearly_same_cruncher = self.tclass(nearly_same_db)
     if test_db.time_col == "year":
         single_date_df = test_db.filter(
             year=int(same_db[same_db.time_col][0]))
     else:
         single_date_df = test_db.filter(
             time=(same_db[same_db.time_col][0]))
     # We choose the case model a, which has only values over 1.
     single_date_df.filter(model=_mb,
                           scenario=_sa,
                           keep=False,
                           inplace=True)
     same_res = same_cruncher.derive_relationship(_ech4,
                                                  [_eco2])(single_date_df)
     nearly_same_res = nearly_same_cruncher.derive_relationship(
         _ech4, [_eco2])(single_date_df)
     assert np.allclose(same_res["value"],
                        nearly_same_res["value"],
                        rtol=5e-4)