Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH10559: Minor improvement: Change read_excel sheet name #16442

Merged
merged 5 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions doc/source/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2561,12 +2561,12 @@ Reading Excel Files
'''''''''''''''''''

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

.. code-block:: python

# Returns a DataFrame
read_excel('path_to_file.xls', sheetname='Sheet1')
read_excel('path_to_file.xls', sheet_name='Sheet1')


.. _io.excel.excelfile_class:
Expand Down Expand Up @@ -2634,12 +2634,12 @@ of sheet names can simply be passed to ``read_excel`` with no loss in performanc
Specifying Sheets
+++++++++++++++++

.. note :: The second argument is ``sheetname``, not to be confused with ``ExcelFile.sheet_names``
.. note :: The second argument is ``sheet_name``, not to be confused with ``ExcelFile.sheet_names``

.. note :: An ExcelFile's attribute ``sheet_names`` provides access to a list of sheets.

- The arguments ``sheetname`` allows specifying the sheet or sheets to read.
- The default value for ``sheetname`` is 0, indicating to read the first sheet
- The arguments ``sheet_name`` allows specifying the sheet or sheets to read.
- The default value for ``sheet_name`` is 0, indicating to read the first sheet
- Pass a string to refer to the name of a particular sheet in the workbook.
- Pass an integer to refer to the index of a sheet. Indices follow Python
convention, beginning at 0.
Expand Down Expand Up @@ -2670,18 +2670,18 @@ Using None to get all sheets:
.. code-block:: python

# Returns a dictionary of DataFrames
read_excel('path_to_file.xls',sheetname=None)
read_excel('path_to_file.xls',sheet_name=None)

Using a list to get multiple sheets:

.. code-block:: python

# Returns the 1st and 4th sheet, as a dictionary of DataFrames.
read_excel('path_to_file.xls',sheetname=['Sheet1',3])
read_excel('path_to_file.xls',sheet_name=['Sheet1',3])

.. versionadded:: 0.16

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

.. versionadded:: 0.13
Expand Down
16 changes: 10 additions & 6 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
The string could be a URL. Valid URL schemes include http, ftp, s3,
and file. For file URLs, a host is expected. For instance, a local
file could be file://localhost/path/to/workbook.xlsx
sheetname : string, int, mixed list of strings/ints, or None, default 0
sheet_name : string, int, mixed list of strings/ints, or None, default 0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add sheetname (DEPRECATED) as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also the deprecated sphinx directive. I don't see any uses of that, but we can give it a shot here. I think it'd be like

sheetname : string, int, mixed list of strings/ints, or None, default 0

    .. deprecated:: 0.21.0
       Use `sheet_name` instead

sheet_name : string, int, mixed list of strings/ints, or None, default 0

Strings are used for sheet names, Integers are used in zero-indexed
sheet positions.
Expand Down Expand Up @@ -144,7 +144,7 @@
Returns
-------
parsed : DataFrame or Dict of DataFrames
DataFrame from the passed in Excel file. See notes in sheetname
DataFrame from the passed in Excel file. See notes in sheet_name
argument for more information on when a Dict of Dataframes is returned.
"""

Expand Down Expand Up @@ -190,7 +190,7 @@ def get_writer(engine_name):


@Appender(_read_excel_doc)
def read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0,
def read_excel(io, sheet_name=0, header=0, skiprows=None, skip_footer=0,
index_col=None, names=None, parse_cols=None, parse_dates=False,
date_parser=None, na_values=None, thousands=None,
convert_float=True, has_index_names=None, converters=None,
Expand All @@ -200,8 +200,12 @@ def read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0,
if not isinstance(io, ExcelFile):
io = ExcelFile(io, engine=engine)

# maintain backwards compatibility by converting sheetname to sheet_name
if 'sheetname' in kwds:
sheet_name = kwds.pop('sheetname')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a deprecation warning on the original arg. You need to also accept the original arg as a kwarg.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use pandas.util._decorators.deprecate_kwarg


return io._parse_excel(
sheetname=sheetname, header=header, skiprows=skiprows, names=names,
sheetname=sheet_name, header=header, skiprows=skiprows, names=names,
index_col=index_col, parse_cols=parse_cols, parse_dates=parse_dates,
date_parser=date_parser, na_values=na_values, thousands=thousands,
convert_float=convert_float, has_index_names=has_index_names,
Expand Down Expand Up @@ -266,7 +270,7 @@ def __init__(self, io, **kwds):
def __fspath__(self):
return self._io

def parse(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
def parse(self, sheet_name=0, header=0, skiprows=None, skip_footer=0,
names=None, index_col=None, parse_cols=None, parse_dates=False,
date_parser=None, na_values=None, thousands=None,
convert_float=True, has_index_names=None,
Expand All @@ -279,7 +283,7 @@ def parse(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
docstring for more info on accepted parameters
"""

return self._parse_excel(sheetname=sheetname, header=header,
return self._parse_excel(sheetname=sheet_name, header=header,
skiprows=skiprows, names=names,
index_col=index_col,
has_index_names=has_index_names,
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,18 @@ def test_date_conversion_overflow(self):
result = self.get_exceldf('testdateoverflow')
tm.assert_frame_equal(result, expected)

# GH10559: Minor improvement: Change to_excel "sheet_name" to "sheetname"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments go inside the function.

# GH10969: DOC: Consistent variable names (sheetname vs sheet_name, issue 10559)
# GH12604: CLN GH10559 Rename sheetname variable to sheet_name for consistency
def test_sheet_name_and_sheetname(self):
dfref = self.get_csv_refdf('test1')
df1 = self.get_exceldf('test1', sheet_name='Sheet1') # doc
df2 = self.get_exceldf('test1', sheetname='Sheet2') # bkwrds compat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should show a FutureWarning

Copy link
Contributor

@TomAugspurger TomAugspurger May 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can assert that a warning is issued with thetm.assert_produces_warning context manager.


tm.assert_frame_equal(df1, dfref, check_names=False)
tm.assert_frame_equal(df2, dfref, check_names=False)



class XlrdTests(ReadingTestsBase):
"""
Expand Down