Skip to content

Commit ba15ce6

Browse files
abarber4ghstangirala
authored andcommitted
DEPR: Change read_excel sheetname to sheet_name (pandas-dev#16442)
* GH10559: Minor improvement: Change to_excel sheet name modify io/excel.py and relevant docs (io.rst) to use sheet_name for read_excel but allow sheetname to still be used for backwards compatibility. add test_excel to verify that sheet_name and sheetname args produce the same result. * GH10559: Minor improvement: Change to_excel sheet name added @deprecate_kwarg to read_excel as arg changes from sheetname to sheet_name. moved test comments into function, add assert_produces_warning. * GH10559: Minor improvement: Change to_excel sheet name remove manual arg change, use @deprecate_kwarg to read_excel as arg changes from sheetname to sheet_name. * GH10559: Minor improvement: Change to_excel sheet name shorten lines under 79 char. * GH10559: Minor improvement: Change to_excel sheet name update whats new 0.21.0 Deprecations section noting sheetname deprecated in favor of sheet_name. add sheetname deprecation in read_excel() docstring.
1 parent 9ec38fc commit ba15ce6

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

doc/source/io.rst

+8-8
Original file line numberDiff line numberDiff line change
@@ -2561,12 +2561,12 @@ Reading Excel Files
25612561
'''''''''''''''''''
25622562

25632563
In the most basic use-case, ``read_excel`` takes a path to an Excel
2564-
file, and the ``sheetname`` indicating which sheet to parse.
2564+
file, and the ``sheet_name`` indicating which sheet to parse.
25652565

25662566
.. code-block:: python
25672567
25682568
# Returns a DataFrame
2569-
read_excel('path_to_file.xls', sheetname='Sheet1')
2569+
read_excel('path_to_file.xls', sheet_name='Sheet1')
25702570
25712571
25722572
.. _io.excel.excelfile_class:
@@ -2634,12 +2634,12 @@ of sheet names can simply be passed to ``read_excel`` with no loss in performanc
26342634
Specifying Sheets
26352635
+++++++++++++++++
26362636

2637-
.. note :: The second argument is ``sheetname``, not to be confused with ``ExcelFile.sheet_names``
2637+
.. note :: The second argument is ``sheet_name``, not to be confused with ``ExcelFile.sheet_names``
26382638
26392639
.. note :: An ExcelFile's attribute ``sheet_names`` provides access to a list of sheets.
26402640
2641-
- The arguments ``sheetname`` allows specifying the sheet or sheets to read.
2642-
- The default value for ``sheetname`` is 0, indicating to read the first sheet
2641+
- The arguments ``sheet_name`` allows specifying the sheet or sheets to read.
2642+
- The default value for ``sheet_name`` is 0, indicating to read the first sheet
26432643
- Pass a string to refer to the name of a particular sheet in the workbook.
26442644
- Pass an integer to refer to the index of a sheet. Indices follow Python
26452645
convention, beginning at 0.
@@ -2670,18 +2670,18 @@ Using None to get all sheets:
26702670
.. code-block:: python
26712671
26722672
# Returns a dictionary of DataFrames
2673-
read_excel('path_to_file.xls',sheetname=None)
2673+
read_excel('path_to_file.xls',sheet_name=None)
26742674
26752675
Using a list to get multiple sheets:
26762676

26772677
.. code-block:: python
26782678
26792679
# Returns the 1st and 4th sheet, as a dictionary of DataFrames.
2680-
read_excel('path_to_file.xls',sheetname=['Sheet1',3])
2680+
read_excel('path_to_file.xls',sheet_name=['Sheet1',3])
26812681
26822682
.. versionadded:: 0.16
26832683

2684-
``read_excel`` can read more than one sheet, by setting ``sheetname`` to either
2684+
``read_excel`` can read more than one sheet, by setting ``sheet_name`` to either
26852685
a list of sheet names, a list of sheet positions, or ``None`` to read all sheets.
26862686

26872687
.. versionadded:: 0.13

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Other API Changes
5959

6060
Deprecations
6161
~~~~~~~~~~~~
62-
62+
- :func:`read_excel()` has deprecated ``sheetname`` in favor of ``sheet_name`` for consistency with to_excel() (:issue:`10559`).
6363

6464

6565
.. _whatsnew_0210.prior_deprecations:

pandas/io/excel.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import pandas.compat.openpyxl_compat as openpyxl_compat
3131
from warnings import warn
3232
from distutils.version import LooseVersion
33-
from pandas.util._decorators import Appender
33+
from pandas.util._decorators import Appender, deprecate_kwarg
3434
from textwrap import fill
3535

3636
__all__ = ["read_excel", "ExcelWriter", "ExcelFile"]
@@ -48,7 +48,7 @@
4848
The string could be a URL. Valid URL schemes include http, ftp, s3,
4949
and file. For file URLs, a host is expected. For instance, a local
5050
file could be file://localhost/path/to/workbook.xlsx
51-
sheetname : string, int, mixed list of strings/ints, or None, default 0
51+
sheet_name : string, int, mixed list of strings/ints, or None, default 0
5252
5353
Strings are used for sheet names, Integers are used in zero-indexed
5454
sheet positions.
@@ -69,6 +69,10 @@
6969
* [0,1,"Sheet5"] -> 1st, 2nd & 5th sheet as a dictionary of DataFrames
7070
* None -> All sheets as a dictionary of DataFrames
7171
72+
sheetname : string, int, mixed list of strings/ints, or None, default 0
73+
.. deprecated:: 0.21.0
74+
Use `sheet_name` instead
75+
7276
header : int, list of ints, default 0
7377
Row (0-indexed) to use for the column labels of the parsed
7478
DataFrame. If a list of integers is passed those row positions will
@@ -144,7 +148,7 @@
144148
Returns
145149
-------
146150
parsed : DataFrame or Dict of DataFrames
147-
DataFrame from the passed in Excel file. See notes in sheetname
151+
DataFrame from the passed in Excel file. See notes in sheet_name
148152
argument for more information on when a Dict of Dataframes is returned.
149153
"""
150154

@@ -189,8 +193,9 @@ def get_writer(engine_name):
189193
raise ValueError("No Excel writer '%s'" % engine_name)
190194

191195

196+
@deprecate_kwarg('sheetname', 'sheet_name')
192197
@Appender(_read_excel_doc)
193-
def read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0,
198+
def read_excel(io, sheet_name=0, header=0, skiprows=None, skip_footer=0,
194199
index_col=None, names=None, parse_cols=None, parse_dates=False,
195200
date_parser=None, na_values=None, thousands=None,
196201
convert_float=True, has_index_names=None, converters=None,
@@ -201,7 +206,7 @@ def read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0,
201206
io = ExcelFile(io, engine=engine)
202207

203208
return io._parse_excel(
204-
sheetname=sheetname, header=header, skiprows=skiprows, names=names,
209+
sheetname=sheet_name, header=header, skiprows=skiprows, names=names,
205210
index_col=index_col, parse_cols=parse_cols, parse_dates=parse_dates,
206211
date_parser=date_parser, na_values=na_values, thousands=thousands,
207212
convert_float=convert_float, has_index_names=has_index_names,
@@ -266,7 +271,7 @@ def __init__(self, io, **kwds):
266271
def __fspath__(self):
267272
return self._io
268273

269-
def parse(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
274+
def parse(self, sheet_name=0, header=0, skiprows=None, skip_footer=0,
270275
names=None, index_col=None, parse_cols=None, parse_dates=False,
271276
date_parser=None, na_values=None, thousands=None,
272277
convert_float=True, has_index_names=None,
@@ -279,7 +284,7 @@ def parse(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
279284
docstring for more info on accepted parameters
280285
"""
281286

282-
return self._parse_excel(sheetname=sheetname, header=header,
287+
return self._parse_excel(sheetname=sheet_name, header=header,
283288
skiprows=skiprows, names=names,
284289
index_col=index_col,
285290
has_index_names=has_index_names,

pandas/tests/io/test_excel.py

+12
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,18 @@ def test_date_conversion_overflow(self):
544544
result = self.get_exceldf('testdateoverflow')
545545
tm.assert_frame_equal(result, expected)
546546

547+
def test_sheet_name_and_sheetname(self):
548+
# GH10559: Minor improvement: Change "sheet_name" to "sheetname"
549+
# GH10969: DOC: Consistent var names (sheetname vs sheet_name)
550+
# GH12604: CLN GH10559 Rename sheetname variable to sheet_name
551+
dfref = self.get_csv_refdf('test1')
552+
df1 = self.get_exceldf('test1', sheet_name='Sheet1') # doc
553+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
554+
df2 = self.get_exceldf('test1', sheetname='Sheet2') # bkwrd compat
555+
556+
tm.assert_frame_equal(df1, dfref, check_names=False)
557+
tm.assert_frame_equal(df2, dfref, check_names=False)
558+
547559

548560
class XlrdTests(ReadingTestsBase):
549561
"""

0 commit comments

Comments
 (0)