Skip to content

Commit

Permalink
Fix more tests and make ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann committed Dec 16, 2024
1 parent cef912d commit 4d1cf87
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
8 changes: 3 additions & 5 deletions pyam/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def interpolate(self, time, inplace=False, **kwargs):
df.columns.name = ret.time_col
df = df.stack(future_stack=True).dropna() # wide data to pd.Series
df.name = "value"
ret._data = df.sort_index()
ret._data = df
ret._set_attributes()

if not inplace:
Expand Down Expand Up @@ -816,12 +816,10 @@ def timeseries(self, iamc_index=False):
)
s = self._data.droplevel(self.extra_cols)
if s.index.has_duplicates:
raise ValueError(
"Dropping non-IAMC-index causes duplicated index."
)
raise ValueError("Dropping non-IAMC-index causes duplicated index.")

return (
s.unstack(level=self.time_col, sort=False)
s.unstack(level=self.time_col)
.rename_axis(None, axis=1)
.sort_index(
axis=1, key=compare_year_time if self.time_domain == "mixed" else None
Expand Down
4 changes: 3 additions & 1 deletion pyam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,9 @@ def print_list(x, n):
[
# set year lower than first timestep of that year (2010 < 2010-01-01 00:00)
datetime(time, 1, 1, 0, 0, 0) - timedelta(0, 0.01)
if isinstance(time, int) else time for time in x
if isinstance(time, int)
else time
for time in x
]
)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_cast_to_iamc.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_cast_from_value_col_and_args(test_df_year):
],
columns=[
"scenario",
"iso",
"node",
"unit",
"year",
"Primary Energy",
Expand All @@ -53,7 +53,7 @@ def test_cast_from_value_col_and_args(test_df_year):
df = IamDataFrame(
df_with_value_cols,
model="model_a",
region="iso",
region="node",
value=["Primary Energy", "Primary Energy|Coal"],
)
pdt.assert_series_equal(df._data, test_df_year._data, check_like=True)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,7 @@ def test_timeseries_wide(test_pd_df, unsort):
exp = test_pd_df.set_index(IAMC_IDX)

if unsort:
obs = IamDataFrame(test_pd_df[IAMC_IDX + [2010, 2005]]
).timeseries()
obs = IamDataFrame(test_pd_df[IAMC_IDX + [2010, 2005]]).timeseries()
else:
obs = IamDataFrame(test_pd_df).timeseries()
pdt.assert_frame_equal(obs, exp, check_column_type=False)
Expand Down
29 changes: 17 additions & 12 deletions tests/test_feature_append_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,10 @@ def test_concat(test_df, reverse, iterable):
pdt.assert_frame_equal(obs.meta, EXP_META, check_like=True)

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


Expand Down Expand Up @@ -162,21 +161,24 @@ def test_concat_with_pd_dataframe(test_df, reverse):

# merge with only the timeseries `data` DataFrame of `other`
if reverse:
result = concat([other.data, test_df])
obs = concat([other.data, test_df])
else:
result = concat([test_df, other.data])
obs = concat([test_df, other.data])

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

# assert that merging meta from `other` is ignored
exp_meta = META_DF.copy()
exp_meta.loc[("model_a", "scen_c"), "number"] = np.nan
pdt.assert_frame_equal(result.meta, exp_meta[META_COLS])
pdt.assert_frame_equal(obs.meta, exp_meta[META_COLS])

# 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().sort_index()
pdt.assert_frame_equal(
ts.iloc[2:3].droplevel("scenario"),
ts.iloc[3:4].droplevel("scenario"),
)


def test_concat_all_pd_dataframe(test_df):
Expand All @@ -185,11 +187,14 @@ def test_concat_all_pd_dataframe(test_df):
other = test_df.filter(scenario="scen_b").rename({"scenario": {"scen_b": "scen_c"}})

# merge only the timeseries `data` DataFrame of both items
result = concat([test_df.data, other.data])
obs = concat([test_df.data, other.data])

# 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().sort_index()
pdt.assert_frame_equal(
ts.iloc[2:3].droplevel("scenario"),
ts.iloc[3:4].droplevel("scenario"),
)


@pytest.mark.parametrize("inplace", (True, False))
Expand Down
4 changes: 1 addition & 3 deletions tests/test_iiasa.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,7 @@ def test_lazy_read(tmpdir):
assert df.model == ["model_a"]
# This is read from the file, so the filter is not applied.
df2 = lazy_read_iiasa(tmp_file, TEST_API)
assert (
df.data.sort_values(by=IAMC_IDX).reset_index(drop=True).equals(df2.data)
)
assert df.data.sort_values(by=IAMC_IDX).reset_index(drop=True).equals(df2.data)
# If requesting with an inconsistent filter, get nothing back. Strings and filters
# work interchangably.
tmp_file = str(tmp_file)
Expand Down

0 comments on commit 4d1cf87

Please sign in to comment.