Skip to content

Commit fc6dd8d

Browse files
authored
BUG: Fix render as column name in to_excel (pandas-dev#34335)
1 parent b6ea970 commit fc6dd8d

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ I/O
823823
- Bug in :meth:`~DataFrame.read_feather` was raising an `ArrowIOError` when reading an s3 or http file path (:issue:`29055`)
824824
- Bug in :meth:`read_parquet` was raising a ``FileNotFoundError`` when passed an s3 directory path. (:issue:`26388`)
825825
- Bug in :meth:`~DataFrame.to_parquet` was throwing an ``AttributeError`` when writing a partitioned parquet file to s3 (:issue:`27596`)
826+
- Bug in :meth:`~DataFrame.to_excel` could not handle the column name `render` and was raising an ``KeyError`` (:issue:`34331`)
826827

827828
Plotting
828829
^^^^^^^^

pandas/io/formats/excel.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pandas.core.dtypes.common import is_float, is_scalar
1717
from pandas.core.dtypes.generic import ABCIndex
1818

19-
from pandas import Index, MultiIndex, PeriodIndex
19+
from pandas import DataFrame, Index, MultiIndex, PeriodIndex
2020
import pandas.core.common as com
2121

2222
from pandas.io.common import stringify_path
@@ -385,7 +385,7 @@ def __init__(
385385
):
386386
self.rowcounter = 0
387387
self.na_rep = na_rep
388-
if hasattr(df, "render"):
388+
if not isinstance(df, DataFrame):
389389
self.styler = df
390390
df = df.data
391391
if style_converter is None:

pandas/tests/io/excel/test_writers.py

+8
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,14 @@ def test_write_lists_dict(self, path):
12011201

12021202
tm.assert_frame_equal(read, expected)
12031203

1204+
def test_render_as_column_name(self, path):
1205+
# see gh-34331
1206+
df = DataFrame({"render": [1, 2], "data": [3, 4]})
1207+
df.to_excel(path, "Sheet1")
1208+
read = pd.read_excel(path, "Sheet1", index_col=0)
1209+
expected = df
1210+
tm.assert_frame_equal(read, expected)
1211+
12041212
def test_true_and_false_value_options(self, path):
12051213
# see gh-13347
12061214
df = pd.DataFrame([["foo", "bar"]], columns=["col1", "col2"])

0 commit comments

Comments
 (0)