Skip to content

Commit

Permalink
BUG: Handle sheetname deprecation directly
Browse files Browse the repository at this point in the history
Since sheetname=None has a special meaning, we can't use the deprecate_kwargs
decorator. We instead handle it in read_excel.
  • Loading branch information
TomAugspurger committed May 25, 2017
1 parent f6cbf8f commit 11bed21
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
13 changes: 11 additions & 2 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import os
import abc
import warnings
import numpy as np

from pandas.core.dtypes.common import (
Expand All @@ -30,7 +31,7 @@
import pandas.compat.openpyxl_compat as openpyxl_compat
from warnings import warn
from distutils.version import LooseVersion
from pandas.util._decorators import Appender, deprecate_kwarg
from pandas.util._decorators import Appender
from textwrap import fill

__all__ = ["read_excel", "ExcelWriter", "ExcelFile"]
Expand Down Expand Up @@ -193,7 +194,6 @@ def get_writer(engine_name):
raise ValueError("No Excel writer '%s'" % engine_name)


@deprecate_kwarg('sheetname', 'sheet_name')
@Appender(_read_excel_doc)
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,
Expand All @@ -202,6 +202,15 @@ def read_excel(io, sheet_name=0, header=0, skiprows=None, skip_footer=0,
dtype=None, true_values=None, false_values=None, engine=None,
squeeze=False, **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)

Expand Down
14 changes: 9 additions & 5 deletions pandas/tests/io/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def test_reading_all_sheets(self):
# Ensure a dict is returned.
# See PR #9450
basename = 'test_multisheet'
dfs = self.get_exceldf(basename, sheetname=None)
dfs = self.get_exceldf(basename, sheet_name=None)
# ensure this is not alphabetical to test order preservation
expected_keys = ['Charlie', 'Alpha', 'Beta']
tm.assert_contains_all(expected_keys, dfs.keys())
Expand All @@ -424,7 +424,7 @@ def test_reading_multiple_specific_sheets(self):
basename = 'test_multisheet'
# Explicitly request duplicates. Only the set should be returned.
expected_keys = [2, 'Charlie', 'Charlie']
dfs = self.get_exceldf(basename, sheetname=expected_keys)
dfs = self.get_exceldf(basename, sheet_name=expected_keys)
expected_keys = list(set(expected_keys))
tm.assert_contains_all(expected_keys, dfs.keys())
assert len(expected_keys) == len(dfs.keys())
Expand All @@ -434,7 +434,7 @@ def test_reading_all_sheets_with_blank(self):
# In the case where some sheets are blank.
# Issue #11711
basename = 'blank_with_header'
dfs = self.get_exceldf(basename, sheetname=None)
dfs = self.get_exceldf(basename, sheet_name=None)
expected_keys = ['Sheet1', 'Sheet2', 'Sheet3']
tm.assert_contains_all(expected_keys, dfs.keys())

Expand Down Expand Up @@ -556,6 +556,10 @@ def test_sheet_name_and_sheetname(self):
tm.assert_frame_equal(df1, dfref, check_names=False)
tm.assert_frame_equal(df2, dfref, check_names=False)

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


class XlrdTests(ReadingTestsBase):
"""
Expand Down Expand Up @@ -589,7 +593,7 @@ def test_read_xlrd_Book(self):
result = read_excel(xl, "SheetA")
tm.assert_frame_equal(df, result)

result = read_excel(book, sheetname="SheetA", engine="xlrd")
result = read_excel(book, sheet_name="SheetA", engine="xlrd")
tm.assert_frame_equal(df, result)

@tm.network
Expand Down Expand Up @@ -691,7 +695,7 @@ def tdf(sheetname):
with ExcelWriter(pth) as ew:
for sheetname, df in iteritems(dfs):
df.to_excel(ew, sheetname)
dfs_returned = read_excel(pth, sheetname=sheets)
dfs_returned = read_excel(pth, sheet_name=sheets)
for s in sheets:
tm.assert_frame_equal(dfs[s], dfs_returned[s])

Expand Down

0 comments on commit 11bed21

Please sign in to comment.