Skip to content

read_excel does not work on excel file binary text or buffered binary text object #15914

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
alexanderwhatley opened this issue Apr 6, 2017 · 2 comments · Fixed by #30519
Closed
Labels
Error Reporting Incorrect or improved errors from pandas IO Excel read_excel, to_excel
Milestone

Comments

@alexanderwhatley
Copy link

Python 3.5, Pandas 0.19.2

I have the following excel file, and I am trying to read its binary content, and then have read_excel read the binary content in, except this is not working, and there may be a bug somewhere, as I have specified the engine for reading.

import pandas as pd
f = open("Test_Prom_Data.xlsx", "rb")
df = pd.read_excel(f.read(), engine = "xlrd")
Traceback (most recent call last):
File "", line 1, in
File "/home/alexander/anaconda3/lib/python3.5/site-packages/pandas/io/excel.py", line 191, in read_excel
io = ExcelFile(io, engine=engine)
File "/home/alexander/anaconda3/lib/python3.5/site-packages/pandas/io/excel.py", line 251, in init
raise ValueError('Must explicitly set engine if not passing in'
ValueError: Must explicitly set engine if not passing in buffer or path for io.
Test_Prom_Data.xlsx

@TomAugspurger
Copy link
Contributor

I don't think that's expected to work, from the doctoring:

io : string, path object (pathlib.Path or py._path.local.LocalPath),
    file-like object, pandas ExcelFile, or xlrd workbook.
    The string could be a URL. Valid URL schemes include http, ftp, s3,
    and file. For file URLs, a host is expected. For instance, a local
    file could be file://localhost/path/to/workbook.xlsx

We could have a better error message though, similar to read_csv:

In [9]: pd.read_csv(b"abc,123", encoding='utf-8')
Traceback (most recent call last):
  File "<ipython-input-9-d793693413f5>", line 1, in <module>
    pd.read_csv(b"abc,123", encoding='utf-8')
  File "/Users/taugspurger/Envs/pandas-dev/lib/python3.6/site-packages/pandas-0.19.0+733.gca8ef494d-py3.6-macosx-10.12-x86_64.egg/pandas/io/parsers.py", line 656, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/Users/taugspurger/Envs/pandas-dev/lib/python3.6/site-packages/pandas-0.19.0+733.gca8ef494d-py3.6-macosx-10.12-x86_64.egg/pandas/io/parsers.py", line 404, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "/Users/taugspurger/Envs/pandas-dev/lib/python3.6/site-packages/pandas-0.19.0+733.gca8ef494d-py3.6-macosx-10.12-x86_64.egg/pandas/io/parsers.py", line 763, in __init__
    self._make_engine(self.engine)
  File "/Users/taugspurger/Envs/pandas-dev/lib/python3.6/site-packages/pandas-0.19.0+733.gca8ef494d-py3.6-macosx-10.12-x86_64.egg/pandas/io/parsers.py", line 967, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/Users/taugspurger/Envs/pandas-dev/lib/python3.6/site-packages/pandas-0.19.0+733.gca8ef494d-py3.6-macosx-10.12-x86_64.egg/pandas/io/parsers.py", line 1552, in __init__
    self._reader = libparsers.TextReader(src, **kwds)
  File "as/pandas/io/parsers.pyx", line 393, in pandas.io.libparsers.TextReader.__cinit__ (pandas/io/parsers.c:4209)
  File "as/pandas/io/parsers.pyx", line 727, in pandas.io.libparsers.TextReader._setup_parser_source (pandas/io/parsers.c:9050)
OSError: Expected file path name or file-like object, got <class 'bytes'> type

@alexanderwhatley interested in submitting a fix?

@TomAugspurger TomAugspurger added Error Reporting Incorrect or improved errors from pandas IO Excel read_excel, to_excel Difficulty Intermediate labels Apr 6, 2017
@TomAugspurger TomAugspurger added this to the Next Major Release milestone Apr 6, 2017
@vermastuti
Copy link

#Read and write to excel
dataFileUrl = R"D:\real_names.xlsx"
data = pd.read_excel(dataFileUrl)

@jreback jreback modified the milestones: Contributions Welcome, 1.0 Jan 3, 2020
Omegastick pushed a commit to Omegastick/energy_efficiency_api that referenced this issue Mar 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas IO Excel read_excel, to_excel
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants