Skip to content

Commit 6c692ae

Browse files
OXPHOSjreback
authored andcommitted
BUG: add names parameter to read_excel
closes #12870 closes #12895
1 parent 2ea0601 commit 6c692ae

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

doc/source/whatsnew/v0.18.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,4 @@ Bug Fixes
320320
- Bug in ``.describe()`` resets categorical columns information (:issue:`11558`)
321321
- Bug where ``loffset`` argument was not applied when calling ``resample().count()`` on a timeseries (:issue:`12725`)
322322
- ``pd.read_excel()`` now accepts path objects (e.g. ``pathlib.Path``, ``py.path.local``) for the file path, in line with other ``read_*`` functions (:issue:`12655`)
323+
- ``pd.read_excel()`` now accepts column names associated with keyword argument ``names``(:issue `12870`)

pandas/io/excel.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0,
170170
io = ExcelFile(io, engine=engine)
171171

172172
return io._parse_excel(
173-
sheetname=sheetname, header=header, skiprows=skiprows,
173+
sheetname=sheetname, header=header, skiprows=skiprows, names=names,
174174
index_col=index_col, parse_cols=parse_cols, parse_dates=parse_dates,
175175
date_parser=date_parser, na_values=na_values, thousands=thousands,
176176
convert_float=convert_float, has_index_names=has_index_names,
@@ -230,7 +230,7 @@ def __init__(self, io, **kwds):
230230
' buffer or path for io.')
231231

232232
def parse(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
233-
index_col=None, parse_cols=None, parse_dates=False,
233+
names=None, index_col=None, parse_cols=None, parse_dates=False,
234234
date_parser=None, na_values=None, thousands=None,
235235
convert_float=True, has_index_names=None,
236236
converters=None, squeeze=False, **kwds):
@@ -242,7 +242,7 @@ def parse(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
242242
"""
243243

244244
return self._parse_excel(sheetname=sheetname, header=header,
245-
skiprows=skiprows,
245+
skiprows=skiprows, names=names,
246246
index_col=index_col,
247247
has_index_names=has_index_names,
248248
parse_cols=parse_cols,
@@ -288,10 +288,10 @@ def _excel2num(x):
288288
else:
289289
return i in parse_cols
290290

291-
def _parse_excel(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
292-
index_col=None, has_index_names=None, parse_cols=None,
293-
parse_dates=False, date_parser=None, na_values=None,
294-
thousands=None, convert_float=True,
291+
def _parse_excel(self, sheetname=0, header=0, skiprows=None, names=None,
292+
skip_footer=0, index_col=None, has_index_names=None,
293+
parse_cols=None, parse_dates=False, date_parser=None,
294+
na_values=None, thousands=None, convert_float=True,
295295
verbose=False, squeeze=False, **kwds):
296296

297297
skipfooter = kwds.pop('skipfooter', None)
@@ -465,6 +465,8 @@ def _parse_cell(cell_contents, cell_typ):
465465
**kwds)
466466

467467
output[asheetname] = parser.read()
468+
if names is not None:
469+
output[asheetname].columns = names
468470
if not squeeze or isinstance(output[asheetname], DataFrame):
469471
output[asheetname].columns = output[
470472
asheetname].columns.set_names(header_names)

pandas/io/tests/test_excel.py

+23
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,29 @@ def test_read_one_empty_col_with_header(self):
458458
expected_header_zero = DataFrame(columns=[0], dtype='int64')
459459
tm.assert_frame_equal(actual_header_zero, expected_header_zero)
460460

461+
def test_set_column_names_in_parameter(self):
462+
# GH 12870 : pass down column names associated with
463+
# keyword argument names
464+
refdf = pd.DataFrame([[1, 'foo'], [2, 'bar'],
465+
[3, 'baz']], columns=['a', 'b'])
466+
467+
with ensure_clean(self.ext) as pth:
468+
with ExcelWriter(pth) as writer:
469+
refdf.to_excel(writer, 'Data_no_head',
470+
header=False, index=False)
471+
refdf.to_excel(writer, 'Data_with_head', index=False)
472+
473+
refdf.columns = ['A', 'B']
474+
475+
with ExcelFile(pth) as reader:
476+
xlsdf_no_head = read_excel(reader, 'Data_no_head',
477+
header=None, names=['A', 'B'])
478+
xlsdf_with_head = read_excel(reader, 'Data_with_head',
479+
index_col=None, names=['A', 'B'])
480+
481+
tm.assert_frame_equal(xlsdf_no_head, refdf)
482+
tm.assert_frame_equal(xlsdf_with_head, refdf)
483+
461484

462485
class XlrdTests(ReadingTestsBase):
463486
"""

0 commit comments

Comments
 (0)