From 11bed216c3021e19618a9c65e6c0430cdf887826 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Wed, 24 May 2017 09:37:06 -0500 Subject: [PATCH] BUG: Handle sheetname deprecation directly Since sheetname=None has a special meaning, we can't use the deprecate_kwargs decorator. We instead handle it in read_excel. --- pandas/io/excel.py | 13 +++++++++++-- pandas/tests/io/test_excel.py | 14 +++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pandas/io/excel.py b/pandas/io/excel.py index 81a36b21b3617..aa08e5fd378f0 100644 --- a/pandas/io/excel.py +++ b/pandas/io/excel.py @@ -8,6 +8,7 @@ import os import abc +import warnings import numpy as np from pandas.core.dtypes.common import ( @@ -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"] @@ -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, @@ -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) diff --git a/pandas/tests/io/test_excel.py b/pandas/tests/io/test_excel.py index 3b7e3e31a6971..07f32ca39f0d7 100644 --- a/pandas/tests/io/test_excel.py +++ b/pandas/tests/io/test_excel.py @@ -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()) @@ -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()) @@ -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()) @@ -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): """ @@ -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 @@ -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])