-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove 'xls' as by-default-supported file format #708
Changes from 12 commits
601014e
4bf52c0
4d8057a
7ba06cc
1678f39
0fdd4a1
ed212d7
ffd91a0
dfd1827
fd63767
6211426
71afe85
442536b
671db63
893fd2f
f2068e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,8 +190,8 @@ def _init(self, data, meta=None, index=DEFAULT_META_INDEX, **kwargs): | |
self.set_meta(meta) | ||
|
||
# if initializing from xlsx, try to load `meta` table from file | ||
if meta_sheet and isinstance(data, Path) and data.suffix == ".xlsx": | ||
excel_file = pd.ExcelFile(data, engine="openpyxl") | ||
if meta_sheet and isinstance(data, Path) and data.suffix in [".xlsx", ".xls"]: | ||
excel_file = pd.ExcelFile(data) | ||
if meta_sheet in excel_file.sheet_names: | ||
self.load_meta(excel_file, sheet_name=meta_sheet, ignore_conflict=True) | ||
|
||
|
@@ -2266,11 +2266,13 @@ def to_csv(self, path, iamc_index=False, **kwargs): | |
|
||
Parameters | ||
---------- | ||
path : str or path object | ||
file path or :class:`pathlib.Path` | ||
iamc_index : bool, default False | ||
if True, use `['model', 'scenario', 'region', 'variable', 'unit']`; | ||
else, use all 'data' columns | ||
path : path-like or file-like | ||
File path or :class:`pathlib.Path`. | ||
iamc_index : bool, optional | ||
If True, use `['model', 'scenario', 'region', 'variable', 'unit']`; | ||
else, use all :meth:`dimensions`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need to describe how you're using these variables There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a reference to the |
||
**kwargs | ||
Passed to :meth:`pandas.DataFrame.to_csv` | ||
""" | ||
self._to_file_format(iamc_index).to_csv(path, index=False, **kwargs) | ||
|
||
|
@@ -2286,30 +2288,31 @@ def to_excel( | |
|
||
Parameters | ||
---------- | ||
excel_writer : str, path object or ExcelWriter object | ||
any valid string path, :class:`pathlib.Path` | ||
or :class:`pandas.ExcelWriter` | ||
excel_writer : path-like, file-like, or ExcelWriter object | ||
File path, :class:`pathlib.Path`, or existing :class:`pandas.ExcelWriter`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar to above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revised. |
||
sheet_name : string | ||
name of sheet which will contain :meth:`timeseries` data | ||
iamc_index : bool, default False | ||
if True, use `['model', 'scenario', 'region', 'variable', 'unit']`; | ||
else, use all 'data' columns | ||
include_meta : boolean or string | ||
if True, write 'meta' to an Excel sheet name 'meta' (default); | ||
if this is a string, use it as sheet name | ||
Name of sheet which will contain :meth:`timeseries` data. | ||
iamc_index : bool, optional | ||
If True, use `['model', 'scenario', 'region', 'variable', 'unit']`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See answer above. |
||
else, use all :meth:`dimensions`. | ||
include_meta : boolean or string, optional | ||
If True, write 'meta' to an Excel sheet name 'meta' (default); | ||
if this is a string, use it as sheet name. | ||
**kwargs | ||
Passed to :class:`pandas.ExcelWriter` (if *excel_writer* is path-like) | ||
""" | ||
# open a new ExcelWriter instance (if necessary) | ||
close = False | ||
if not isinstance(excel_writer, pd.ExcelWriter): | ||
close = True | ||
excel_writer = pd.ExcelWriter(excel_writer, engine="xlsxwriter") | ||
excel_writer = pd.ExcelWriter(excel_writer, **kwargs) | ||
|
||
# write data table | ||
write_sheet(excel_writer, sheet_name, self._to_file_format(iamc_index)) | ||
|
||
# write meta table unless `include_meta=False` | ||
if include_meta: | ||
meta_rename = dict([(i, i.capitalize()) for i in META_IDX]) | ||
meta_rename = dict([(i, i.capitalize()) for i in self.index.names]) | ||
write_sheet( | ||
excel_writer, | ||
"meta" if include_meta is True else include_meta, | ||
|
@@ -2320,20 +2323,21 @@ def to_excel( | |
if close: | ||
excel_writer.close() | ||
|
||
def export_meta(self, excel_writer, sheet_name="meta"): | ||
"""Write the 'meta' indicators of this object to an Excel sheet | ||
def export_meta(self, excel_writer, sheet_name="meta", **kwargs): | ||
"""Write the 'meta' indicators of this object to an Excel spreadsheet | ||
|
||
Parameters | ||
---------- | ||
excel_writer : str, path object or ExcelWriter object | ||
any valid string path, :class:`pathlib.Path` | ||
or :class:`pandas.ExcelWriter` | ||
File path, :class:`pathlib.Path`, or existing :class:`pandas.ExcelWriter`. | ||
sheet_name : str | ||
name of sheet which will contain dataframe of 'meta' indicators | ||
Name of sheet which will contain 'meta'. | ||
**kwargs | ||
Passed to :class:`pandas.ExcelWriter` (if *excel_writer* is path-like) | ||
""" | ||
close = False | ||
if not isinstance(excel_writer, pd.ExcelWriter): | ||
excel_writer = pd.ExcelWriter(excel_writer, engine="xlsxwriter") | ||
excel_writer = pd.ExcelWriter(excel_writer, **kwargs) | ||
close = True | ||
write_sheet(excel_writer, sheet_name, self.meta, index=True) | ||
if close: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this description is unclear - it's presumably still accepting strings, as well as file objects or paths?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for noticing... I looked at the pandas to_csv in more detail to be sure, and noticed that you can even use
path=None
- now supported by pyam, and with a test.. (and I also improved the docs).