Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann committed Dec 16, 2024
1 parent a52c32d commit cef912d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pyam/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ def timeseries(self, iamc_index=False):
s = self._data.droplevel(self.extra_cols)
if s.index.has_duplicates:
raise ValueError(
"Data with IAMC-index has duplicated index, use `iamc_index=False`"
"Dropping non-IAMC-index causes duplicated index."
)

return (
Expand Down
19 changes: 10 additions & 9 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_init_df_with_non_default_index(test_pd_df, index):
"""Casting to IamDataFrame and returning as `timeseries()` yields original frame"""

# set a value to `nan` to check that timeseries columns are ordered correctly
test_pd_df.loc[0, test_pd_df.columns[5]] = np.nan
test_pd_df.loc[0, 2010] = np.nan

# any number of columns can be set as index
df = test_pd_df.copy() if index is None else test_pd_df.set_index(index)
Expand Down Expand Up @@ -553,7 +553,7 @@ def test_timeseries_long(test_df, unsort):
exp.columns.name = None

obs = test_df.timeseries()
pdt.assert_frame_equal(obs, exp, check_column_type=False)
pdt.assert_frame_equal(obs, exp, check_like=True, check_column_type=False)


@pytest.mark.parametrize("unsort", [False, True])
Expand Down Expand Up @@ -588,11 +588,11 @@ def test_timeseries_time_iamc_raises(test_df_time):
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"]
extra_col_df = IamDataFrame(test_pd_df)
assert extra_col_df.extra_cols == ["foo"]

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

Expand All @@ -602,13 +602,14 @@ def test_timeseries_to_iamc_index_duplicated_raises(test_pd_df):
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"]

extra_col_df = IamDataFrame(test_pd_df)
assert extra_col_df.extra_cols == ["foo"]

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


def test_pivot_table(test_df):
Expand Down
12 changes: 8 additions & 4 deletions tests/test_feature_append_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,22 @@ def test_concat(test_df, reverse, iterable):
if iterable:
dfs = iter(dfs)

result = concat(dfs)
obs = concat(dfs)

# check that the original object is not updated
assert test_df.scenario == ["scen_a", "scen_b"]
assert other.scenario == ["scen_c"]

# assert that merging of meta works as expected
pdt.assert_frame_equal(result.meta[EXP_META.columns], EXP_META, check_like=True)
pdt.assert_frame_equal(obs.meta, EXP_META, check_like=True)

# assert that appending data works as expected
ts = result.timeseries()
npt.assert_array_equal(ts.iloc[2].values, ts.iloc[3].values)
ts = obs.timeseries()
print(ts)
pdt.assert_frame_equal(
ts.iloc[0:1].droplevel("scenario"),
ts.iloc[3:].droplevel("scenario")
)


def test_concat_non_default_index():
Expand Down

0 comments on commit cef912d

Please sign in to comment.