Skip to content

Commit

Permalink
bugfix for interpolation with extra columns (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann authored Mar 24, 2020
1 parent f5cba2a commit 0d2451c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ an IamDataFrame with `n/a` entries in columns other than `value` raises an error

## Individual Updates

- [#352](https://github.com/IAMconsortium/pyam/pull/352) Bugfix when using `interpolate()` on data with extra columns
- [#349](https://github.com/IAMconsortium/pyam/pull/349) Fixes an issue with checking that time columns are equal when appending IamDataFrames
- [#348](https://github.com/IAMconsortium/pyam/pull/348) Extend pages for API docs, clean up docstrings, and harmonize formatting
- [#347](https://github.com/IAMconsortium/pyam/pull/347) Enable contexts and custom UnitRegistry with unit conversion
Expand Down
3 changes: 2 additions & 1 deletion pyam/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ def interpolate(self, time):
raise ValueError(
'The `time` argument `{}` is not an integer'.format(time)
)
df = self.pivot_table(index=IAMC_IDX, columns=[self.time_col],
index = [i for i in self._LONG_IDX if i not in [self.time_col]]
df = self.pivot_table(index=index, columns=[self.time_col],
values='value', aggfunc=np.sum)
# drop time-rows where values are already defined
if time in df.columns:
Expand Down
37 changes: 32 additions & 5 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from pyam import IamDataFrame, validate, categorize, \
require_variable, filter_by_meta, META_IDX, IAMC_IDX, sort_data, compare
from pyam.core import _meta_idx, concat
from pyam.utils import isstr
from pyam.testing import assert_iamframe_equal

from conftest import TEST_DTS

Expand Down Expand Up @@ -687,15 +689,40 @@ def test_category_top_level(test_df):
pd.testing.assert_series_equal(obs, exp)


def test_interpolate(test_df_year):
test_df_year.interpolate(2007)
obs = test_df_year.filter(year=2007).data['value'].reset_index(drop=True)
def test_interpolate(test_pd_df):
_df = test_pd_df.copy()
_df['foo'] = ['bar', 'baz', 2] # add extra_col (check for #351)
df = IamDataFrame(_df)
df.interpolate(2007)
obs = df.filter(year=2007).data['value'].reset_index(drop=True)
exp = pd.Series([3, 1.5, 4], name='value')
pd.testing.assert_series_equal(obs, exp)

# redo the interpolation and check that no duplicates are added
test_df_year.interpolate(2007)
assert not test_df_year.filter().data.duplicated().any()
df.interpolate(2007)
assert not df.filter().data.duplicated().any()

# assert that extra_col does not have nan's (check for #351)
assert all([True if isstr(i) else ~np.isnan(i) for i in df.data.foo])


def test_interpolate_extra_cols():
# check hat interpolation with non-matching extra_cols has no effect (#351)
EXTRA_COL_DF = pd.DataFrame([
['foo', 2005, 1],
['bar', 2010, 3],
],
columns=['extra_col', 'year', 'value'],
)
df = IamDataFrame(EXTRA_COL_DF, model='model_a', scenario='scen_a',
region='World', variable='Primary Energy', unit='EJ/yr')

# create a copy, interpolate
df2 = df.copy()
df2.interpolate(2007)

# assert that interpolation didn't change any data
assert_iamframe_equal(df, df2)


def test_interpolate_datetimes(test_df):
Expand Down

0 comments on commit 0d2451c

Please sign in to comment.