Skip to content

Commit d0a6391

Browse files
committed
rename keyword 'as_table' to 'table', and change type from boolean to string
1 parent 9636999 commit d0a6391

File tree

5 files changed

+92
-67
lines changed

5 files changed

+92
-67
lines changed

pandas/core/generic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,8 +2182,8 @@ def _repr_data_resource_(self):
21822182
freeze_panes : tuple of int (length 2), optional
21832183
Specifies the one-based bottommost row and rightmost column that
21842184
is to be frozen.
2185-
as_table : boolean, default False
2186-
Write the dataframe as a formatted excel table object
2185+
table : string, default None
2186+
Write the dataframe to a named and formatted excel table object
21872187
21882188
See Also
21892189
--------
@@ -2249,7 +2249,7 @@ def to_excel(
22492249
inf_rep="inf",
22502250
verbose=True,
22512251
freeze_panes=None,
2252-
as_table=False
2252+
table=None
22532253
):
22542254
df = self if isinstance(self, ABCDataFrame) else self.to_frame()
22552255

@@ -2273,7 +2273,7 @@ def to_excel(
22732273
startcol=startcol,
22742274
freeze_panes=freeze_panes,
22752275
engine=engine,
2276-
as_table=as_table
2276+
table=None
22772277
)
22782278

22792279
def to_json(

pandas/io/excel/_openpyxl.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,20 @@ def write_cells(
467467
for k, v in style_kwargs.items():
468468
setattr(xcell, k, v)
469469

470-
def write_table(self, cells, sheet_name=None, startrow=0, startcol=0,
471-
freeze_panes=None, header=True):
470+
def write_table(
471+
self,
472+
cells,
473+
table,
474+
sheet_name=None,
475+
startrow=0,
476+
startcol=0,
477+
freeze_panes=None,
478+
header=True,
479+
):
472480
# Write the frame to an excel table using openpyxl.
473481

474482
from openpyxl.worksheet.table import Table, TableStyleInfo
483+
475484
sheet_name = self._get_sheet_name(sheet_name)
476485

477486
if sheet_name in self.sheets:
@@ -482,8 +491,9 @@ def write_table(self, cells, sheet_name=None, startrow=0, startcol=0,
482491
self.sheets[sheet_name] = wks
483492

484493
if _validate_freeze_panes(freeze_panes):
485-
wks.freeze_panes = wks.cell(row=freeze_panes[0] + 1,
486-
column=freeze_panes[1] + 1)
494+
wks.freeze_panes = wks.cell(
495+
row=freeze_panes[0] + 1, column=freeze_panes[1] + 1
496+
)
487497

488498
header_rows = 1 if header > 0 else 0
489499

@@ -496,9 +506,7 @@ def write_table(self, cells, sheet_name=None, startrow=0, startcol=0,
496506
header_cells[cell.col] = cell.val
497507
continue
498508
wks.cell(
499-
row=startrow + cell.row + 1,
500-
column=startcol + cell.col + 1,
501-
value=val,
509+
row=startrow + cell.row + 1, column=startcol + cell.col + 1, value=val
502510
)
503511
n_cols = max(n_cols, cell.col)
504512
n_rows = max(n_rows, cell.row)
@@ -508,25 +516,27 @@ def write_table(self, cells, sheet_name=None, startrow=0, startcol=0,
508516
if col in header_cells:
509517
val = str(header_cells[col])
510518
else:
511-
val = 'Column%d' % (col + 1)
512-
wks.cell(
513-
row=startrow + 1,
514-
column=startcol + col + 1,
515-
value=val,
516-
)
519+
val = "Column%d" % (col + 1)
520+
wks.cell(row=startrow + 1, column=startcol + col + 1, value=val)
517521

518-
ref = self._to_excel_range(startrow, startcol, startrow + n_rows,
519-
startcol + n_cols)
520-
tab = Table(displayName="Table1", ref=ref, headerRowCount=header_rows)
522+
ref = self._to_excel_range(
523+
startrow, startcol, startrow + n_rows, startcol + n_cols
524+
)
525+
tab = Table(displayName=table, ref=ref, headerRowCount=header_rows)
521526

522527
# Add a default style with striped rows
523528
style = TableStyleInfo(
524-
name="TableStyleMedium9", showFirstColumn=False,
525-
showLastColumn=False, showRowStripes=True, showColumnStripes=False)
529+
name="TableStyleMedium9",
530+
showFirstColumn=False,
531+
showLastColumn=False,
532+
showRowStripes=True,
533+
showColumnStripes=False,
534+
)
526535
tab.tableStyleInfo = style
527536

528537
wks.add_table(tab)
529538

539+
530540
class _OpenpyxlReader(_BaseExcelReader):
531541
def __init__(self, filepath_or_buffer: FilePathOrBuffer) -> None:
532542
"""Reader using openpyxl engine.

pandas/io/excel/_xlsxwriter.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,16 @@ def write_cells(
236236
else:
237237
wks.write(startrow + cell.row, startcol + cell.col, val, style)
238238

239-
def write_table(self, cells, sheet_name=None, startrow=0, startcol=0,
240-
freeze_panes=None, header=True):
239+
def write_table(
240+
self,
241+
cells,
242+
table,
243+
sheet_name=None,
244+
startrow=0,
245+
startcol=0,
246+
freeze_panes=None,
247+
header=True,
248+
):
241249
# Write the frame to an excel table using xlsxwriter.
242250
sheet_name = self._get_sheet_name(sheet_name)
243251

@@ -258,18 +266,21 @@ def write_table(self, cells, sheet_name=None, startrow=0, startcol=0,
258266
if header and cell.row == 0:
259267
header_cells[cell.col] = cell.val
260268
continue
261-
wks.write(startrow + cell.row,
262-
startcol + cell.col,
263-
val)
269+
wks.write(startrow + cell.row, startcol + cell.col, val)
264270
n_cols = max(n_cols, cell.col)
265271
n_rows = max(n_rows, cell.row)
266272

267273
# add generic name for every unnamed (index) column that is included
268-
columns = [{'header': str(header_cells[col])
269-
if col in header_cells else 'Column%d' % (col + 1)}
270-
for col in range(n_cols + 1)]
274+
columns = [
275+
{
276+
"header": str(header_cells[col])
277+
if col in header_cells
278+
else "Column%d" % (col + 1)
279+
}
280+
for col in range(n_cols + 1)
281+
]
282+
283+
options = {"columns": columns, "name": table}
271284

272-
options = {'columns': columns}
285+
wks.add_table(startrow, startcol, startrow + n_rows, startcol + n_cols, options)
273286

274-
wks.add_table(startrow, startcol, startrow + n_rows,
275-
startcol + n_cols, options)

pandas/io/formats/excel.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,12 @@ def _generate_body(self, coloffset):
682682
xlstyle = self.style_converter(";".join(styles[i, colidx]))
683683
yield ExcelCell(self.rowcounter + i, colidx + coloffset, val, xlstyle)
684684

685-
def get_formatted_cells(self):
686-
for cell in itertools.chain(self._format_header(), self._format_body()):
685+
def get_formatted_cells(self, include_header=True):
686+
if include_header:
687+
cells = itertools.chain(self._format_header(), self._format_body())
688+
else:
689+
cells = self._format_body()
690+
for cell in cells:
687691
cell.val = self._format_value(cell.val)
688692
yield cell
689693

@@ -695,6 +699,7 @@ def write(
695699
startcol=0,
696700
freeze_panes=None,
697701
engine=None,
702+
table=None,
698703
):
699704
"""
700705
writer : string or ExcelWriter object
@@ -712,6 +717,9 @@ def write(
712717
write engine to use if writer is a path - you can also set this
713718
via the options ``io.excel.xlsx.writer``, ``io.excel.xls.writer``,
714719
and ``io.excel.xlsm.writer``.
720+
table : string, default None
721+
Write the dataframe to a named and formatted excel table object
722+
715723
"""
716724
from pandas.io.excel import ExcelWriter
717725
from pandas.io.common import _stringify_path
@@ -731,12 +739,23 @@ def write(
731739
need_save = True
732740

733741
formatted_cells = self.get_formatted_cells()
734-
writer.write_cells(
735-
formatted_cells,
736-
sheet_name,
737-
startrow=startrow,
738-
startcol=startcol,
739-
freeze_panes=freeze_panes,
740-
)
742+
if table is not None:
743+
writer.write_table(
744+
formatted_cells,
745+
sheet_name,
746+
table,
747+
startrow=startrow,
748+
startcol=startcol,
749+
freeze_panes=freeze_panes,
750+
header=self.header,
751+
)
752+
else:
753+
writer.write_cells(
754+
formatted_cells,
755+
sheet_name,
756+
startrow=startrow,
757+
startcol=startcol,
758+
freeze_panes=freeze_panes,
759+
)
741760
if need_save:
742761
writer.save()

pandas/tests/io/excel/test_writers.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -233,22 +233,23 @@ def test_read_excel_parse_dates(self, ext):
233233
)
234234
tm.assert_frame_equal(df, res)
235235

236-
def test_excel_as_table_roundtrip_indexname(self, engine, ext):
237-
if ext == '.xls':
236+
def test_excel_table_roundtrip_indexname(self, ext):
237+
if ext == ".xls":
238238
pytest.skip()
239239
df = DataFrame(np.random.randn(10, 4))
240240

241241
df.columns = df.columns.map(str)
242-
df.index.name = 'foo'
242+
df.index.name = "foo"
243243

244-
df.to_excel(self.path, header=True, as_table=True)
244+
with ensure_clean(ext) as pth:
245+
df.to_excel(pth, header=True, table='Table1')
245246

246-
xf = ExcelFile(self.path)
247-
result = pd.read_excel(xf, xf.sheet_names[0],
248-
index_col=0)
247+
xf = ExcelFile(pth)
248+
result = pd.read_excel(xf, xf.sheet_names[0], index_col=0)
249+
250+
tm.assert_frame_equal(result, df)
251+
assert result.index.name == "foo"
249252

250-
tm.assert_frame_equal(result, df)
251-
assert result.index.name == 'foo'
252253

253254
class _WriterBase:
254255
@pytest.fixture(autouse=True)
@@ -1227,22 +1228,6 @@ def test_raise_when_saving_timezones(self, engine, ext, dtype, tz_aware_fixture)
12271228
with pytest.raises(ValueError, match="Excel does not support"):
12281229
df.to_excel(self.path)
12291230

1230-
def test_excel_as_table_roundtrip_indexname(self, engine, ext):
1231-
if ext == '.xls':
1232-
pytest.skip()
1233-
df = DataFrame(np.random.randn(10, 4))
1234-
1235-
df.columns = df.columns.map(str)
1236-
df.index.name = 'foo'
1237-
1238-
df.to_excel(self.path, header=True, as_table=True)
1239-
1240-
xf = ExcelFile(self.path)
1241-
result = read_excel(xf, xf.sheet_names[0],
1242-
index_col=0)
1243-
1244-
tm.assert_frame_equal(result, df)
1245-
assert result.index.name == 'foo'
12461231

12471232
class TestExcelWriterEngineTests:
12481233
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)