Skip to content

Commit

Permalink
Fix/concat (#665)
Browse files Browse the repository at this point in the history
* Add index transfer to concat

* Add test for non-standard concat

* Add details to concat docstring

* Apply suggestions from code review

Co-authored-by: Daniel Huppmann <[email protected]>

Co-authored-by: Daniel Huppmann <[email protected]>
  • Loading branch information
phackstock and danielhuppmann committed May 24, 2022
1 parent c5e8f9c commit f8e50aa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
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

0 comments on commit f8e50aa

Please sign in to comment.