Repository: pandas-dev/pandas

Issue:
read_excel and ExcelFile engine conflict
This is currently valid code:

```python
>>> xl = pd.ExcelFile('foo.xlsx', engine='xlrd')
>>> pd.read_excel(xl, engine='openpyxl')
Empty DataFrame
Columns: []
Index: []
```

Though ambiguous as to which engine is actually being used. I think we should be raising if an engine is specified in `read_excel` when the target is a `pd.ExcelFile`, as the latter is a lower level construct and will already have an engine in place.

@simonjayhawkins 


Discussion:
To clarify I suppose this isn’t ambiguous now since xlrd is our only reader but will be once that doesn’t hold true. May be a similar issue on the write side
Makes sense to me - happy to take a look 
Isn't this a documentation issue, because only when input to `read_excel` is not an instance of `ExcelFile`, then only engine argument given to `read_excel` will be used, in all other cases, engine argument has no impact whatsoever. 
https://github.com/pandas-dev/pandas/blob/master/pandas/io/excel/_base.py#L290
@WillAyd 
I think Will is suggesting it should throw some kind of error - otherwise it’s ambiguous ( without reading the docs ) what the engine is . But a doc update would be welcome too - I can do that as part of the PR

Sent from my iPhone

> On 6 Jun 2019, at 11:35, Sumanau Sareen <notifications@github.com> wrote:
> 
> Isn't this a documentation issue, because only when input to read_excel is not an instance of ExcelFile, then only engine argument given to read_excel will be used, in all other cases, engine argument has no impact whatsoever. @WillAyd
> 
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub, or mute the thread.


Resolving patch:
diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py
--- 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


Test patch:
diff --git a/pandas/tests/io/excel/test_readers.py b/pandas/tests/io/excel/test_readers.py
--- 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, read_ext, excel_engine):
+        # GH 26566
+        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')