diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 41d519e0765dc..ee7ead19faec9 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -794,6 +794,7 @@ I/O - Bug in :meth:`~DataFrame.read_feather` was raising an `ArrowIOError` when reading an s3 or http file path (:issue:`29055`) - Bug in :meth:`read_parquet` was raising a ``FileNotFoundError`` when passed an s3 directory path. (:issue:`26388`) - Bug in :meth:`~DataFrame.to_parquet` was throwing an ``AttributeError`` when writing a partitioned parquet file to s3 (:issue:`27596`) +- Bug in :meth:`~DataFrame.to_excel` could not handle the column name `render` and was raising an ``KeyError`` (:issue:`34331`) Plotting ^^^^^^^^ diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 4ec20977a5e7f..bf4586a4b5b96 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -16,7 +16,7 @@ from pandas.core.dtypes.common import is_float, is_scalar from pandas.core.dtypes.generic import ABCIndex -from pandas import Index, MultiIndex, PeriodIndex +from pandas import DataFrame, Index, MultiIndex, PeriodIndex import pandas.core.common as com from pandas.io.common import stringify_path @@ -385,7 +385,7 @@ def __init__( ): self.rowcounter = 0 self.na_rep = na_rep - if hasattr(df, "render"): + if not isinstance(df, DataFrame): self.styler = df df = df.data if style_converter is None: diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py index 1692e1a8a0dd3..ff366036714e4 100644 --- a/pandas/tests/io/excel/test_writers.py +++ b/pandas/tests/io/excel/test_writers.py @@ -1201,6 +1201,14 @@ def test_write_lists_dict(self, path): tm.assert_frame_equal(read, expected) + def test_render_as_column_name(self, path): + # see gh-34331 + df = DataFrame({"render": [1, 2], "data": [3, 4]}) + df.to_excel(path, "Sheet1") + read = pd.read_excel(path, "Sheet1", index_col=0) + expected = df + tm.assert_frame_equal(read, expected) + def test_true_and_false_value_options(self, path): # see gh-13347 df = pd.DataFrame([["foo", "bar"]], columns=["col1", "col2"])