Skip to content

Commit

Permalink
Merge branch 'master' into tutorials/first_step_update
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann authored Dec 12, 2019
2 parents 2a3e56b + 9931f38 commit 84011be
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Next Release

- [#302](https://github.com/IAMconsortium/pyam/pull/302) Rework the tutorials
- [#301](https://github.com/IAMconsortium/pyam/pull/301) Bugfix when using `to_excel()` with a `pd.ExcelWriter`
- [#297](https://github.com/IAMconsortium/pyam/pull/297) Add `empty` attribute, better error for `timeseries()` on empty dataframe
- [#295](https://github.com/IAMconsortium/pyam/pull/295) Include `meta` table when writing to or reading from `xlsx` files
- [#292](https://github.com/IAMconsortium/pyam/pull/292) Add warning message if `data` is empty at initialization (after formatting)
Expand Down
3 changes: 3 additions & 0 deletions pyam/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ def to_excel(self, excel_writer, sheet_name='data', iamc_index=False,
if True, write :code:`meta` to an Excel sheet 'meta' (default);
if this is a string, use it as sheet name
"""
close = False
if not isinstance(excel_writer, pd.ExcelWriter):
close = True
excel_writer = pd.ExcelWriter(excel_writer)
Expand All @@ -1171,6 +1172,8 @@ def to_excel(self, excel_writer, sheet_name='data', iamc_index=False,
include_meta = 'meta' if include_meta is True else include_meta
if isstr(include_meta):
write_sheet(excel_writer, include_meta, self.meta, index=True)

# close the file if `excel_writer` arg was a file name
if close:
excel_writer.close()

Expand Down
24 changes: 13 additions & 11 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ def test_io_xlsx(meta_df, meta_args):
# add column to `meta`
meta_df.set_meta(['a', 'b'], 'string')

# write to xlsx
# write to xlsx (direct file name and ExcelWriter, see bug report #300)
file = 'testing_io_write_read.xlsx'
meta_df.to_excel(file, **meta_args[0])

# read from xlsx
import_df = IamDataFrame(file, **meta_args[1])

# assert that `data` and `meta` tables are equal and delete file
pd.testing.assert_frame_equal(meta_df.data, import_df.data)
pd.testing.assert_frame_equal(meta_df.meta, import_df.meta)

os.remove(file)
for f in [file, pd.ExcelWriter(file)]:
meta_df.to_excel(f, **meta_args[0])
if isinstance(f, pd.ExcelWriter):
f.close()

# read from xlsx
import_df = IamDataFrame(file, **meta_args[1])

# assert that `data` and `meta` tables are equal and delete file
pd.testing.assert_frame_equal(meta_df.data, import_df.data)
pd.testing.assert_frame_equal(meta_df.meta, import_df.meta)
os.remove(file)


@pytest.mark.parametrize("args", [{}, dict(sheet_name='meta')])
Expand Down

0 comments on commit 84011be

Please sign in to comment.