Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/concat #665

Merged
merged 4 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pyam/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2793,6 +2793,8 @@ def concat(objs, ignore_meta_conflict=False, **kwargs):
The *meta* attributes are merged only for those objects of *objs* that are passed
as :class:`IamDataFrame` instances.

The :attr:`dimensions` and :attr:`index` names of all elements of *dfs* must be
identical. The returned IamDataFrame inherits the dimensions and index names.
"""
if not islistable(objs) or isinstance(objs, pd.DataFrame):
raise TypeError(f"'{objs.__class__.__name__}' object is not iterable")
Expand Down Expand Up @@ -2844,7 +2846,11 @@ def as_iamdataframe(df):
)

# return as new IamDataFrame, this will verify integrity as part of `__init__()`
return IamDataFrame(pd.concat(ret_data, verify_integrity=False), meta=ret_meta)
return IamDataFrame(
pd.concat(ret_data, verify_integrity=False),
meta=ret_meta,
index=ret_meta.index.names,
)


def read_datapackage(path, data="data", meta="meta"):
Expand Down
32 changes: 32 additions & 0 deletions tests/test_feature_append_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,38 @@ def test_concat(test_df, reverse, iterable):
npt.assert_array_equal(ts.iloc[2].values, ts.iloc[3].values)


def test_concat_non_default_index():
# Test that merging two IamDataFrames with identical, non-standard index dimensions
# preserves the index.

df1 = IamDataFrame(
pd.DataFrame(
[["model_a", "scenario_a", "region_a", "variable_a", "unit", 1, 1]],
columns=IAMC_IDX + ["version", 2005],
),
index=META_IDX + ["version"],
)
df2 = IamDataFrame(
pd.DataFrame(
[["model_a", "scenario_a", "region_a", "variable_a", "unit", 2, 2]],
columns=IAMC_IDX + ["version", 2005],
),
index=META_IDX + ["version"],
)
exp = IamDataFrame(
pd.DataFrame(
[
["model_a", "scenario_a", "region_a", "variable_a", "unit", 1, 1],
["model_a", "scenario_a", "region_a", "variable_a", "unit", 2, 2],
],
columns=IAMC_IDX + ["version", 2005],
),
index=META_IDX + ["version"],
)

assert_iamframe_equal(exp, concat([df1, df2]))


@pytest.mark.parametrize("reverse", (False, True))
def test_concat_with_pd_dataframe(test_df, reverse):
other = test_df.filter(scenario="scen_b").rename({"scenario": {"scen_b": "scen_c"}})
Expand Down