From b2199e77138f645209f893ed066bcb91dc677740 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Sat, 8 Jun 2019 13:19:33 +0100 Subject: [PATCH 1/5] Throw when read_excel is passed an engine and excel file --- pandas/io/excel/_base.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py index 24412b26b021b..631461b3d14ab 100644 --- a/pandas/io/excel/_base.py +++ b/pandas/io/excel/_base.py @@ -289,6 +289,9 @@ def read_excel(io, if not isinstance(io, ExcelFile): io = ExcelFile(io, engine=engine) + elif engine and engine != io.engine: + raise ValueError("Engine should not be specified when passing " + "an ExcelFile - ExcelFile already has the engine set") return io.parse( sheet_name=sheet_name, @@ -777,6 +780,7 @@ def __init__(self, io, engine=None): if engine not in self._engines: raise ValueError("Unknown engine: {engine}".format(engine=engine)) + self.engine = engine # could be a str, ExcelFile, Book, etc. self.io = io # Always a string From 4e61e63b22ff99f36f5dcd1962b0f5838b61b9ce Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Sun, 9 Jun 2019 01:10:21 +0100 Subject: [PATCH 2/5] Test + whatsnew --- doc/source/whatsnew/v0.25.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 844e062b20ca3..ed866250f3883 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -665,6 +665,7 @@ I/O - Bug in :func:`read_json` where date strings with ``Z`` were not converted to a UTC timezone (:issue:`26168`) - Added ``cache_dates=True`` parameter to :meth:`read_csv`, which allows to cache unique dates when they are parsed (:issue:`25990`) - :meth:`DataFrame.to_excel` now raises a ``ValueError`` when the caller's dimensions exceed the limitations of Excel (:issue:`26051`) +- :func:`read_excel` now raises a ``ValueError`` when its ``engine`` param and :class:`ExcelFile` passed since an :class:`ExcelFile` has an engine defined (:issue:`26566`) Plotting ^^^^^^^^ From 22d8a97553e581d8d300119b0d90e43e06463a66 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Sun, 9 Jun 2019 01:16:27 +0100 Subject: [PATCH 3/5] Reword whatsnew --- doc/source/whatsnew/v0.25.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index ed866250f3883..7c7112bae0ff3 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -665,7 +665,7 @@ I/O - Bug in :func:`read_json` where date strings with ``Z`` were not converted to a UTC timezone (:issue:`26168`) - Added ``cache_dates=True`` parameter to :meth:`read_csv`, which allows to cache unique dates when they are parsed (:issue:`25990`) - :meth:`DataFrame.to_excel` now raises a ``ValueError`` when the caller's dimensions exceed the limitations of Excel (:issue:`26051`) -- :func:`read_excel` now raises a ``ValueError`` when its ``engine`` param and :class:`ExcelFile` passed since an :class:`ExcelFile` has an engine defined (:issue:`26566`) +- :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`) Plotting ^^^^^^^^ From 983ab97aaeafee5ee6c0a95f858450693b8dec44 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Mon, 10 Jun 2019 21:20:41 +0100 Subject: [PATCH 4/5] move test case --- pandas/tests/io/excel/test_readers.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/tests/io/excel/test_readers.py b/pandas/tests/io/excel/test_readers.py index 140727599b182..7a6c9ada6823d 100644 --- a/pandas/tests/io/excel/test_readers.py +++ b/pandas/tests/io/excel/test_readers.py @@ -834,3 +834,14 @@ def test_reader_closes_file(self, read_ext): pd.read_excel(xlsx, 'Sheet1', index_col=0) assert f.closed + + @pytest.mark.parametrize('excel_engine', [ + 'xlrd', + None + ]) + def test_read_excel_engine_value(self, ext, excel_engine): + # GH 26566 + xl = ExcelFile("test1" + ext, engine=excel_engine) + msg = "Engine should not be specified when passing an ExcelFile" + with pytest.raises(ValueError, match=msg): + pd.read_excel(xl, engine='openpyxl') From f52a53aa8bfc9b2a8576bfd1a694743ea133f4af Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Mon, 10 Jun 2019 21:54:57 +0100 Subject: [PATCH 5/5] Change fixture --- pandas/tests/io/excel/test_readers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/io/excel/test_readers.py b/pandas/tests/io/excel/test_readers.py index 7a6c9ada6823d..48fb6b705a4a4 100644 --- a/pandas/tests/io/excel/test_readers.py +++ b/pandas/tests/io/excel/test_readers.py @@ -839,9 +839,9 @@ def test_reader_closes_file(self, read_ext): 'xlrd', None ]) - def test_read_excel_engine_value(self, ext, excel_engine): + def test_read_excel_engine_value(self, read_ext, excel_engine): # GH 26566 - xl = ExcelFile("test1" + ext, engine=excel_engine) + xl = ExcelFile("test1" + read_ext, engine=excel_engine) msg = "Engine should not be specified when passing an ExcelFile" with pytest.raises(ValueError, match=msg): pd.read_excel(xl, engine='openpyxl')