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

Bugfix for incorrect dimension-accessors when initializing from pandas #763

Merged
merged 6 commits into from
Aug 4, 2023
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
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ When importing an xlsx file created with pyam < 2.0, which has an "exclude" colu

## Individual updates

- [#763](https://github.com/IAMconsortium/pyam/pull/763) Implement a fix against carrying over unused levels when initializing from an indexed pandas object
- [#759](https://github.com/IAMconsortium/pyam/pull/759) Excise "exclude" column from meta and add a own attribute
- [#747](https://github.com/IAMconsortium/pyam/pull/747) Drop support for Python 3.7 #747

Expand Down
3 changes: 3 additions & 0 deletions pyam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ def format_data(df, index, **kwargs):

df = df.reorder_levels(index + REQUIRED_COLS + [time_col] + extra_cols).dropna()

# remove unused levels to guard against issue #762
df.index = df.index.remove_unused_levels()

else:
if isinstance(df, pd.Series):
if not df.name:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ def test_init_df_from_timeseries(test_df):
pd.testing.assert_frame_equal(df.timeseries(), test_df.timeseries())


def test_init_df_from_timeseries_unused_levels(test_df):
# this test guards against regression for the bug
# reported in https://github.com/IAMconsortium/pyam/issues/762

for (model, scenario), data in test_df.timeseries().groupby(["model", "scenario"]):
# we're only interested in the second model-scenario combination
if model == "model_a" and scenario == "scen_b":
df = IamDataFrame(data)

# pandas 2.0 does not remove unused levels (here: "Primary Energy|Coal") in groupby
# we check that unused levels are removed at initialization of the IamDataFrame
assert df.variable == ["Primary Energy"]


def test_init_df_with_extra_col(test_pd_df):
tdf = test_pd_df.copy()

Expand Down
Loading