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

CLN GH10559 Rename sheetname variable to sheet_name for consistency #12604

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 24 additions & 24 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_writer(engine_name):
raise ValueError("No Excel writer '%s'" % engine_name)


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,
Copy link
Contributor

Choose a reason for hiding this comment

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

so this will actually need to be deprecated, so re-add sheetname=None. Then if sheetname is not None, you will issue a warning that it is deprecated and reset sheet_name=sheetname.

This way if people pass as positional args it will work, and if they pass as a kwarg then it will show the deprecation warning (but still work).

Pls add a tests for this as well.

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 @@ -86,7 +86,7 @@ def read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0,
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

Strings are used for sheet names, Integers are used in zero-indexed
sheet positions.
Expand Down Expand Up @@ -162,14 +162,14 @@ def read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0,
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.
"""
if not isinstance(io, ExcelFile):
io = ExcelFile(io, engine=engine)

return io._parse_excel(
sheetname=sheetname, header=header, skiprows=skiprows,
sheet_name=sheet_name, header=header, skiprows=skiprows,
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 @@ -226,7 +226,7 @@ def __init__(self, io, **kwds):
raise ValueError('Must explicitly set engine if not passing in'
' buffer or path for 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,
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 @@ -238,7 +238,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(sheet_name=sheet_name, header=header,
skiprows=skiprows,
index_col=index_col,
has_index_names=has_index_names,
Expand Down Expand Up @@ -285,7 +285,7 @@ def _excel2num(x):
else:
return i in parse_cols

def _parse_excel(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
def _parse_excel(self, sheet_name=0, header=0, skiprows=None, skip_footer=0,
index_col=None, has_index_names=None, parse_cols=None,
parse_dates=False, date_parser=None, na_values=None,
thousands=None, convert_float=True,
Expand Down Expand Up @@ -370,29 +370,29 @@ def _parse_cell(cell_contents, cell_typ):

ret_dict = False

# Keep sheetname to maintain backwards compatibility.
if isinstance(sheetname, list):
sheets = sheetname
# Keep sheet_name to maintain backwards compatibility.
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(set(sheets))

output = {}

for asheetname in sheets:
for a_sheet_name in sheets:
if verbose:
print("Reading sheet %s" % asheetname)
print("Reading sheet %s" % a_sheet_name)

if isinstance(asheetname, compat.string_types):
sheet = self.book.sheet_by_name(asheetname)
if isinstance(a_sheet_name, compat.string_types):
sheet = self.book.sheet_by_name(a_sheet_name)
else: # assume an integer if not a string
sheet = self.book.sheet_by_index(asheetname)
sheet = self.book.sheet_by_index(a_sheet_name)

data = []
should_parse = {}
Expand All @@ -409,7 +409,7 @@ def _parse_cell(cell_contents, cell_typ):
data.append(row)

if sheet.nrows == 0:
output[asheetname] = DataFrame()
output[a_sheet_name] = DataFrame()
continue

if com.is_list_like(header) and len(header) == 1:
Expand Down Expand Up @@ -461,18 +461,18 @@ def _parse_cell(cell_contents, cell_typ):
squeeze=squeeze,
**kwds)

output[asheetname] = parser.read()
if not squeeze or isinstance(output[asheetname], DataFrame):
output[asheetname].columns = output[
asheetname].columns.set_names(header_names)
output[a_sheet_name] = parser.read()
if not squeeze or isinstance(output[a_sheet_name], DataFrame):
output[a_sheet_name].columns = output[
a_sheet_name].columns.set_names(header_names)
except StopIteration:
# No Data, return an empty DataFrame
output[asheetname] = DataFrame()
output[a_sheet_name] = DataFrame()

if ret_dict:
return output
else:
return output[asheetname]
return output[a_sheet_name]

@property
def sheet_names(self):
Expand Down
24 changes: 12 additions & 12 deletions pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,34 +360,34 @@ def test_reader_converters(self):
tm.assert_frame_equal(actual, expected)

def test_reading_all_sheets(self):
# Test reading all sheetnames by setting sheetname to None,
# Test reading all sheet_names by setting sheet_name to None,
# 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)
expected_keys = ['Alpha', 'Beta', 'Charlie']
tm.assert_contains_all(expected_keys, dfs.keys())

def test_reading_multiple_specific_sheets(self):
# Test reading specific sheetnames by specifying a mixed list
# Test reading specific sheet_names by specifying a mixed list
# of integers and strings, and confirm that duplicated sheet
# references (positions/names) are removed properly.
# Ensure a dict is returned
# See PR #9450
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())

def test_reading_all_sheets_with_blank(self):
# Test reading all sheetnames by setting sheetname to None,
# Test reading all sheet_names by setting sheet_name to None,
# 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 @@ -488,7 +488,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 @@ -546,9 +546,9 @@ def test_creating_and_reading_multiple_sheets(self):
_skip_if_no_xlwt()
_skip_if_no_openpyxl()

def tdf(sheetname):
def tdf(sheet_name):
d, i = [11, 22, 33], [1, 2, 3]
return DataFrame(d, i, columns=[sheetname])
return DataFrame(d, i, columns=[sheet_name])

sheets = ['AAA', 'BBB', 'CCC']

Expand All @@ -557,9 +557,9 @@ def tdf(sheetname):

with ensure_clean(self.ext) as pth:
with ExcelWriter(pth) as ew:
for sheetname, df in iteritems(dfs):
df.to_excel(ew, sheetname)
dfs_returned = read_excel(pth, sheetname=sheets)
for sheet_name, df in iteritems(dfs):
df.to_excel(ew, sheet_name)
dfs_returned = read_excel(pth, sheet_name=sheets)
for s in sheets:
tm.assert_frame_equal(dfs[s], dfs_returned[s])

Expand Down