Skip to content

Commit

Permalink
DEPR: deprecated sheetname from ExcelFile.parse (#20938)
Browse files Browse the repository at this point in the history
  • Loading branch information
deflatSOCO authored and jreback committed May 4, 2018
1 parent 28dbae9 commit ec4609e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Deprecations
retain the previous behavior, use a list instead of a tuple (:issue:`18314`)
- ``Series.valid`` is deprecated. Use :meth:`Series.dropna` instead (:issue:`18800`).
- :func:`read_excel` has deprecated the ``skip_footer`` parameter. Use ``skipfooter`` instead (:issue:`18836`)
- :meth:`ExcelFile.parse` has deprecated ``sheetname`` in favor of ``sheet_name`` for consistency with :func:`read_excel` (:issue:`20920`).
- The ``is_copy`` attribute is deprecated and will be removed in a future version (:issue:`18801`).
- ``IntervalIndex.from_intervals`` is deprecated in favor of the :class:`IntervalIndex` constructor (:issue:`19263`)
- ``DataFrame.from_items`` is deprecated. Use :func:`DataFrame.from_dict` instead, or ``DataFrame.from_dict(OrderedDict())`` if you wish to preserve the key order (:issue:`17320`, :issue:`17312`)
Expand Down
34 changes: 17 additions & 17 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,11 @@ def read_excel(io,
convert_float=True,
**kwds):

# Can't use _deprecate_kwarg since sheetname=None has a special meaning
if is_integer(sheet_name) and sheet_name == 0 and 'sheetname' in kwds:
warnings.warn("The `sheetname` keyword is deprecated, use "
"`sheet_name` instead", FutureWarning, stacklevel=2)
sheet_name = kwds.pop("sheetname")
elif 'sheetname' in kwds:
raise TypeError("Cannot specify both `sheet_name` and `sheetname`. "
"Use just `sheet_name`")

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

return io._parse_excel(
sheetname=sheet_name,
return io.parse(
sheet_name=sheet_name,
header=header,
names=names,
index_col=index_col,
Expand Down Expand Up @@ -435,7 +426,16 @@ def parse(self,
docstring for more info on accepted parameters
"""

return self._parse_excel(sheetname=sheet_name,
# Can't use _deprecate_kwarg since sheetname=None has a special meaning
if is_integer(sheet_name) and sheet_name == 0 and 'sheetname' in kwds:
warnings.warn("The `sheetname` keyword is deprecated, use "
"`sheet_name` instead", FutureWarning, stacklevel=2)
sheet_name = kwds.pop("sheetname")
elif 'sheetname' in kwds:
raise TypeError("Cannot specify both `sheet_name` "
"and `sheetname`. Use just `sheet_name`")

return self._parse_excel(sheet_name=sheet_name,
header=header,
names=names,
index_col=index_col,
Expand Down Expand Up @@ -489,7 +489,7 @@ def _excel2num(x):
return i in usecols

def _parse_excel(self,
sheetname=0,
sheet_name=0,
header=0,
names=None,
index_col=None,
Expand Down Expand Up @@ -585,14 +585,14 @@ def _parse_cell(cell_contents, cell_typ):
ret_dict = False

# Keep sheetname to maintain backwards compatibility.
if isinstance(sheetname, list):
sheets = sheetname
if isinstance(sheet_name, list):
sheets = sheet_name
ret_dict = True
elif sheetname is None:
elif sheet_name is None:
sheets = self.sheet_names
ret_dict = True
else:
sheets = [sheetname]
sheets = [sheet_name]

# handle same-type duplicates.
sheets = list(OrderedDict.fromkeys(sheets).keys())
Expand Down
17 changes: 16 additions & 1 deletion pandas/tests/io/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,20 +503,35 @@ def test_sheet_name_and_sheetname(self, ext):
# GH10559: Minor improvement: Change "sheet_name" to "sheetname"
# GH10969: DOC: Consistent var names (sheetname vs sheet_name)
# GH12604: CLN GH10559 Rename sheetname variable to sheet_name
# GH20920: ExcelFile.parse() and pd.read_xlsx() have different
# behavior for "sheetname" argument
dfref = self.get_csv_refdf('test1')
df1 = self.get_exceldf('test1', ext, sheet_name='Sheet1') # doc
df1 = self.get_exceldf('test1', ext,
sheet_name='Sheet1') # doc
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
df2 = self.get_exceldf('test1', ext,
sheetname='Sheet1') # bkwrd compat

excel = self.get_excelfile('test1', ext)
df1_parse = excel.parse(sheet_name='Sheet1') # doc
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
df2_parse = excel.parse(sheetname='Sheet1') # bkwrd compat

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

def test_sheet_name_both_raises(self, ext):
with tm.assert_raises_regex(TypeError, "Cannot specify both"):
self.get_exceldf('test1', ext, sheetname='Sheet1',
sheet_name='Sheet1')

excel = self.get_excelfile('test1', ext)
with tm.assert_raises_regex(TypeError, "Cannot specify both"):
excel.parse(sheetname='Sheet1',
sheet_name='Sheet1')


@pytest.mark.parametrize("ext", ['.xls', '.xlsx', '.xlsm'])
class TestXlrdReader(ReadingTestsBase):
Expand Down

0 comments on commit ec4609e

Please sign in to comment.