Skip to content

BUG: add names parameter to read_excel #12895

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

Closed
wants to merge 3 commits into from
Closed
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: 9 additions & 7 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0,
io = ExcelFile(io, engine=engine)

return io._parse_excel(
sheetname=sheetname, header=header, skiprows=skiprows,
sheetname=sheetname, 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 @@ -230,7 +230,7 @@ def __init__(self, io, **kwds):
' buffer or path for io.')

def parse(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
index_col=None, parse_cols=None, parse_dates=False,
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,
converters=None, squeeze=False, **kwds):
Expand All @@ -242,7 +242,7 @@ def parse(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
"""

return self._parse_excel(sheetname=sheetname, header=header,
skiprows=skiprows,
skiprows=skiprows, names=names,
index_col=index_col,
has_index_names=has_index_names,
parse_cols=parse_cols,
Expand Down Expand Up @@ -288,10 +288,10 @@ def _excel2num(x):
else:
return i in parse_cols

def _parse_excel(self, sheetname=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,
def _parse_excel(self, sheetname=0, header=0, skiprows=None, names=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,
verbose=False, squeeze=False, **kwds):

skipfooter = kwds.pop('skipfooter', None)
Expand Down Expand Up @@ -465,6 +465,8 @@ def _parse_cell(cell_contents, cell_typ):
**kwds)

output[asheetname] = parser.read()
if names is not None:
output[asheetname].columns = names
if not squeeze or isinstance(output[asheetname], DataFrame):
output[asheetname].columns = output[
asheetname].columns.set_names(header_names)
Expand Down
Binary file added pandas/io/tests/data/testcolnameset.xls
Binary file not shown.
Binary file added pandas/io/tests/data/testcolnameset.xlsm
Binary file not shown.
Binary file added pandas/io/tests/data/testcolnameset.xlsx
Binary file not shown.
8 changes: 8 additions & 0 deletions pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,14 @@ def test_read_one_empty_col_with_header(self):
expected_header_zero = DataFrame(columns=[0], dtype='int64')
tm.assert_frame_equal(actual_header_zero, expected_header_zero)

def test_set_column_names_in_parameter(self):
excel = self.get_excelfile('testcolnameset')
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 the issue number here as a comment

refdf = pd.DataFrame([[1, 'foo'], [2, 'bar'],
[3, 'baz']], columns=['A', 'B'])

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think its necessary to include the files, instead I'd like to test with a dataframe that you write out, these cases:

  • write with no header, read back with the specified names (like you are testing here)
  • write with a header, and use names (I think we replace with the names at this point)

xlsdf = read_excel(excel, header=None, names=['A', 'B'])
tm.assert_frame_equal(xlsdf, refdf)


class XlrdTests(ReadingTestsBase):
"""
Expand Down