Skip to content

Commit

Permalink
Merge branch 'main' into performance/concat
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann authored Mar 22, 2021
2 parents 9e9efd9 + ebceba1 commit 7c1b183
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
16 changes: 10 additions & 6 deletions pyam/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ def timeseries(self, iamc_index=False):
Parameters
----------
iamc_index : bool, default False
iamc_index : bool, optional
if True, use `['model', 'scenario', 'region', 'variable', 'unit']`;
else, use all 'data' columns
Expand All @@ -718,12 +718,16 @@ def timeseries(self, iamc_index=False):
if self.empty:
raise ValueError("This IamDataFrame is empty!")

df = self._data.unstack(level=self.time_col).rename_axis(None, axis=1)
s = self._data
if iamc_index:
if self.time_col == "time":
raise ValueError(
"Cannot use IAMC-index with continuous-time data format!"
)
s = s.droplevel(self.extra_cols)

df = s.unstack(level=self.time_col).rename_axis(None, axis=1).sort_index(axis=1)

if df.index.has_duplicates:
raise ValueError(
"Data with IAMC-index has duplicated index, use `iamc_index=False`"
)
return df

def reset_exclude(self):
Expand Down
40 changes: 38 additions & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import pandas as pd
from numpy import testing as npt
from pandas import testing as pdt

from pyam import IamDataFrame, filter_by_meta, META_IDX, IAMC_IDX, sort_data, compare
from pyam.core import _meta_idx, concat
Expand Down Expand Up @@ -643,9 +644,44 @@ def test_timeseries(test_df):
npt.assert_array_equal(obs, exp)


def test_timeseries_raises(test_df_year):
def test_timeseries_empty_raises(test_df_year):
"""Calling `timeseries()` on an empty IamDataFrame raises"""
_df = test_df_year.filter(model="foo")
pytest.raises(ValueError, _df.timeseries)
with pytest.raises(ValueError, match="This IamDataFrame is empty!"):
_df.timeseries()


def test_timeseries_time_iamc_raises(test_df_time):
"""Calling `timeseries(iamc_index=True)` on a continuous-time IamDataFrame raises"""
match = "Cannot use IAMC-index with continuous-time data format!"
with pytest.raises(ValueError, match=match):
test_df_time.timeseries(iamc_index=True)


def test_timeseries_to_iamc_index(test_pd_df, test_df_year):
"""Reducing timeseries() of an IamDataFrame with extra-columns to IAMC-index"""
test_pd_df["foo"] = "bar"
exta_col_df = IamDataFrame(test_pd_df)
assert exta_col_df.extra_cols == ["foo"]

# assert that reducing to IAMC-columns (dropping extra-columns) with timeseries()
obs = exta_col_df.timeseries(iamc_index=True)
exp = test_df_year.timeseries()
pdt.assert_frame_equal(obs, exp)


def test_timeseries_to_iamc_index_duplicated_raises(test_pd_df):
"""Assert that using `timeseries(iamc_index=True)` raises if there are duplicates"""
test_pd_df = pd.concat([test_pd_df, test_pd_df])
# adding an extra-col creates a unique index
test_pd_df["foo"] = ["bar", "bar", "bar", "baz", "baz", "baz"]
exta_col_df = IamDataFrame(test_pd_df)
assert exta_col_df.extra_cols == ["foo"]

# dropping the extra-column by setting `iamc_index=True` creates duplicated index
match = "Index contains duplicate entries, cannot reshape"
with pytest.raises(ValueError, match=match):
exta_col_df.timeseries(iamc_index=True)


def test_pivot_table(test_df):
Expand Down

0 comments on commit 7c1b183

Please sign in to comment.