Skip to content

Commit f197f50

Browse files
alimcmaster1jreback
authored andcommitted
read_excel and Excel File Engine conflict (#26736)
1 parent 118d621 commit f197f50

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ I/O
665665
- Bug in :func:`read_json` where date strings with ``Z`` were not converted to a UTC timezone (:issue:`26168`)
666666
- Added ``cache_dates=True`` parameter to :meth:`read_csv`, which allows to cache unique dates when they are parsed (:issue:`25990`)
667667
- :meth:`DataFrame.to_excel` now raises a ``ValueError`` when the caller's dimensions exceed the limitations of Excel (:issue:`26051`)
668+
- :func:`read_excel` now raises a ``ValueError`` when input is of type :class:`pandas.io.excel.ExcelFile` and ``engine`` param is passed since :class:`pandas.io.excel.ExcelFile` has an engine defined (:issue:`26566`)
668669

669670
Plotting
670671
^^^^^^^^

pandas/io/excel/_base.py

+4
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ def read_excel(io,
289289

290290
if not isinstance(io, ExcelFile):
291291
io = ExcelFile(io, engine=engine)
292+
elif engine and engine != io.engine:
293+
raise ValueError("Engine should not be specified when passing "
294+
"an ExcelFile - ExcelFile already has the engine set")
292295

293296
return io.parse(
294297
sheet_name=sheet_name,
@@ -777,6 +780,7 @@ def __init__(self, io, engine=None):
777780
if engine not in self._engines:
778781
raise ValueError("Unknown engine: {engine}".format(engine=engine))
779782

783+
self.engine = engine
780784
# could be a str, ExcelFile, Book, etc.
781785
self.io = io
782786
# Always a string

pandas/tests/io/excel/test_readers.py

+11
Original file line numberDiff line numberDiff line change
@@ -834,3 +834,14 @@ def test_reader_closes_file(self, read_ext):
834834
pd.read_excel(xlsx, 'Sheet1', index_col=0)
835835

836836
assert f.closed
837+
838+
@pytest.mark.parametrize('excel_engine', [
839+
'xlrd',
840+
None
841+
])
842+
def test_read_excel_engine_value(self, read_ext, excel_engine):
843+
# GH 26566
844+
xl = ExcelFile("test1" + read_ext, engine=excel_engine)
845+
msg = "Engine should not be specified when passing an ExcelFile"
846+
with pytest.raises(ValueError, match=msg):
847+
pd.read_excel(xl, engine='openpyxl')

0 commit comments

Comments
 (0)