Exemplo n.º 1
0
def simple_df(request):
    _df = FULL_FEATURE_DF.copy()
    if request.param == 'datetime':
        _df.rename(DTS_MAPPING, axis="columns", inplace=True)
    yield IamDataFrame(model='model_a', scenario='scen_a', data=_df)
Exemplo n.º 2
0
def test_init_df_with_index(test_pd_df):
    df = IamDataFrame(test_pd_df.set_index(META_IDX))
    pd.testing.assert_frame_equal(df.timeseries().reset_index(), test_pd_df)
Exemplo n.º 3
0
def test_init_df_from_timeseries(test_df):
    df = IamDataFrame(test_df.timeseries())
    pd.testing.assert_frame_equal(df.timeseries(), test_df.timeseries())
Exemplo n.º 4
0
def plot_stack_plot_df():
    df = IamDataFrame(TEST_STACKPLOT_DF)
    yield df
Exemplo n.º 5
0
def test_init(data):
    IamDataFrame(data)
Exemplo n.º 6
0
def test_init_df_with_duplicates_raises(test_df):
    _df = test_df.timeseries()
    _df = _df.append(_df.iloc[0]).reset_index()
    match = '3  model_a   scen_a  World  Primary Energy  EJ/yr'
    with pytest.raises(ValueError, match=match):
        IamDataFrame(_df)
Exemplo n.º 7
0
def reg_df():
    df = IamDataFrame(data=REG_DF)
    yield df
Exemplo n.º 8
0
def test_load_meta_empty(test_pd_df):
    """Initializing from xlsx where 'meta' has no rows and non-empty invisible header"""
    obs = IamDataFrame(TEST_DATA_DIR / "empty_meta_sheet.xlsx")
    exp = IamDataFrame(test_pd_df)
    assert_iamframe_equal(obs, exp)
Exemplo n.º 9
0
def test_load_rcp_database_downloaded_file(test_pd_df):
    exp = IamDataFrame(test_pd_df).filter(**FILTER_ARGS).as_pandas()
    file = TEST_DATA_DIR / "test_RCP_database_raw_download.xlsx"
    obs_df = IamDataFrame(file)
    pd.testing.assert_frame_equal(obs_df.as_pandas(), exp)
Exemplo n.º 10
0
import pandas as pd
from pyam import IamDataFrame
from nomenclature import swap_time_for_subannual

TEST_DF = pd.DataFrame([
    ['model_a', 'scen_a', 'Europe', 'Primary Energy', 'EJ/yr', 1, 6.],
],
                       columns=[
                           'model', 'scenario', 'region', 'variable', 'unit',
                           '2005-06-17T00:00+0100', '2010-07-21T12:00+0100'
                       ])
df = IamDataFrame(TEST_DF)


def test_swap_time_for_subannual():
    # test transforming of IamDataFrame in datetime domain to year + subannual
    obs = swap_time_for_subannual(df).data
    obs_year = list(obs['year'].values)
    obs_subannual = list(obs['subannual'].values)
    assert obs_year == [2005, 2010] and \
        obs_subannual == ['06-17 00:00+01:00', '07-21 12:00+01:00']
Exemplo n.º 11
0
def test_data_none():
    # initializing with 'data=None' raises an error
    match = "IamDataFrame constructor not properly called!"
    with pytest.raises(ValueError, match=match):
        IamDataFrame(None)
Exemplo n.º 12
0
def test_load_rcp_database_downloaded_file(test_df_year):
    exp = test_df_year.filter(**FILTER_ARGS).as_pandas()
    obs_df = IamDataFrame(
        os.path.join(TEST_DATA_DIR, 'test_RCP_database_raw_download.xlsx'))
    pd.testing.assert_frame_equal(obs_df.as_pandas(), exp)
Exemplo n.º 13
0
import copy
import pytest

import numpy as np
import pandas as pd
from numpy import testing as npt

from pyam import IamDataFrame, META_IDX, IAMC_IDX, compare

from conftest import TEST_DTS, META_COLS

RENAME_DF = IamDataFrame(
    pd.DataFrame(
        [
            ["model", "scen", "region_a", "test_1", "unit", 1, 5],
            ["model", "scen", "region_a", "test_2", "unit", 2, 6],
            ["model", "scen", "region_a", "test_3", "unit", 3, 7],
            ["model", "scen", "region_b", "test_3", "unit", 4, 8],
        ],
        columns=IAMC_IDX + [2005, 2010],
    ))

# expected output
EXP_RENAME_DF = (IamDataFrame(
    pd.DataFrame(
        [
            ["model", "scen", "region_c", "test", "unit", 4, 12],
            ["model", "scen", "region_a", "test_2", "unit", 2, 6],
            ["model", "scen", "region_b", "test_3", "unit", 4, 8],
        ],
        columns=IAMC_IDX + [2005, 2010],
    )).data.sort_values(by="region").reset_index(drop=True))
Exemplo n.º 14
0
import copy
import pytest

import numpy as np
import pandas as pd
from numpy import testing as npt

from pyam import IamDataFrame, META_IDX, IAMC_IDX, compare

from conftest import TEST_DTS, META_COLS

RENAME_DF = IamDataFrame(
    pd.DataFrame(
        [
            ['model', 'scen', 'region_a', 'test_1', 'unit', 1, 5],
            ['model', 'scen', 'region_a', 'test_2', 'unit', 2, 6],
            ['model', 'scen', 'region_a', 'test_3', 'unit', 3, 7],
            ['model', 'scen', 'region_b', 'test_3', 'unit', 4, 8],
        ],
        columns=IAMC_IDX + [2005, 2010],
    ))

# expected output
EXP_RENAME_DF = IamDataFrame(
    pd.DataFrame(
        [
            ['model', 'scen', 'region_c', 'test', 'unit', 4, 12],
            ['model', 'scen', 'region_a', 'test_2', 'unit', 2, 6],
            ['model', 'scen', 'region_b', 'test_3', 'unit', 4, 8],
        ],
        columns=IAMC_IDX + [2005, 2010],
    )).data.sort_values(by='region').reset_index(drop=True)
Exemplo n.º 15
0
def test_filter_empty_df():
    # test for issue seen in #254
    df = IamDataFrame(data=df_empty)
    obs = df.filter(variable='foo')
    assert len(obs) == 0
Exemplo n.º 16
0
def test_unknown_type():
    # initializing with unsupported argument type raises an error
    match = "IamDataFrame constructor not properly called!"
    with pytest.raises(ValueError, match=match):
        IamDataFrame(True)
Exemplo n.º 17
0
def test_init_from_iamdf_raises(test_df_year):
    # casting an IamDataFrame instance again with extra args fails
    args = dict(model='foo')
    match = f'Invalid arguments `{args}` for initializing an IamDataFrame'
    with pytest.raises(ValueError, match=match):
        IamDataFrame(test_df_year, **args)
Exemplo n.º 18
0
def test_not_a_file():
    # initializing with a file-like that's not a file raises an error
    match = "File foo.csv does not exist"
    with pytest.raises(FileNotFoundError, match=match):
        IamDataFrame("foo.csv")
Exemplo n.º 19
0
def test_df_year():
    df = IamDataFrame(data=TEST_DF)
    for i in META_COLS:
        df.set_meta(META_DF[i])
    yield df
Exemplo n.º 20
0
def test_io_list():
    # initializing with a list raises an error
    match = r"Initializing from list is not supported,*."
    with pytest.raises(ValueError, match=match):
        IamDataFrame([1, 2])
Exemplo n.º 21
0
def plot_df():
    df = IamDataFrame(data=os.path.join(TEST_DATA_DIR, 'plot_data.csv'))
    yield df
Exemplo n.º 22
0
import pandas as pd
from pyam import IamDataFrame, IAMC_IDX
from pyam.testing import assert_iamframe_equal
import pytest

TEST_DF = IamDataFrame(
    pd.DataFrame(
        [
            ["model_a", "scen_a", "World", "Cap", "GW", 1, 2],
            ["model_a", "scen_a", "World", "Cost", "US$2010/kW", 1, 0.5],
            ["model_a", "scen_b", "World", "Cap", "GW", 0.1, 0.2],
            ["model_a", "scen_b", "World", "Cost", "US$2010/kW", 1, 0.5],
            ["model_a", "scen_c", "World", "Cap", "GW", 10, 20],
            ["model_a", "scen_c", "World", "Cost", "US$2010/kW", 1, 0.5],
            ["model_a", "scen_d", "World", "Cap", "GW", 1, 2],
            ["model_a", "scen_d", "World", "Cost", "US$2010/kW", 1, 0.75],
            ["model_a", "scen_e", "World", "Cap", "GW", 1, 2],
            ["model_a", "scen_e", "World", "Cost", "US$2010/kW", 1, 0.25],
        ],
        columns=IAMC_IDX + [2005, 2010],
    ))

EXP_DF = IamDataFrame(
    pd.DataFrame(
        [
            ["model_a", "scen_a", "World", "Learning Rate", "", 0.5],
            ["model_a", "scen_b", "World", "Learning Rate", "", 0.5],
            ["model_a", "scen_c", "World", "Learning Rate", "", 0.5],
            ["model_a", "scen_d", "World", "Learning Rate", "", 0.25],
            ["model_a", "scen_e", "World", "Learning Rate", "", 0.75],
        ],
Exemplo n.º 23
0
import math
import pandas as pd
import pandas.testing as pdt
from pyam import IamDataFrame, IAMC_IDX
from pyam.testing import assert_iamframe_equal
from pyam.timeseries import growth_rate
import pytest

from conftest import META_DF

EXP_DF = IamDataFrame(
    pd.DataFrame(
        [
            ["model_a", "scen_a", "World", "Growth Rate", "", 0.430969],
            ["model_a", "scen_b", "World", "Growth Rate", "", 0.284735],
        ],
        columns=IAMC_IDX + [2005],
    ),
    meta=META_DF,
)


@pytest.mark.parametrize("append", (False, True))
def test_growth_rate(test_df_year, append):
    """Check computing the growth rate from an IamDataFrame"""

    if append:
        obs = test_df_year.copy()
        obs.compute.growth_rate({"Primary Energy": "Growth Rate"}, append=True)
        assert_iamframe_equal(test_df_year.append(EXP_DF), obs)
    else:
Exemplo n.º 24
0
def test_init_empty_message(caplog):
    IamDataFrame(data=df_empty)
    drop_message = ("Formatted data is empty!")
    message_idx = caplog.messages.index(drop_message)
    assert caplog.records[message_idx].levelno == logging.WARNING
Exemplo n.º 25
0
def test_read_pandas():
    df = IamDataFrame(os.path.join(TEST_DATA_DIR, 'testing_data_2.csv'))
    assert list(df.variables()) == ['Primary Energy']
Exemplo n.º 26
0
def test_equals_raises(test_pd_df):
    df = IamDataFrame(test_pd_df)
    pytest.raises(ValueError, df.equals, test_pd_df)
Exemplo n.º 27
0
def test_init_df_with_float_cols(test_pd_df):
    _test_df = test_pd_df.rename(columns={2005: 2005., 2010: 2010.})
    obs = IamDataFrame(_test_df).timeseries().reset_index()
    pd.testing.assert_series_equal(obs[2005], test_pd_df[2005])
Exemplo n.º 28
0
def test_index_attributes_extra_col(test_pd_df):
    test_pd_df['subannual'] = ['summer', 'summer', 'winter']
    df = IamDataFrame(test_pd_df)
    assert df.subannual == ['summer', 'winter']
Exemplo n.º 29
0
def test_load_SSP_database_downloaded_file(test_df):
    obs_df = IamDataFrame(
        os.path.join(TEST_DATA_DIR, 'test_SSP_database_raw_download.xlsx'))
    pd.testing.assert_frame_equal(obs_df.as_pandas(), test_df.as_pandas())
Exemplo n.º 30
0
def test_df_year():
    df = IamDataFrame(data=TEST_DF)
    yield df