Skip to content

Commit 5be379f

Browse files
committed
API: default value for read_excel sheet (GH6573)
1 parent a71ede3 commit 5be379f

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

doc/source/io.rst

+14-5
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,7 @@ a DataFrame. See the :ref:`cookbook<cookbook.excel>` for some
18201820
advanced strategies
18211821

18221822
Besides ``read_excel`` you can also read Excel files using the ``ExcelFile``
1823-
class. The following two command are equivalent:
1823+
class. The following two commands are equivalent:
18241824

18251825
.. code-block:: python
18261826
@@ -1843,10 +1843,13 @@ the sheet names using the ``sheet_names`` attribute.
18431843
.. versionadded:: 0.13
18441844

18451845
There are now two ways to read in sheets from an Excel file. You can provide
1846-
either the index of a sheet or its name. If the value provided is an integer
1847-
then it is assumed that the integer refers to the index of a sheet, otherwise
1848-
if a string is passed then it is assumed that the string refers to the name of
1849-
a particular sheet in the file.
1846+
either the index of a sheet or its name to by passing different values for
1847+
``sheet_name``.
1848+
1849+
- Pass a string to refer to the name of a particular sheet in the workbook.
1850+
- Pass an integer to refer to the index of a sheet. Indices follow Python
1851+
convention, beginning at 0.
1852+
- The default value is ``sheet_name=0``. This reads the first sheet.
18501853

18511854
Using the sheet name:
18521855

@@ -1860,6 +1863,12 @@ Using the sheet index:
18601863
18611864
read_excel('path_to_file.xls', 0, index_col=None, na_values=['NA'])
18621865
1866+
Using all default values:
1867+
1868+
.. code-block:: python
1869+
1870+
read_excel('path_to_file.xls')
1871+
18631872
It is often the case that users will insert columns to do temporary computations
18641873
in Excel and you may not want to read in those columns. `read_excel` takes
18651874
a `parse_cols` keyword to allow you to specify a subset of columns to parse.

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ New features
6363
API Changes
6464
~~~~~~~~~~~
6565

66+
- ``read_excel`` uses 0 as the default sheet (:issue:`6573`)
6667
- ``iloc`` will now accept out-of-bounds indexers, e.g. a value that exceeds the length of the object being
6768
indexed. These will be excluded. This will make pandas conform more with pandas/numpy indexing of out-of-bounds
6869
values. A single indexer that is out-of-bounds and drops the dimensions of the object will still raise

doc/source/v0.14.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Highlights include:
1515
API changes
1616
~~~~~~~~~~~
1717

18+
``read_excel`` uses 0 as the default sheet (:issue:`6573`)
1819
- ``iloc`` will now accept out-of-bounds indexers for slices, e.g. a value that exceeds the length of the object being
1920
indexed. These will be excluded. This will make pandas conform more with pandas/numpy indexing of out-of-bounds
2021
values. A single indexer / list of indexers that is out-of-bounds will still raise

pandas/io/excel.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ def get_writer(engine_name):
4949
raise ValueError("No Excel writer '%s'" % engine_name)
5050

5151

52-
def read_excel(io, sheetname, **kwds):
52+
def read_excel(io, sheetname=0, **kwds):
5353
"""Read an Excel table into a pandas DataFrame
5454
5555
Parameters
5656
----------
5757
io : string, file-like object or xlrd workbook
5858
If a string, expected to be a path to xls or xlsx file
59-
sheetname : string
60-
Name of Excel sheet
59+
sheetname : string or int, default 0
60+
Name of Excel sheet or the page number of the sheet
6161
header : int, default 0
62-
Row to use for the column labels of the parsed DataFrame
62+
Row to use for the column labels of the parsed DataFrame
6363
skiprows : list-like
6464
Rows to skip at the beginning (0-indexed)
6565
skip_footer : int, default 0
@@ -147,7 +147,7 @@ def __init__(self, io, **kwds):
147147
raise ValueError('Must explicitly set engine if not passing in'
148148
' buffer or path for io.')
149149

150-
def parse(self, sheetname, header=0, skiprows=None, skip_footer=0,
150+
def parse(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
151151
index_col=None, parse_cols=None, parse_dates=False,
152152
date_parser=None, na_values=None, thousands=None, chunksize=None,
153153
convert_float=True, has_index_names=False, **kwds):
@@ -200,7 +200,8 @@ def parse(self, sheetname, header=0, skiprows=None, skip_footer=0,
200200
if skipfooter is not None:
201201
skip_footer = skipfooter
202202

203-
return self._parse_excel(sheetname, header=header, skiprows=skiprows,
203+
return self._parse_excel(sheetname=sheetname, header=header,
204+
skiprows=skiprows,
204205
index_col=index_col,
205206
has_index_names=has_index_names,
206207
parse_cols=parse_cols,
@@ -244,7 +245,7 @@ def _excel2num(x):
244245
else:
245246
return i in parse_cols
246247

247-
def _parse_excel(self, sheetname, header=0, skiprows=None, skip_footer=0,
248+
def _parse_excel(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
248249
index_col=None, has_index_names=None, parse_cols=None,
249250
parse_dates=False, date_parser=None, na_values=None,
250251
thousands=None, chunksize=None, convert_float=True,

pandas/io/tests/test_excel.py

+9
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,15 @@ def test_roundtrip(self):
445445
na_values=[88, 88.0])
446446
tm.assert_frame_equal(self.frame, recons)
447447

448+
# GH 6573
449+
self.frame.to_excel(path, 'Sheet1')
450+
recons = read_excel(path, index_col=0)
451+
tm.assert_frame_equal(self.frame, recons)
452+
453+
self.frame.to_excel(path, '0')
454+
recons = read_excel(path, index_col=0)
455+
tm.assert_frame_equal(self.frame, recons)
456+
448457
def test_mixed(self):
449458
_skip_if_no_xlrd()
450459

0 commit comments

Comments
 (0)