Skip to content

allow path to be an StringIO object and skip extension check for that case #5992

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

Closed
wants to merge 12 commits into from
6 changes: 4 additions & 2 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pandas.compat as compat
import pandas.core.common as com
from warnings import warn
from StringIO import StringIO
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to do: from pandas.compat import StringIO in order to pass python3


__all__ = ["read_excel", "ExcelWriter", "ExcelFile"]

Expand Down Expand Up @@ -431,8 +432,9 @@ def save(self):

def __init__(self, path, engine=None, **engine_kwargs):
# validate that this engine can handle the extnesion
ext = os.path.splitext(path)[-1]
self.check_extension(ext)
if not isinstance(path, StringIO):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thius should be not hasattr(path,'read') to check a file-like object

ext = os.path.splitext(path)[-1]
self.check_extension(ext)

self.path = path
self.sheets = {}
Expand Down
17 changes: 17 additions & 0 deletions pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from pandas.util.testing import ensure_clean
from pandas.core.config import set_option, get_option
from StringIO import StringIO
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

import pandas.util.testing as tm
import pandas as pd

Expand Down Expand Up @@ -410,6 +411,22 @@ def test_excelwriter_contextmanager(self):
tm.assert_frame_equal(found_df, self.frame)
tm.assert_frame_equal(found_df2, self.frame2)

def test_stringio_writer(self):
_skip_if_no_xlsxwriter()
_skip_if_no_xlrd()

path = StringIO()
with ExcelWriter(path, engine='xlsxwriter', **{'options': {'in-memory': True}}) as ew:
self.frame.to_excel(ew, 'test1', engine='xlsxwriter')
ew.save()
path.seek(0)
ef = ExcelFile(path)
found_df = ef.parse('test1')
tm.assert_frame_equal(self.frame, found_df)
path.close()



def test_roundtrip(self):
_skip_if_no_xlrd()

Expand Down