55

I'm diving into pandas and experimenting around. As for reading data from an Excel file. I wonder what's the difference between using ExcelFile to read_excel. Both seem to work (albeit slightly different syntax, as could be expected), and the documentation supports both. In both cases, the documentation describes the method the same: "Read an Excel table into DataFrame" and "Read an Excel table into a pandas DataFrame". (documentation for read_excel, and for excel_file)

I'm seeing answers here on SO that uses either, w/o addressing the difference. Also, a Google search didn't produce a result that discusses this issue.

WRT my testing, these seem equivalent:

path = "test/dummydata.xlsx"
xl = pd.ExcelFile(path)
df = xl.parse("dummydata")  # sheet name

and

path = "test/dummydata.xlsx" 
df = pd.io.excel.read_excel(path, sheetname=0)

other than the fact that the latter saves me a line, is there a difference between the two, and is there a reason to use either one?

Thanks!

2
  • 4
    doesn't really save you a line either, because: df = pd.ExcelFile(path).parse("dummydata") Commented Apr 25, 2016 at 17:33
  • 1
    Great question. This is the kind of question that I think makes a positive impact on aspiring coders.
    – psrpsrpsr
    Commented Dec 21, 2020 at 17:36

3 Answers 3

71

There's no particular difference beyond the syntax. Technically, ExcelFile is a class and read_excel is a function. In either case, the actual parsing is handled by the _parse_excel method defined within ExcelFile.

In earlier versions of pandas, read_excel consisted entirely of a single statement (other than comments):

return ExcelFile(path_or_buf,kind=kind).parse(sheetname=sheetname,
                                              kind=kind, **kwds)

And ExcelFile.parse didn't do much more than call ExcelFile._parse_excel.

In recent versions of pandas, read_excel ensures that it has an ExcelFile object (and creates one if it doesn't), and then calls the _parse_excel method directly:

if not isinstance(io, ExcelFile):
    io = ExcelFile(io, engine=engine)

return io._parse_excel(...)

and with the updated (and unified) parameter handling, ExcelFile.parse really is just the single statement:

return self._parse_excel(...)

That is why the docs for ExcelFile.parse now say

Equivalent to read_excel(ExcelFile, ...) See the read_excel docstring for more info on accepted parameters

As for another answer which claims that ExcelFile.parse is faster in a loop, that really just comes down to whether you are creating the ExcelFile object from scratch every time. You could certainly create your ExcelFile once, outside the loop, and pass that to read_excel inside your loop:

xl = pd.ExcelFile(path)
for name in xl.sheet_names:
    df = pd.read_excel(xl, name)

This would be equivalent to

xl = pd.ExcelFile(path)
for name in xl.sheet_names:
    df = xl.parse(name)

If your loop involves different paths (in other words, you are reading many different workbooks, not just multiple sheets within a single workbook), then you can't get around having to create a brand-new ExcelFile instance for each path anyway, and then once again, both ExcelFile.parse and read_excel will be equivalent (and equally slow).

3
  • 1
    Typo: ExcelFile.sheetnames should be ExcelFile.sheet_names Commented May 17, 2018 at 14:18
  • @grisaitis - Thanks, fixed!
    – John Y
    Commented May 17, 2018 at 18:52
  • 1
    To open a workbook using ExcelFile followed by parsing of specific worksheets, the solution benefits from the use of the engine parameter since xlrd 2.0 resp. pandas 1.2.0 (Dec 2020). Example: xls = pd.ExcelFile(path,engine='xlrd'), df = xls.parse(sheet_name=sheetname) ; or xlsx = pd.ExcelFile(path,engine='openpyxl'), df = xlsx.parse(sheet_name=sheetname)
    – Sven Haile
    Commented Mar 10, 2021 at 21:58
14

ExcelFile.parse is faster.

Suppose you are reading dataframes in a loop. With ExcelFile.parse you just pass the Excelfile object(xl in your case). So the excel sheet is just loaded once and you use this to get your dataframes. In case of Read_Excel you pass the path instead of Excelfile object. So essentially every time the workbook is loaded again. Makes a mess if your workbook has loads of sheets and tens of thousands of rows.

1
  • 3
    You don't have to pass a path to read_excel. You could also pass an ExcelFile object, in which case they really are just equivalent.
    – John Y
    Commented Apr 23, 2018 at 23:01
5

I believe Pandas first implementation of excel used the two step process, but then added the one step process called read_excel. Probably left the first one in because folks were already using it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.